如何选择QComboBox内的QTreeView中的项目

时间:2012-03-12 16:45:21

标签: python qt pyqt qtreeview qcombobox

我有一个QtreeView作为QComboBox中的视图。在我的应用程序中,根项目是类别标签,不能被选中。当我创建视图时,我想预先选择其中一个子项(默认选择第一个根项),但我无法弄清楚如何。这方面的例子(特别是对于python)在地面上很薄。

这是我的简化示例:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

data = [ (("Cat A",False), [(("Thing 1",True), []),(("Thing 2",True), [])]),
    (("Cat B",False), [(("Thing 3",True), []), (("Thing 4",True), [])])]

class MyComboBox(QComboBox):
    def __init__(self):
        super(QComboBox,self).__init__()
        self.setView(QTreeView())

        self.view().setHeaderHidden(True)
        self.view().setItemsExpandable(False)
        self.view().setRootIsDecorated(False)

    def showPopup(self):
        self.view().expandAll()
        QComboBox.showPopup(self)

class Window(QWidget):
    def __init__(self):

        QWidget.__init__(self)

        self.model = QStandardItemModel()
        self.addItems(self.model, data)

        self.combo = MyComboBox()
        self.combo.setModel(self.model)

        layout = QVBoxLayout()
        layout.addWidget(self.combo)
        self.setLayout(layout)

        # I can choose which combobox item to select here, but I am unable to
        #choose child items
        #self.combo.setCurrentIndex(1)

    def addItems(self, parent, elements):
        for text, children in elements:
            item = QStandardItem(text[0])
            # root items are not selectable, users pick from child items
            item.setSelectable(text[1])
            parent.appendRow(item)
            if children:
                self.addItems(item, children)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

我使用了示例herehere

问题几乎被问到before,但不是python,并且发布的解决方案对我不起作用。

2 个答案:

答案 0 :(得分:2)

如果您在组合框上使用QTreeWidget作为视图和模型,那么这将起作用。

self.tree = QTreeWidget()
self.combo.setModel(self.tree.model())
self.combo.setView(self.tree)

此外,您需要更改addItems()函数以使用QTreeWidgetItem作为子项构造QTreeWidget。完成此操作后,以下内容将在您的treewidget中选择一个项目:

# make item current in tree to get hold of its index
self.tree.setCurrentItem(ITEMTOSELECT)
# make item's parent reference point and provide index in relation to parent
self.combo.setRootModelIndex(self.tree.currentIndex().parent())
self.combo.setCurrentIndex(self.tree.currentIndex().row())
# reset combobox to display full tree again
self.tree.setCurrentItem(self.tree.invisibleRootItem())
self.combo.setRootModelIndex(self.tree.currentIndex()) 

这是基于here找到的示例。

希望这有帮助。

答案 1 :(得分:2)

这是您当前代码的另一种更通用的方法。它适用于其他级别的嵌套项和任何可选项的配置。

class MyComboBox(QComboBox):
    def __init__(self):
        super(MyComboBox,self).__init__()  # your super was wrong.
                                           # you need to pass the _current_ class name
        self.setView(QTreeView())

        self.view().setHeaderHidden(True)
        self.view().setItemsExpandable(False)
        self.view().setRootIsDecorated(False)

    def showPopup(self):
        self.setRootModelIndex(QModelIndex()) # you need to add this
        self.view().expandAll()
        QComboBox.showPopup(self)

    def setModel(self, model):
        super(MyComboBox, self).setModel(model)
        parent, row = self._firstSelectableItem()
        if row is not None:
            self.setRootModelIndex(parent)
            self.setCurrentIndex(row)

    def _firstSelectableItem(self, parent=QModelIndex()):
        """
        Internal recursive function for finding the first selectable item.
        """
        for i in range(self.model().rowCount(parent)):
            itemIndex = self.model().index(i,0,parent)
            if self.model().itemFromIndex(itemIndex).isSelectable():
                return parent, i
            else:
                itemIndex, row = self._firstSelectableItem(itemIndex)
                if row is not None:
                    return itemIndex, row
        return parent, None