简体   繁体   中英

Changing multiple depending comboboxes with Python through pyqt

I am trying to add three depending comboboxes in Qt with python. That means that you can change one combobox which changes the list of items in the secound combobox. There you can select another item which changes the selection in the third combobox. Therefore i use qtpy as connection between both.

What is really important: The connection of all three comboboxes!

It worked all fine until you change the first combobox for a second time (It worked all fine while changing the secound & third combobox all the time)

My goal is to change the values in all three comboboxes depending on the first combobox (and secound) as often as i want to.

Here is my code:

    self.ui.type1CB.currentTextChanged.connect(self.type1_changed)
    self.ui.type2CB.currentTextChanged.connect(self.type2_changed)


def type1_changed(self):
    self.ui.type2CB.clear()

    type1 = self.ui.type1CB.currentText()
    rev = ["Ge", "Er"]
    cos = ["Au", "Fr", "pe", "So", "Wo"]

    if type1 == "Ei":
        self.ui.type2CB.addItems(rev)
    elif type1 == "Au":
        self.ui.type2CB.addItems(cos)
    else:
        self.ui.type2CB.addItems(" ")

def type2_changed(self):
    self.ui.type3CB.clear()

    type2 = self.ui.type2CB.currentText()
    sa = ["Ge", "Li", "To"]
    re = ["Pf", "Ne", "Ve"]
    ca = ["Be", "Re", "Ve"]
    le = ["Au", "Be", "Ur"]
    pr = ["Le", "Ge", "Sü", "Kl", "Hy", "Ge", "Ve"]
    ot = ["An", "Ar", "Fa", "Ba", "Fe"]
    ho = ["Ha", "Ne", "Te", "Mi"]

    if type2 == "Ge":
        self.ui.type3CB.addItems(sa)
    elif type2 == "Er":
        self.ui.type3CB.addItems(re)
    elif type2 == "Au":
        self.ui.type3CB.addItems(ca)
    elif type2 == "Fr":
        self.ui.type3CB.addItems(le)
    elif type2 == "pe":
        self.ui.type3CB.addItems(pr)
    elif type2 == "So":
        self.ui.type3CB.addItems(ot)
    elif type2 == "Wo":
        self.ui.type3CB.addItems(ho)
    else:
        self.ui.type3CB.addItems(" ")

error is in this line:

    else:
        self.dlg.type3CB.addItems(" ")

if you are using addItems you need to provide list, and you are providing string

you can do it in two ways:

1. use addItem

addItem(const QString &text, const QVariant &userData = QVariant())

    self.dlg.type3CB.addItem(" ")

2. use addItems

addItems(const QStringList &texts)

    self.dlg.type3CB.addItems([" "])

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