[SOLVED] Is a QSqlQueryModel the proper way to bind a table's field to a QListView?



  • I have a db table with two fields: id and keyword

    I wanted to display keyword in a QListView but was getting the id value, since that is the first field in the table. I ended up using a QSqlQueryModel and it works but I'm wondering if this is the standard way of doing this.

    The user will be able to add additional keywords to the db, so in order to display the new data I need to do a new query. I tried switching to a QTableView and QSQLTableModel but I don't need that functionality. Bad decision?

    I'm setting up the QSqlQueryModel > QListView connection as follows.
    @
    model = new QSqlQueryModel(this);
    model->setQuery("SELECT keyword FROM keywords",db);
    keywordListView->setModel(model);
    @



  • You can use the table model with a list view by setting the column that is displayed: QListView::setModelColumn().

    QSqlQueryModel is read-only. If you want to update through the model then you need to use QSqlTableModel (or your own custom model).
    You will need to address how the undisplayed column gets a value when new rows are added.



  • Hey thanks! I'd missed _ setModelColumn()._


Log in to reply
 

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