Using QComboBox with QSqlQueryModel



  • 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?



  • 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.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.