[Solved] Mapping a column from a table to a QComboBox
-
Hi!
I have a table in a database and I want a column's content be shown in a QComboBox.
I've seen examples using a QDataWidgetMapper for this but there is another table.
I've thought creating another table and create a relation with a foreign key and then to follow the strategy in sql examples for mapping
the column to QComboBox.
Is there a way for mapping the content of a table's column to a QComboBox without creating another table?Thanks!
-
@combo->setModel(yourModel);
combo->setModelColumn(columnToDisplay);@if you want to map in this combo box the column of an external table, your model must be a QSqlRelationalTableModel, you have to set relations properly (see the docs) then you can get the external table with
@yourModel=modelToMap->relationModel(<foreing key column>);@ -
you can do an "insertRow(0)":http://doc.qt.nokia.com/4.7/qabstractitemmodel.html#insertRow on yourModel. you have to pay attention not to submit changes to the underlying db.
@yourModel->setEditStrategy(QSqlTableModel::OnManualSubmit);@
if you set this, the db won't be modified unless you call yourModel->submitAll();
-
I didn't made it. I did like that and doesn't work:
@
model = new QSqlRelationalTableModel(this);
model->setTable("Produse");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
proxyModel = new QSortFilterProxyModel;
proxyModel->setSourceModel(model);
proxyModel->setFilterKeyColumn(model->fieldIndex("Denumire"));
denumireCBox->setModel(proxyModel);
denumireCBox->setModelColumn(proxyModel->filterKeyColumn());
@