【问题标题】:How do i select at item in a QTreeView that is inside a QComboBox如何在 QComboBox 内的 QTreeView 中选择项目
【发布时间】:2012-03-29 02:19:18
【问题描述】:

我有一个 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,发布的解决方案对我不起作用。

【问题讨论】:

    标签: python qt pyqt qtreeview qcombobox


    【解决方案1】:

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

    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
    

    【讨论】:

    • 太好了,更适合我。感谢(两者)抽出宝贵时间! (也感谢您的超级笔记)
    【解决方案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

    希望这会有所帮助。

    【讨论】:

    • 是的,这对我有用——我看过那个例子,但没有跳到 QTreeWidget。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多