python - Trying to get a specific column value out of parent row in qtreeview
问题描述
I have a qtreeview that I populate with hierarchical data like this:
- id1 id1
-id1.id2 id2
- id1.id2.id3 id3
In my non sample application I want to be able to right click on the last row and select a de-link menu option that then goes and deletes the linking record between id2 and id3. To do this I need the id of the child and the parent.
The problem enters with this bit of code
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()
So I grab the current index from the view, get the parent index from the child index. Up to there everything works as I would expect. If I right clicked on the second column on id3 text1 will equal id2.
Since I need to grab a specific column of the parent row I try to adjust the parent index to that specific column by using the model.index function. However when I do it this way I get the top row (id1) instead of id2.
So I am apparently going about this the wrong way, but I'm not sure what else to try right now.
What would be the proper way of adjusting an index to get a specific column out of the index's row?
Here is a working sample of what I am talking about
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_())
解决方案
我认为你想要的是类似于我正在做的事情如下:
def __itemSingleClicked(self, index):
ItemIdxs = self.selectedIndexes()[0]
Item = ItemIdxs.model().itemFromIndex(index)
if Item.hasChildren():
Parnt = ItemIdxs.model().itemFromIndex(index).text()
self.CntrPane.RefrshTreeSelctd(Parnt)
else:
Parnt = ItemIdxs.model().itemFromIndex(index).parent().text()
Child = ItemIdxs.model().itemFromIndex(index).text()
self.CntrPane.RefrshTreeSelctd(Parnt, Child)
RefrshTreeSelctd 函数要么显示属于父项的所有项,要么只显示属于父项的子项
希望能帮助你走上正确的道路。
推荐阅读
- vim - 如何阻止 vi 在下一行继续评论?
- python-3.x - 奇怪的 numpy loadtxt 行为:未正确读取第二列
- javascript - 未定义但可以在控制台(Webpack)中使用的函数
- amazon-web-services - AWS Serverless:未创建 SQS Lambda 触发器
- assembly - 如何在 8086 中编码 POP 指令?
- unity3d - 我的一项资产在统一时遇到问题
- php - PHPUnit 测试未执行
- laravel - 使用 Guzzle 发布表单数据总是返回 404
- java - 如何从对象流中统一对象的集合
- javascript - 无法在 fetch api 中使用变量