spring
spring

Reputation: 18487

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

Core question: is there a way to specify what field is displayed by 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. That might be kind of dopey. 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);

Upvotes: 0

Views: 562

Answers (1)

alexisdm
alexisdm

Reputation: 29886

You can get the column index of that field with QSqlTableModel::fieldIndex, and set it as the field to display by the view with QListView::setModelColumn.

Upvotes: 4

Related Questions