PDA

View Full Version : Have dynamic combobox based on changes in another combobox



te777
17th April 2018, 00:26
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.

te777
17th April 2018, 03:38
I got it working using a connect slot and a better alternative method too. No need to reply.


Added after 24 minutes:

The connect slot solution is:


connect(ui->comboBox, static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::currentIndexChanged), this, &MainWindow::setGames);

An alternative and easier solution is:


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");
}

}