PyQt:如何将组合框项目设置为可检查?

PyQt:如何将组合框项目设置为可检查?,第1张

PyQt:如何将组合框项目设置为可检查?

关于多选组合的想法是在之前提出的,但是我不确定它是否是最佳解决方案。确实,所需要的只是一个带有下拉菜单的工具按钮(类似于网络浏览器中的历史记录按钮)。

这是示例的更新,说明了两个选项:

from PyQt4 import QtGui, QtCoreimport sys, osclass 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_())


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5639596.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存