Have dynamic combo box based on selections from another combo box
-
wrote on 16 Apr 2018, 22:28 last edited by
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.
-
wrote on 16 Apr 2018, 23:27 last edited by AmrCoder
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
-
wrote on 17 Apr 2018, 01:11 last edited by
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.
-
wrote on 17 Apr 2018, 01:20 last edited by
I tried your method. Works perfectly. Seems a little quicker than the connect method too. Thanks again.
-
wrote on 18 Apr 2018, 18:33 last edited by
@te777 if your issue is solved, please don't forget to mark your post as such. Thanks.
1/5