Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Is a QSqlQueryModel the proper way to bind a table's field to a QListView?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved