编程语言
首页 > 编程语言> > python – PyQt:如何设置Combobox项目是否可检查?

python – PyQt:如何设置Combobox项目是否可检查?

作者:互联网

为了使GUI小部件数量保持最小,我需要找到一种方法来为用户提供可用于过滤掉listWidget项目中显示的下拉菜单项.
假设listWidget列出了5种不同类别的物品:“Cat A”,“Cat B”,“Cat C”,“Cat D”,“Cat E”.我可以为每个项目类别实现广播或复选框.但是5个单选按钮或复选框会占用大量的GUI空间.具有可检查项目的组合框似乎是正确的选择.有任何想法吗?

from PyQt4 import QtGui, QtCore
import sys, os


class CheckableComboBox(QtGui.QComboBox):
    def __init__(self):    
        super(CheckableComboBox, self).__init__()

    def flags(self, index):
        return Qt.ItemIsUserCheckable | Qt.ItemIsSelectable | Qt.ItemIsEnabled


class Dialog_01(QtGui.QMainWindow):
    def __init__(self):
        super(QtGui.QMainWindow,self).__init__()

        myQWidget = QtGui.QWidget()
        myBoxLayout = QtGui.QVBoxLayout()
        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)

        self.ComboBox = CheckableComboBox()
        for i in range(3):
            self.ComboBox.addItem("Combobox Item " + str(i))

        myBoxLayout.addWidget(self.ComboBox)


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    dialog_1 = Dialog_01()
    dialog_1.show()
    dialog_1.resize(480,320)
    sys.exit(app.exec_())

解决方法:

这个多选组合的想法有come up before,但我不确定它是最好的解决方案.真的,所需要的只是一个带有下拉菜单的工具按钮(类似于网络浏览器中的历史按钮).

以下是说明这两个选项的示例的更新:

from PyQt4 import QtGui, QtCore
import sys, os

class CheckableComboBox(QtGui.QComboBox):
    def __init__(self):
        super(CheckableComboBox, self).__init__()
        self.view().pressed.connect(self.handleItemPressed)
        self.setModel(QtGui.QStandardItemModel(self))

    def handleItemPressed(self, index):
        item = self.model().itemFromIndex(index)
        if item.checkState() == QtCore.Qt.Checked:
            item.setCheckState(QtCore.Qt.Unchecked)
        else:
            item.setCheckState(QtCore.Qt.Checked)

class Dialog_01(QtGui.QMainWindow):
    def __init__(self):
        super(QtGui.QMainWindow,self).__init__()
        myQWidget = QtGui.QWidget()
        myBoxLayout = QtGui.QVBoxLayout()
        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)
        self.ComboBox = CheckableComboBox()
        for i in range(3):
            self.ComboBox.addItem("Combobox Item " + str(i))
            item = self.ComboBox.model().item(i, 0)
            item.setCheckState(QtCore.Qt.Unchecked)
        self.toolbutton = QtGui.QToolButton(self)
        self.toolbutton.setText('Select Categories ')
        self.toolmenu = QtGui.QMenu(self)
        for i in range(3):
            action = self.toolmenu.addAction("Category " + str(i))
            action.setCheckable(True)
        self.toolbutton.setMenu(self.toolmenu)
        self.toolbutton.setPopupMode(QtGui.QToolButton.InstantPopup)
        myBoxLayout.addWidget(self.toolbutton)
        myBoxLayout.addWidget(self.ComboBox)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    dialog_1 = Dialog_01()
    dialog_1.show()
    dialog_1.resize(480,320)
    sys.exit(app.exec_())

标签:qcombobox,python,qt,pyqt
来源: https://codeday.me/bug/20190926/1819164.html