0

我有一个QTableViewQSortFilterProxyModel一个QStandardItemModel作为源模型的。我真的很绝望,因为我QModelIndex对以下代码无效。无效意味着索引的列和行是-1,当我想获取 indexWidget 时,我得到 null 作为小部件。 我真的不知道 QModelIndex 中的行和列会发生什么。

QStandardItemModel* model = static_cast<QStandardItemModel*> (proxyModel.sourceModel());
QModelIndex index = model->horizontalHeaderItem (0)->index ();

我实际上想从 headerView 访问单个小部件。

4

1 回答 1

0

试试这个方法

connect(m_adminTable, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(onTableDoubleClickedSlot(QModelIndex)));

之后

void UsersTableWidget::onTableDoubleClickedSlot(const QModelIndex& modelIndex)
{
    QModelIndex sourceModelIndex = m_adminModelProxy->mapToSource(modelIndex);
    m_currentUserID = m_adminModel->data(sourceModelIndex, Qt::UserRole).toInt(nullptr);//patientIDs.at(row);;

    emit  onUserSelectionChanged(m_currentUserID);
}



QVariant AdminModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
    return QVariant();

if (index.row() >= m_users.size() || index.row() < 0)
    return QVariant();

if (role == Qt::DisplayRole) {
    RowType row = m_users.at(index.row());
    return row.getColumnValue(index.column());
} else if(role == Qt::UserRole) {
    return m_users.at(index.row()).getId();
}
if (role == Qt::TextAlignmentRole)
        return Qt::AlignCenter;
return QVariant();
}
于 2017-07-14T13:21:35.633 回答