How to insert item to combobox that uses a QSQLquery model



  • 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");
    @



  • 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.



  • Thank you for your reply, but i dont know how to read the data from the modelquery
    do you have an example?



  • 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.



  • I don't want to use QSQLQuery. Is there a way to copy the model into another editable model?



  • Sure, but that would be more complicated to do, I think. Did you try using a QTableModel instead?



  • I found this link which describes it. Thank you Andre

    http://apidocs.meego.com/1.2/qt4/sql-model.html


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.