正如标题所述,我正在尝试从qtreeview中当前选定行的父行的特定列中提取一个值。
我尝试过这种方式:
index = self.view.currentIndex()
parent_index = index.parent()
parent_col1_index = self.model.index(parent_index.row(), 1)
text = self.model.data(parent_col1_index)
在这样的数据结构上,已选择id3:
- id1 id1
- id1.id2 id2
- id1.id2.id3 id3
当我使用parent_index变量从模型中提取数据时,我得到了从父行(id2)单击的任何列。但是,当我尝试使用parent_col1_index变量从该父项获取特定列时,我从顶部行(id1)获得了正确的列。即使使用更大的数据集,这也是一致的。如果我尝试从2行深的任何行的父行中获取一列,则会得到返回的treeview的第一行。
我认为这与如何使用model.index创建列特定索引有关。但是我不确定还能怎么做。
有什么想法吗?
如果有帮助,这里是一个有效的示例。单击按钮时,如果选择了id3行,则应该输出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_())
答案 0 :(得分:0)
糟糕,想通了。仅仅是因为我不应该调用model.index方法来为父级中的特定列创建索引。我需要的是调用index.sibling方法。像这样:
parent = index.parent()
parent_col1_index = parent.sibling(parent.row(), 1)