PDA

View Full Version : QComboBox -> setModel -> Strange behaviour



oscar
13th August 2008, 15:37
Hi,

If I associate a QComboBox to a table with setModel, then I can only manually add one item to this combobox. If I add 2 items, only one empty item appears at the end of the list.
How can I do? (I don't want to put the missing items into the database, I want them to be added dynamically).

Here is the source code:


myModel = new QSqlRelationalTableModel();
myModel->setTable("woundType");
myModel->select();
typeCbx->setModel(myModel);
typeCbx->setModelColumn(2);
typeCbx->insertItem(2, QString(""));
typeCbx->addItem(QString("Ajout/Modification/Suppression"));
typeCbx->addItem(QString("Others")); //if I add this item, only one empty line appears at the end of the combobox

Thanks for yout help
Regards,
Oscar

wysota
13th August 2008, 16:45
In general you have to decide whether you want to use the model approach or the manual approach, mixing both is not advised. In your situation I'd suggest inserting those items from the database manually into the widget. Otherwise implement a simple proxy model around your model (or subclass the model you are using) to unite both the model data and the manual data into a single model.

oscar
13th August 2008, 22:27
Thanks for your reply.
so, I finally chose the manual approach like this:



QSqlQuery query;
QString queryStr = QString("select id,value from woundType order by displayOrder");
query.exec(queryStr);
if (!query.isActive())
Logger::log(Logger::fatalLevel, QString("200808131608: ") + qPrintable(query.lastError().text()));

typeCbx->addItem("");

while (query.next()) {
typeCbx->addItem(query.value(1).toString());
typeCbx->setItemData(typeCbx->count()-1, query.value(0).toInt(), Qt::UserRole);
}
typeCbx->addItem("Ajout/Modification/Suppression");
typeCbx->addItem("Others");



which is the only one which really suits my needs but the counter part is more code to maintain...

the use of proxy is indeed maybe the best solution, I'll have a look.

All the best,
Oscar