简体   繁体   English

QTreeView中的QComboBox?

[英]QComboBox in QTreeView?

I'm trying to create a QTreeView that should hold a QComboBox on each row in a certain column so that I can choose the data for a cell from a list of strings. 我试图创建一个QTreeView,它应该在某个列的每一行上保存一个QComboBox,以便我可以从字符串列表中选择单元格的数据。 What I would like to do is something like 我想做的是

item = QtGui.QStandardItem(QtGui.QComboBox()) item = QtGui.QStandardItem(QtGui.QComboBox())

but that is obviously not possible. 但这显然是不可能的。

This is fairly easy to do in GTK+ toolkit, so I guess it should be possible in Qt4 too. 在GTK +工具箱中,这相当容易做到,因此我想在Qt4中也应该有可能。 If it is not (easily) possible, what would be an alternative? 如果不可能(很容易),有什么替代方案?

Presently, I have no code to present. 目前,我没有代码可以呈现。 Could someone give a hint on which direction to go? 有人可以提示要走的方向吗? I'm writing the code in python. 我正在用python编写代码。

Apparently delegating the editor role is the way to go. 显然,委派编辑角色是可行的方法。 After cutting and pasting and editing examples I've found, I have got something working: 在剪切,粘贴和编辑我发现的示例之后,我有了一些工作:

import sys
from PyQt4 import QtGui, QtCore

class MyModel(QtGui.QStandardItemModel):
    def __init__(self):
        super(QtGui.QStandardItemModel, self).__init__()
        self.setColumnCount(3)
        self.setHorizontalHeaderLabels(['Col 1', 'Col2 2', 'Col 3'])

    def setData(self, index, value, role=QtCore.Qt.DisplayRole):
        item = self.itemFromIndex(index)
        item.setData(value, role=QtCore.Qt.DisplayRole)


class ComboDelegate(QtGui.QItemDelegate):
    def __init__(self, parent):
        QtGui.QItemDelegate.__init__(self, parent)

    def createEditor(self, parent, option, index):
        combo = QtGui.QComboBox(parent)
        combo.addItem('a')
        combo.addItem('b')
        combo.addItem('c')
        combo.addItem('t')
        combo.addItem('w')
        return combo

    def setEditorData(self, editor, index):
        text = index.data().toString()
        index = editor.findText(text)
        editor.setCurrentIndex(index)

    def setModelData(self, editor, model, index):
        model.setData(index, editor.itemText(editor.currentIndex()))

    def updateEditorGeometry(self, editor, option, index):
        print option, option.rect
        editor.setGeometry(option.rect)


def row_clicked(model_index):
    row = model_index.row()
    print model_index.data(0).toString()


if __name__ == '__main__':
    myapp = QtGui.QApplication(sys.argv)
    model = MyModel()
    view = QtGui.QTreeView()
    view.setUniformRowHeights(True)
    view.setModel(model)
    view.setItemDelegateForColumn(1, ComboDelegate(view))
    view.show()
    view.pressed.connect(row_clicked)

    for n in [['q','w','e'],['r','t','y']]:
        item_a = QtGui.QStandardItem(unicode(n[0]))
        item_b = QtGui.QStandardItem(unicode(n[1]))
        item_c = QtGui.QStandardItem(unicode(n[2]))
        model.appendRow([item_a, item_b, item_c])

    for row in range(0, model.rowCount()):
        view.openPersistentEditor(model.index(row, 1))

    myapp.exec_()

Now, the problem is that when I'm using the combo boxes to change the data of the model items, the row heights of the view rows do not match the heights of the combos, until I resize the window. 现在的问题是,当我使用组合框更改模型项的数据时,在我调整窗口大小之前,视图行的行高与组合框的高度不匹配。 What's wrong? 怎么了?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM