Have dynamic combo box based on selections from another combo box



  • My application currently has 2 comboboxes on the main form. The first combobox has 14 items, but 5 categories. I would like to list the categories in an additional combobox that I would place on the form above the 14 item combobox. Then populate dynamically the original 14 item combobox (doing a clear()) first) with items based on the category combox selection. I think I've found how to clear and add items, but I don't know how to connect the signal. Any help would be greatly appreciated. Thanks.



  • as i understand the question, you can use the currentTextChanged signal in the first combobox, or by index using currentIndexChanged signal and check the changed item and depend on that value you can update the second combobox on that item in the first combobox like this for example, using by text change which return a string of the current value in the first combobox

    void MainWindow::on_comboBox_currentTextChanged(const QString &arg1)
    {
        if (arg1 == "first") {
            ui->comboBox_2->clear();
            ui->comboBox_2->addItem("1");
        }
        else if (arg1 == "second") {
            ui->comboBox_2->clear();
            ui->comboBox_2->addItem("2");
        }
        else if (arg1 == "third") {
            ui->comboBox_2->clear();
            ui->comboBox_2->addItem("3");
        }
    
    }
    

    now whenever you change a value in the first comobox you will update the second one



  • Thanks. I already got it working using a connect slot. But I'll try your method and see if it works. Thanks again for your help.



  • I tried your method. Works perfectly. Seems a little quicker than the connect method too. Thanks again.



  • @te777 if your issue is solved, please don't forget to mark your post as such. Thanks.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.