How to insert item to combobox that uses a QSQLquery model
-
wrote on 7 Aug 2012, 01:30 last edited by
Hello guys
I have Combobox and SQLite. I read the database and load the data into the combobox. How can I add data to this combobox that does not exist in the database?
@
model->setQuery(query);
ui->logindBList->setModel(model);I want to add this item to the list of data in the combo but it does not work
ui->logindBList->addItem("localhost");
@ -
wrote on 7 Aug 2012, 07:53 last edited by
No, that does not work. It is either/or. Either you use an editable model like the default model, or you use an SQL query model. An SQL query model is not editable, so you cannot add items to it. You might try if it works with an QSqlTableModel instead, but I doubt it. Instead, you could read-in the data from the query, and add this data as items in your combobox. That will allow you to add new items, that you can then add back to the database.
-
wrote on 7 Aug 2012, 09:06 last edited by
Thank you for your reply, but i dont know how to read the data from the modelquery
do you have an example? -
wrote on 7 Aug 2012, 09:28 last edited by
I'd not use the query model in in this case, but use a [[doc:QSqlQuery]] directly. The documentation of QSqlQuery contains an example on how to read the results.
-
wrote on 7 Aug 2012, 19:59 last edited by
I don't want to use QSQLQuery. Is there a way to copy the model into another editable model?
-
wrote on 7 Aug 2012, 20:20 last edited by
Sure, but that would be more complicated to do, I think. Did you try using a QTableModel instead?
-
wrote on 7 Aug 2012, 20:51 last edited by
I found this link which describes it. Thank you Andre
1/7