【问题标题】:Using QComboBox with QSqlQueryModel将 QComboBox 与 QSqlQueryModel 一起使用
【发布时间】:2015-09-19 19:58:59
【问题描述】:

为了加快QComboBox 处理非常大的数据集的速度,想尝试使用QSqlQueryModel 而不是QStandardItemModel。但是 QComboBox 中的文本数据我需要映射到一个 ID,该 ID 当前由 itemData(rowIndex, Qt::UserRole) 存储和访问。在QSqlQueryModel 查询中将有 2 列:ID 和 Text;并定义了QComboBox setModelColumn(1),即文本。

如果combobox->itemData(rowIndex, Qt::UserRole) 必须包含ID,如何正确子类化或重新定义QSqlQueryModel?谁实施了这样的事情或知道到源的链接?如果我这样定义QVariant QSqlQueryModel::data(const QModelIndex & item, int role = Qt::DisplayRole)

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

它会起作用吗,即combobox->itemData(rowIndex, Qt::UserRole) 在这种情况下是否会包含 ID?还是需要调查 Qt 源?

【问题讨论】:

    标签: c++ model-view-controller qt4 qcombobox


    【解决方案1】:

    是的,根据 QComboBox 代码应该可以工作:

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

    将实现这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多