从Pyqt5中的可检查组合框中获取检查文本

时间:2018-06-19 07:31:06

标签: qt checkbox pyqt5 qcombobox

我正在尝试从pyqt5中的复选框创建的自定义组合框中收集已检查项目的文本。

这是我的可行片段,不按照我的期望打印所选文本:

from PyQt5 import QtCore, QtGui, QtWidgets
import sys

class CheckableComboBox(QtWidgets.QComboBox):
    def __init__(self, parent = None):
        super(CheckableComboBox, self).__init__(parent)
        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)

    def checkedItems(self):
        checkedItems = []
        for index in range(self.count()):
            item = self.item(index)
            if item.checkState() == Qt.Checked:
                checkedItems.append(item)
        return checkedItems

class Ui_dialogCreateBatch(object):
    def setupUi(self, dialogCreateBatch):
        dialogCreateBatch.resize(400, 338)
        dialogCreateBatch.setMouseTracking(True)

        self.gridLayoutWidget = QtWidgets.QWidget(dialogCreateBatch)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(10, 10, 360, 115))
        self.gridLayoutWidget.setObjectName("gridLayoutWidget")
        self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setContentsMargins(0, 0, 0, 0)
        self.gridLayout.setObjectName("gridLayout")

        self.cboItemList = CheckableComboBox(self.gridLayoutWidget)
        self.cboItemList.setObjectName("cboItemList")
        self.gridLayout.addWidget(self.cboItemList, 0, 0, 1, 1)

        data = ('item1', 'item2', 'item3')
        for index, element in enumerate(data):
            self.cboItemList.addItem(element)
            item = self.cboItemList.model().item(index, 0)
            item.setCheckState(QtCore.Qt.Unchecked)

        self.buttonBox = QtWidgets.QDialogButtonBox(dialogCreateBatch)
        self.buttonBox.setGeometry(QtCore.QRect(100, 300, 156, 23))
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)

        self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName("buttonBox")

        self.retranslateUi(dialogCreateBatch)

        self.buttonBox.accepted.connect(self.accept)
        self.buttonBox.rejected.connect(dialogCreateBatch.reject)
        QtCore.QMetaObject.connectSlotsByName(dialogCreateBatch)

    def retranslateUi(self, dialogCreateBatch):
        _translate = QtCore.QCoreApplication.translate
        dialogCreateBatch.setWindowTitle(_translate("dialogCreateBatch", "Create Item Batch"))

    def accept(self):
        selectedItems = self.cboItemList.checkedItems()
        print(selectedItems)
        dialogCreateBatch.close

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    dialogCreateBatch = QtWidgets.QDialog()
    ui = Ui_dialogCreateBatch()
    ui.setupUi(dialogCreateBatch)
    dialogCreateBatch.show()
    sys.exit(app.exec_())

在上面的代码中,我添加了用于记录返回数据的打印行。但是我没有收到从这里返回的任何数据

提前致谢

1 个答案:

答案 0 :(得分:0)

就我的情况而言,当我执行您的代码时,我看不到QCheckBox,解决方案是建立QListView view()的{​​{1}}。

我看到的最大错误是,在下面的表达式QComboBox中,您试图访问self.cboMulSup.checkedItems(),但您从未定义过它。

另一方面,不建议修改Qt Designer生成的代码,PyQt我建议创建另一个继承自相应小部件并使用类cboMulSup的类。

Ui_xxx