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. Using QComboBox with QSqlQueryModel
Qt 6.11 is out! See what's new in the release blog

Using QComboBox with QSqlQueryModel

Scheduled Pinned Locked Moved General and Desktop
qsqlquerymodelqcomboboxc++model-viewitemdataqt4
2 Posts 1 Posters 2.7k 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.
  • Aleksey_KA Offline
    Aleksey_KA Offline
    Aleksey_K
    wrote on last edited by Aleksey_K
    #1

    To speed-up QComboBox work with very big data set want to try to use QSqlQueryModel instead of QStandardItemModel. However text data in QComboBox I need to map to an ID, which is stored and accessible currently by itemData(rowIndex, Qt::UserRole). In QSqlQueryModel query there will be 2 columns: ID and Text; and QComboBox setModelColumn(1) is defined, i.e. Text.

    How to correctly subclass or redefine QSqlQueryModel, if combobox->itemData(rowIndex, Qt::UserRole) have to contain ID? Who had implemented such things or know link to a source? If I define QVariant QSqlQueryModel::data ( const QModelIndex & item, int role = Qt::DisplayRole ) in a such way:

    QVariant MySqlModel::data(const QModelIndex &index, int role) const
    {
        if(role == Qt::UserRole && index.column() == 1)
             return QSqlQueryModel::data(this->index(index.row(), 0), Qt::DisplayRole);
    
        return QSqlQueryModel::data(index, role);
    }
    

    will it work, i.e. whether combobox->itemData(rowIndex, Qt::UserRole) will contain ID in this case? Or need to investigate Qt sources?

    Aleksey_KA 1 Reply Last reply
    0
    • Aleksey_KA Aleksey_K

      To speed-up QComboBox work with very big data set want to try to use QSqlQueryModel instead of QStandardItemModel. However text data in QComboBox I need to map to an ID, which is stored and accessible currently by itemData(rowIndex, Qt::UserRole). In QSqlQueryModel query there will be 2 columns: ID and Text; and QComboBox setModelColumn(1) is defined, i.e. Text.

      How to correctly subclass or redefine QSqlQueryModel, if combobox->itemData(rowIndex, Qt::UserRole) have to contain ID? Who had implemented such things or know link to a source? If I define QVariant QSqlQueryModel::data ( const QModelIndex & item, int role = Qt::DisplayRole ) in a such way:

      QVariant MySqlModel::data(const QModelIndex &index, int role) const
      {
          if(role == Qt::UserRole && index.column() == 1)
               return QSqlQueryModel::data(this->index(index.row(), 0), Qt::DisplayRole);
      
          return QSqlQueryModel::data(index, role);
      }
      

      will it work, i.e. whether combobox->itemData(rowIndex, Qt::UserRole) will contain ID in this case? Or need to investigate Qt sources?

      Aleksey_KA Offline
      Aleksey_KA Offline
      Aleksey_K
      wrote on last edited by
      #2

      Yeah, according to QComboBox code should work:

      QVariant QComboBox::itemData(int index, int role) const
      {
          Q_D(const QComboBox);
          QModelIndex mi = d->model->index(index, d->modelColumn, d->root);
          return d->model->data(mi, role);
      }
      

      Will implement this.

      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