信号获取多个Qcomboxs的多个内容作为函数中的多个参数

Signal getting multiple contents of several Qcomboxs as multiple parameters in function

我有 3 个 QCombobox 具有不同的内容想要获取所有 3 个 Qcombobox 的当前索引并将它们作为多个参数仅发送给一个函数。我不知道在 PyQt5?

中是否可行
    # Creating multiple comboxs
    self.combo_1 = QtWidgets.QComboBox()
    self.combo_1.addItems(['a', 'b', 'c', 'd', 'e'])
    self.combo_2 = QtWidgets.QComboBox()
    self.combo_2.addItems([1, 2, 3, 4, 5])
    self.combo_3 = QtWidgets.QComboBox()
    self.combo_3.addItems(['I', 'II', 'III', 'IV', 'V'])

    ----------------------------------
    # Creating singal and slots
    self.combo_1.activated[int].connect(self.getindex)
    self.combo_2.activated[int].connect(self.getindex)
    self.combo_3.activated[int].connect(self.getindex)
    self.combo_1.activated.emit(self.combo_1.currentIndex())
    self.combo_2.activated.emit(self.combo_2.currentIndex())
    self.combo_3.activated.emit(self.combo_3.currentIndex())

    ----------------------------------
    # forward it to function
    @QtCore.pyqtSlot(int)
    def getindex(self, index1, index2, index3 ):
        print('{} index from combo_1'.format(index1))
        print('{} index from combo_2'.format(index2))
        print('{} index from combo_3'.format(index3))

我怎样才能实现这种行为?谢谢

QComboBox 是 class 的成员,因此您可以使用 currentIndex() 方法获取索引:

    self.combo_1.activated.connect(self.getindex)
    self.combo_2.activated.connect(self.getindex)
    self.combo_3.activated.connect(self.getindex)
    self.getIndex()

@QtCore.pyqtSlot()
def getindex(self):
    index1 = self.combo_1.currentIndex()
    index2 = self.combo_2.currentIndex()
    index3 = self.combo_2.currentIndex()
    print('{} index from combo_1'.format(index1))
    print('{} index from combo_2'.format(index2))
    print('{} index from combo_3'.format(index3))

您确定要使用 activated 而不是 currentIndexChanged 吗? 如果您只想激活一个功能,那么 eyllanesc 的解决方案很棒。但是如果您想在 Widget class 之外使用它,我建议创建自定义信号和方法来计算它的值。

以下是我将如何以通用方式解决它。

from typing import List
from PyQt5.QtWidgets import QWidget, QComboBox, QHBoxLayout, QApplication
from PyQt5.QtCore import pyqtSignal, pyqtSlot


class ComboBoxGroup(QWidget):
    indexes_changed = pyqtSignal(list)

    def __init__(self, initial_values: List[List[str]]):
        super().__init__()
        layout = QHBoxLayout()
        self.combos = []
        for values in initial_values:
            combo_box = QComboBox()
            combo_box.addItems(values)
            combo_box.currentIndexChanged.connect(self.value_changed)
            layout.addWidget(combo_box)
            self.combos.append(combo_box)
        self.setLayout(layout)


    def value_changed(self):
        res = [x.currentIndex() for x in self.combos]
        self.indexes_changed.emit(res)


def test(values: List[int]):
    print("current indexes: " + ",".join(map(str, values)))

if __name__ == "__main__":
    app = QApplication([])
    group = ComboBoxGroup([['a', 'b', 'c', 'd', 'e'], ['1', '2', '3', '4', '5'], ['I', 'II', 'III', 'IV', 'V']])
    group.indexes_changed.connect(test)
    group.show()
    app.exec_()