在QTreeView的子项上设置小部件 [英] To set widgets on children items on QTreeView
问题描述
由于此线程,我能够将窗口小部件添加到QAbstractItemView
(在我的示例中, QTreeView ) view
.
Thanks to this thread, I'm able to add widgets to 2nd or later column of QAbstractItemView
(in my example QTreeView) of top level items of a view
.
但是可以将小部件添加到子项吗?
But is it possible to add widgets to the children items?
这是我尝试过的部分成功的方法:
Here's what I've tried which partly went well:
#!/usr/bin/env python
import os
from PyQt4.QtCore import QModelIndex, Qt
from PyQt4.QtGui import QApplication, QItemSelectionModel, \
QPushButton, QStandardItem, \
QStandardItemModel, QTreeView
from PyQt4.uic import loadUi
class PrvTreeviewNest(QTreeView):
def __init__(self):
super(PrvTreeviewNest, self).__init__()
loadUi('/home/user/yourproject/resource/treeview_nest.ui')
# row can be 0 even when it's more than 0.
self._datamodel = QStandardItemModel(0, 2)
self.setModel(self._datamodel)
for i in range(4):
self._add_widget(i + 1)
self.show()
def _add_widget(self, n):
std_item = QStandardItem('{}th item'.format(n))
self._datamodel.setItem(n, 0, std_item)
node_widget = QPushButton('{}th button'.format(n))
qindex_widget = self._datamodel.index(n, 1, QModelIndex())
self.setIndexWidget(qindex_widget, node_widget)
if n == 2:
std_item_child = QStandardItem('child')
std_item.appendRow(std_item_child)
node_widget_child = QPushButton('petit button')
qindex_widget_child = self._datamodel.index(n, 1, QModelIndex())
self.setIndexWidget(qindex_widget_child, node_widget_child)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
# window = TreeviewWidgetSelectProve()
window = PrvTreeviewNest()
# window = TreeviewWidgetSelectProve()
window.resize(320, 240)
# window.show();
window.setWindowTitle(
QApplication.translate("toplevel", "Top-level widget"))
# window.add_cols()
sys.exit(app.exec_())
treeview_nest.ui 可用.
在下面的图像中您看到,项目child
没有显示按钮,并且其父项的按钮被覆盖.显然我不知道如何为它编写代码.
You see in the image below that the item child
doesn't show a button and its parent's button is overwritten. Apparently I have no idea how to write a code for it.
更新),我了解了如何向孩子添加小部件.足够棘手的是,将 QStandardItem.insertRow 与索引结合使用工作.在上面的示例代码中,将_add_widget
替换为以下内容:
Update) I figured out how to add widget to children. Tricky enough, using QStandardItem.insertRow combined with index does the work. In my sample code above, replace _add_widget
with the following:
def _add_widget(self, n):
item_toplevel = QStandardItem('{}th item'.format(n))
self._datamodel.setItem(n, 0, item_toplevel)
widget_toplevel = QPushButton('{}th button'.format(n))
qindex_toplevel = self._datamodel.index(n, 1, QModelIndex())
self.setIndexWidget(qindex_toplevel, widget_toplevel)
if n == 2:
item_child_col0 = QStandardItem('child col0')
item_child_col1 = QStandardItem('child col1')
#item_toplevel.appendRow(item_child_col0)
item_toplevel.insertRow(0, [item_child_col0, item_child_col1])
widget_child = QPushButton('child widget')
qindex_child = item_child_col1.index()
self.setIndexWidget(qindex_child, widget_child)
我确定这不是最佳/理想的设计方法,但似乎对我有用.在受到@Phlucious的回答启发后,我想到了这一点.谢谢!
I'm sure this is NOT the best/ideal designed way but seems to work for me. I came up with after inspired by @Phlucious's answer. Thanks!
推荐答案
您的错误在此行上:
qindex_widget_child = self._datamodel.index(n, 1, QModelIndex())
self.setIndexWidget(qindex_widget_child, node_widget_child)
它为您提供了模型的第2行第1列 上的索引,它是第二项",而不是您的孩子.您的孩子在std_item
的第1行第1列中.在Qt中,尤其是在QStandardItemModel中,子级是相对于其父级而不是相对于模型存储的.
It's giving you the index on row 2 column 1 of the model, which is the "2th item", not your child. Your child is on row 1 column 1 of std_item
. In Qt, especially in QStandardItemModel, Children are stored relative to their parent, not relative to the model.
我对PyQt不够熟悉,无法为您提供确切的代码,但是您应该可以执行以下操作:
I'm not familiar enough with PyQt to give you the exact code, but you should be able to do something like this:
qindex_widget_child = std_item_child.index(QModelIndex())
self.setIndexWidget(qindex_widget_child, node_widget_child)
或类似这样:
qindex_widget_child = std_item.child(0, 1).index(QModelIndex())
self.setIndexWidget(qindex_widget_child, node_widget_child)
这篇关于在QTreeView的子项上设置小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!