Using QComboBox with QSqlQueryModel
-
To speed-up
QComboBox
work with very big data set want to try to useQSqlQueryModel
instead ofQStandardItemModel
. However text data in QComboBox I need to map to an ID, which is stored and accessible currently byitemData(rowIndex, Qt::UserRole)
. InQSqlQueryModel
query there will be 2 columns: ID and Text; andQComboBox setModelColumn(1)
is defined, i.e. Text.How to correctly subclass or redefine
QSqlQueryModel
, ifcombobox->itemData(rowIndex, Qt::UserRole)
have to contain ID? Who had implemented such things or know link to a source? If I defineQVariant 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? -
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.