PyQt: How to set Combobox Items be Checkable?

前端 未结 3 1625
悲哀的现实
悲哀的现实 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)
    

提交回复
热议问题