如何使 QComboBox 组互斥?

How to make a QComboBox group mutually exclusive?

一旦在组合中选择了一个项目,它应该从其他项目中删除,除非它是“无用”。

我做了三个QComboBox的选项,每一个都包含相同的项目。

解释是这样的:

那些Qcombobox的默认值是'No Use'。

如何从 QComboBox Tug 2 中删除 QComboBox Tug 1 的选定值?

重点是'No Use'不应该被删除;只有“207HR”、“306DR”和 'Jupiter'.

中的一项

我制作的代码如下:

class Ship_Use_Tug_Input_Program(QWidget):
    def __init__(self, master):
        super().__init__()
        self.initUI()

    def initUI(self):
        tug1_cb = QComboBox(self)
        jeju_tug = ['No use','207HR (2,500HP)', '306DR (3,600HP)', 'Jupiter (3,600HP)']
        tug1_cb.addItems(jeju_tug)

        tug2_cb = QComboBox(self)
        tug2_cb.addItems(jeju_tug)

        tug3_cb = QComboBox(self)
        tug3_cb.addItems(jeju_tug)

        self.setGeometry(100,100,1000,500) 
        self.setWindowTitle('Ship_Use_Tug_Input_Program')
        self.show()
app = QApplication(sys.argv)
exc = Ship_Use_Tug_Input_Program(master=Ship_Use_Tug_Input_Program)
app.exec_()

说明图如下:

只要当前项目之一发生变化,view-widget of the combo-box can be used to hide the rows, and the item-data can be used to keep track of which combo-box is showing which row. A slot connected to the activated signal 就可以更新这些项目。

下面是实现它的完整演示脚本。 ExclusiveComboGroup class 可用于任何组合框组。要使用它,只需创建一个实例,然后使用其 addCombo 方法添加所有组合框。

演示脚本:

import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class ExclusiveComboGroup(QObject):
    def __init__(self, parent=None):
        super().__init__(parent)
        self._combos = []
        self._role = Qt.UserRole + 500

    def addCombo(self, combo):
        combo.activated.connect(
            lambda: self.handleActivated(combo))
        self._combos.append(combo)

    def handleActivated(self, target):
        index = target.currentIndex()
        groupid = id(target)
        for combo in self._combos:
            if combo is target:
                continue
            previous = combo.findData(groupid, self._role)
            if previous >= 0:
                combo.view().setRowHidden(previous, False)
                combo.setItemData(previous, None, self._role)
            if index > 0:
                combo.setItemData(index, groupid, self._role)
                combo.view().setRowHidden(index, True)

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.group = QGroupBox('Selected Tug')
        layout = QVBoxLayout(self)
        layout.addWidget(self.group)
        layout = QFormLayout(self.group)
        layout.setVerticalSpacing(15)
        layout.setHorizontalSpacing(50)
        jeju_tug = [
            'No use',
            '207HR (2,500HP)',
            '306DR (3,600HP)',
            'Jupiter (3,600HP)',
            ]
        # create a combo-group
        self.tugs = ExclusiveComboGroup(self)
        for index in range(3):
            combo = QComboBox(self)
            combo.addItems(jeju_tug)
            layout.addRow(f'Tug {index + 1}', combo)
            # add the combo-box
            self.tugs.addCombo(combo)

if __name__ == "__main__":

    app = QApplication(sys.argv)
    window = Window()
    window.setWindowTitle('Demo')
    window.setGeometry(800, 200, 100, 50)
    window.show()
    sys.exit(app.exec_())

PS:以下是如何在您自己的示例中使用它:

def initUI(self):
    tug1_cb = QComboBox(self)
    jeju_tug = ['No use','207HR (2,500HP)', '306DR (3,600HP)', 'Jupiter (3,600HP)']
    tug1_cb.addItems(jeju_tug)

    tug2_cb = QComboBox(self)
    tug2_cb.addItems(jeju_tug)

    tug3_cb = QComboBox(self)
    tug3_cb.addItems(jeju_tug)
    
    # copy the ExclusiveComboGroup class into
    # your code and then add this section
    tugs = ExclusiveComboGroup(self)
    tugs.addCombo(tug1_cb)
    tugs.addCombo(tug2_cb)
    tugs.addCombo(tug3_cb)