尝试从qtreeview的父行中获取特定的列值

时间:2019-07-15 17:22:23

标签: python pyqt5 qtreeview

我有一个用这样的分层数据填充的qtreeview:

- id1                    id1
    -id1.id2             id2
        - id1.id2.id3    id3

在我的非示例应用程序中,我希望能够右键单击最后一行并选择一个去链接菜单选项,然后该选项将删除并删除id2和id3之间的链接记录。为此,我需要孩子和父母的ID。

问题通过这段代码进入

    index = self.eventTree.currentIndex()
    parent_index = index.parent()
    text1 = parent_index.data()
    parent_index = self.model.index(parent_index.row(), 1)
    text2 = parent_index.data()

所以我从视图中获取当前索引,从子索引中获取父索引。到此为止,一切都会按我的预期进行。如果我右键单击id3的第二列,text1将等于id2。

由于我需要获取父行的特定列,因此我尝试使用model.index函数将父索引调整为该特定列。但是,以这种方式执行操作时,我得到的是第一行(id1)而不是id2。

因此,我显然是在以错误的方式进行操作,但是我不确定目前还有什么尝试。

调整索引以使特定列从索引行中移出的正确方法是什么?

这是我所谈论的内容的工作样本

from PyQt5 import QtCore, QtGui, QtWidgets
from collections import deque

test_data = [
    {"column2": "id1", "column1": "id1", "column3": "id1-value", "lvl": 0},
    {"column2": "id2", "column1": "id1.id2", "column3": "id2-value", "lvl": 1},
    {"column2": "id3", "column1": "id1.id2.id3", "column3": "id3-value", "lvl": 2},
]

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(640, 480)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.eventTree = QtWidgets.QTreeView(self.centralwidget)
        self.eventTree.setGeometry(QtCore.QRect(10, 40, 621, 391))
        self.eventTree.setObjectName("eventTree")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(20, 10, 89, 25))
        self.pushButton.setObjectName("pushButton")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(170, 10, 391, 17))
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.model = QtGui.QStandardItemModel()
        self.model.setHorizontalHeaderLabels(["Column1", "Column2", "Column3"])
        self.eventTree.header().setDefaultSectionSize(180)
        self.eventTree.setModel(self.model)
        self.pushButton.clicked.connect(self.clickHandle)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        self.importData(test_data)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))
        self.label.setText(_translate("MainWindow", "TextLabel"))

    def clickHandle(self):
        index = self.eventTree.currentIndex()
        parent_index = index.parent()
        parent_index = self.model.index(parent_index.row(), 1)
        text = parent_index.data()
        self.label.setText(text)

    def importData(self, data, root=None, extend=False):
        self.model.setRowCount(0)
        if root is None:
            root = self.model.invisibleRootItem()

        seen = {}
        values = deque(data)
        while values:
            value = values.popleft()
            if value['lvl'] == 0:
                parent = root
            else:
                pid = ".".join(value['column1'].split(".")[:-1])
                if pid not in seen:
                    values.append(value)
                    continue
                parent = seen[pid]
            dbid = value['column1']
            parent.appendRow([
                QtGui.QStandardItem(value['column1']),
                QtGui.QStandardItem(value['column2']),
                QtGui.QStandardItem(value['column3']),
            ])
            seen[dbid] = parent.child(parent.rowCount() - 1)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:1)

我认为您想要的东西与我正在做的事情类似,如下所示:

tolist

函数RefrshTreeSelctd显示属于父级的所有项目,或者仅显示属于父级子级的项目

希望可以帮助您走上正确的道路。