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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.