如何在组合框内创建树视图(带复选框) - PyQt [英] How do I create a tree view (with checkbox) inside a combo box - PyQt

查看:51
本文介绍了如何在组合框内创建树视图(带复选框) - PyQt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PYQT 来开发应用程序.我的要求是在组合框项目中插入一个带有复选框的树视图.我想知道如何实现这一目标?

I'm using PYQT for developing an application. my requirement is to insert a tree view with checkbox inside a combobox items. I would like to know how to achieve this?

我有以下代码,但这不起作用.

I have the following code but this does not work.

class CheckboxInsideListbox(QWidget):
def __init__(self, parent = None):
    super(CheckboxInsideListbox, self).__init__(parent)
    self.setGeometry(250,250,300,300)
    self.MainUI()

def MainUI(self):
    #stb_label = QLabel("Select STB\'s")
    stb_combobox = QComboBox()

    length = 10
    cb_layout = QVBoxLayout(stb_combobox)
    for i in range(length):
        c = QCheckBox("STB %i" % i)
        cb_layout.addWidget(c)

    main_layout = QVBoxLayout()
    main_layout.addWidget(stb_combobox)
    main_layout.addLayout(cb_layout)



    self.setLayout(main_layout)

如果我在这里遗漏了什么,请告诉我.

Please let me know if I'm missing anything here.

推荐答案

您应该创建一个模型,该模型支持 data 和 SetData 方法中的 Qt.CheckStateRole 以及 flags 方法中的标志 Qt.ItemIsUserCheckable.

You should create a model that support Qt.CheckStateRole in data and SetData methods and the flag Qt.ItemIsUserCheckable in the flags method.

我在这里粘贴一个我在项目中使用的示例,这是一个 QSortFilterProxyModel 通用实现,可在任何模型中使用,但您可以在模型实现中使用相同的想法,显然我在您拥有的这个子类中使用内部结构不是直接在 PyQt 中,而是附加到我的内部实现(self.booleanSet 和 self.readOnlySet).

I Paste you here an example i am using in a project, this is a QSortFilterProxyModel generic implementation to use in any model but you can use the same ideas in your model implementation, obviously i am using internal structures in this subclass you have not directly in PyQt and are attached to my internal implementation (self.booleanSet and self.readOnlySet).

def flags(self, index):
    if not index.isValid():
        return Qt.ItemIsEnabled

    if index.column() in self.booleanSet:
        return Qt.ItemIsUserCheckable | Qt.ItemIsSelectable | Qt.ItemIsEnabled
    elif index.column() in self.readOnlySet:
        return Qt.ItemIsSelectable | Qt.ItemIsEnabled
    else:
        return QSortFilterProxyModel.flags(self, index)

def data(self, index, role):
    if not index.isValid():
        return QVariant()

    if index.column() in self.booleanSet and role in (Qt.CheckStateRole, Qt.DisplayRole):
        if role == Qt.CheckStateRole:
            value = QVariant(Qt.Checked) if index.data(Qt.EditRole).toBool() else QVariant(Qt.Unchecked)
            return value
        else: #if role == Qt.DisplayRole:
            return QVariant()
    else:
        return QSortFilterProxyModel.data(self, index, role)

def setData(self, index, data, role):
    if not index.isValid():
        return False

    if index.column() in self.booleanSet and role == Qt.CheckStateRole:
        value = QVariant(True) if data.toInt()[0] == Qt.Checked else QVariant(False)
        return QSortFilterProxyModel.setData(self, index, value, Qt.EditRole)
    else:
        return QSortFilterProxyModel.setData(self, index, data, role)

这篇关于如何在组合框内创建树视图(带复选框) - PyQt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆