Update combobox data from model
-
wrote on 19 Oct 2017, 14:33 last edited by
I have a categories QComboBox which is linked to a QSqlTableModel. Also I have a second window to manage categories (add/remove). How to update/refresh/reload the combobox whenever a category is added/removed?
My code:
int catIndex = model2->fieldIndex("category"); model2->setRelation(catIndex, QSqlRelation("categories", "id", "name")); QSqlTableModel *catModel = model2->relationModel(catIndex); ui->comboBox_2->setModel(catModel); ui->comboBox_2->setModelColumn(catModel->fieldIndex("name")); mapper->addMapping(ui->comboBox_2, catIndex);
-
I have a categories QComboBox which is linked to a QSqlTableModel. Also I have a second window to manage categories (add/remove). How to update/refresh/reload the combobox whenever a category is added/removed?
My code:
int catIndex = model2->fieldIndex("category"); model2->setRelation(catIndex, QSqlRelation("categories", "id", "name")); QSqlTableModel *catModel = model2->relationModel(catIndex); ui->comboBox_2->setModel(catModel); ui->comboBox_2->setModelColumn(catModel->fieldIndex("name")); mapper->addMapping(ui->comboBox_2, catIndex);
-
Hi,
How are you adding / removing these categories ?
-
@m-sue
I can call the main window from the categories window. Problem is I don't know the code to reload the QComboBox.@SGaist
It's another window which has a QTableView with a QSqlTableModel linked to the categories table.wrote on 20 Oct 2017, 05:51 last edited by m.sueHi @rudag
I do not understand. You wrote the code in your first post. Just call it again. Then the changed model should refill the combobox.
If you want the model to update your combobox, though, you will probably have to derive your own model class and override the functions for update and remove of data sets.
-Michael.
-
wrote on 21 Oct 2017, 01:40 last edited by
@m-sue
I'm getting the following error when I try to call that piece of code more than once:
QComboBox::setModel: cannot set a 0 model -
Then why not share the model between the widgets ?
-
wrote on 24 Oct 2017, 00:46 last edited by
I did this:
ui->comboBox->clear(); ((QSqlTableModel*)ui->comboBox->model())->select();
And it's working!
4/8