PyQt: How to set Combobox Items be Checkable?

前端 未结 3 1613
悲哀的现实
悲哀的现实 2020-12-01 17:19

To keep the GUI widgets number to minimum I need to find a way to give to user a choice of pull-down menu items that could be used to filter out the displayed in a listWidge

相关标签:
3条回答
  • 2020-12-01 18:01

    (Not an answere to the question, hence I used most of the code from here)

    I added a function and changed the name to RadioComboBox if anyone else wants to have a class for a RadioComboBox.

    from PyQt4 import QtCore
    from PyQt4.QtGui import QComboBox, QStandardItemModel
    
    
    class RadioComboBox(QComboBox):
        def __init__(self):
            super(RadioComboBox, self).__init__()
            self.view().pressed.connect(self.handle_item_pressed)
            self.setModel(QStandardItemModel(self))
    
        def handle_item_pressed(self, index):
            item = self.model().itemFromIndex(index)
            target_row = item.index().row()
            if item.checkState() != QtCore.Qt.Checked:
                item.setCheckState(QtCore.Qt.Checked)
            self.check_others(target_row)
    
        def check_others(self, target_row):
            for i in range(self.model().rowCount()):
                if i == target_row:
                    continue
                else:
                    item = self.model().item(i)
                    item.setCheckState(QtCore.Qt.Unchecked)
    
    0 讨论(0)
  • 2020-12-01 18:06

    This idea of a multi-select combo has come up before, but I'm not sure that its the best solution. Really, all that's needed is a tool-button with a drop-down menu (similar to the history buttons in a web-browser).

    Here's an update of the example that illustrates both options:

    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_())
    
    0 讨论(0)
  • 2020-12-01 18:11

    It is very easy. Just set the item Checkable using the flags() function in the model associated with the comboBox.

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

    UPDATE


    This may not be the best method, But it should sort your problem. Here is a something u may want look at , IF you have space restrictionn and cannot display the complete listView, Just resize it until it looks like a ComboBox.

    enter image description here

    0 讨论(0)
提交回复
热议问题