简体   繁体   中英

Combo box index value returning to default 0 when clicked radio buttons or change spin boxes in PyQt

I'm trying to connect a combo box with another combo box, radio buttons and spin boxes in PyQt5. The problem is that every time the last combo box is triggered and I click the radio buttons, or another combo box, the last combo box jumps to the default index 0. In my example, the last combo box contains RA, RB and RC. If I click R in the radio button and then select RC from the combo box, then when I change to T, it jumps to TA. What I want is that the script remembers the option C and when I change from R to T in the radio buttons, returns RC or TC correspondingly, without jumping to RA or TA. I couldn't recreate the jump to the default index value in the first combo box, neither the spin boxes but I hope you understand my problem. How can I store the combo box index selected from a combo box and when can I call it to avoid it returning to 0?

I have tried setCurrentIndex but it only works when I open the program. It changes back to the currentIndex every time I select an item in the combo box but not to the selected option.

I have also tried currentIndexChanged but it doesn't seem to work properly. Any ideas?

This is a part of my recreated code:

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()

In your solution you are eliminating and adding items so that by default it will be established in the initial position causing the problem you observe. A possible solution is to store the currentIndex before removing the items and then when the new items are established, set it as currentIndex but a more robust solution is to use models with multiple columns and change the modelColumn depending on the QRadioButton checked.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM