0

我正在尝试将组合框与 PyQt5 中的另一个组合框、单选按钮和旋转框连接起来。问题是每次触发最后一个组合框并且我单击单选按钮或另一个组合框时,最后一个组合框都会跳转到默认索引 0。在我的示例中,最后一个组合框包含 RA、RB 和 RC。如果我单击单选按钮中的 R,然后从组合框中选择 RC,那么当我更改为 T 时,它会跳转到 TA。我想要的是脚本记住选项 C,并且当我在单选按钮中从 R 更改为 T 时,相应地返回 RC 或 TC,而不跳转到 RA 或 TA。我无法在第一个组合框中重新创建到默认索引值的跳转,旋转框也没有,但我希望你能理解我的问题。

我试过 setCurrentIndex 但它只在我打开程序时才有效。每次我在组合框中选择一个项目而不是所选选项时,它都会变回 currentIndex。

我也试过 currentIndexChanged 但它似乎不能正常工作。有任何想法吗?

这是我重新创建的代码的一部分:

import sys
from functools import partial

from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, \
    QHBoxLayout, QRadioButton, QMainWindow


class ComboWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(ComboWidget, self).__init__(parent)

        self.radio_buttons = {}
        self.combo_boxes = {}
        self.setGeometry(50, 50, 500, 500)

        layout = QtWidgets.QHBoxLayout(self)
        radio_button_names = ['R',
                              'T',
                              ]
        for name in radio_button_names:
            self.radio_buttons[name] = QtWidgets.QRadioButton(name)
            self.radio_buttons[name].toggled.connect(self._radio_button_toggled)
            layout.addWidget(self.radio_buttons[name])

        combo_box_names = ['RT', 'select_plot_type']
        for name in combo_box_names:
            self.combo_boxes[name] = QtWidgets.QComboBox()
            self.combo_boxes[name].currentIndexChanged.connect(partial(self.indexChanged, name))
            layout.addWidget(self.combo_boxes[name])
            self.indexChanged(name, self.combo_boxes[name].currentIndex())
        self.combo_boxes['RT'].addItems(['R', 'T'])
        self.combo_boxes['select_plot_type'].addItems(['R A', 'R B', 'R C'])

        self.show()

    def indexChanged(self, name, combo_box_index):
        print('combo box changed')
        if combo_box_index == -1:
            return
        if name == 'select_plot_type':
            self.combo_boxes['select_plot_type'].itemData(combo_box_index)

        if name == 'RT':
            self.combo_boxes['RT'].itemData(combo_box_index)

    def _radio_button_toggled(self):
        self.combo_boxes['RT'].clear()
        self.combo_boxes['select_plot_type'].clear()
        if self.radio_buttons['R'].isChecked()==True:
            self.combo_boxes['RT'].addItems(['R1', 'R2', 'R3'])
            self.combo_boxes['select_plot_type'].addItems(['R A', 'R B', 'R C'])
        elif self.radio_buttons['T'].isChecked()==True:
            self.combo_boxes['RT'].addItems(['T1', 'T2', 'T3'])
            self.combo_boxes['select_plot_type'].addItems(['T A', 'T B', 'T C'])
        else:
            return


def main():
    app = QtWidgets.QApplication(sys.argv)
    w = ComboWidget()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
4

1 回答 1

0

在您的解决方案中,您正在消除和添加项目,以便默认情况下它将建立在导致您观察到的问题的初始位置。一种可能的解决方案是在删除项目之前存储 currentIndex,然后在建立新项目时将其设置为 currentIndex,但更强大的解决方案是使用具有多列的模型并根据选中的 QRadioButton 更改 modelColumn。

import sys

from PyQt5 import QtGui, QtWidgets


class ComboWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(ComboWidget, self).__init__(parent)

        options = ["R", "T"]

        self.setGeometry(50, 50, 500, 500)
        layout = QtWidgets.QHBoxLayout(self)

        group = QtWidgets.QButtonGroup(self)

        for i, option in enumerate(options):
            radiobutton = QtWidgets.QRadioButton(option)
            group.addButton(radiobutton, i)
            layout.addWidget(radiobutton)
            if i == 0:
                radiobutton.setChecked(True)

        for values in (("{}1", "{}2", "{}3"), ("{} A", "{} B", "{} C")):
            combobox = QtWidgets.QComboBox()
            self.fill_model(combobox, values, options)
            group.buttonClicked[int].connect(combobox.setModelColumn)
            combobox.setModelColumn(group.checkedId())
            layout.addWidget(combobox)

    def fill_model(self, combobox, values, options):
        model = QtGui.QStandardItemModel(self)
        for i, text in enumerate(values):
            for j, option in enumerate(options):
                it = QtGui.QStandardItem(text.format(option))
                model.setItem(i, j, it)
        combobox.setModel(model)


def main():
    app = QtWidgets.QApplication(sys.argv)
    w = ComboWidget()
    w.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()
于 2020-01-13T16:21:58.470 回答