我有一个QTableView和一个QSortFilterProxyModel,其中有一个QStandardItemModel作为源模型。我真的很绝望,因为对于下面的代码我得到了一个无效的QModelIndex。无效意味着索引的列和行为-1,当我想要获得一个indexWidget时,我会将它作为一个小部件获得空。--我真的不知道作为QModelIndex.的行和列的期望是什么
QStandardItemModel* model = static_cast<QStandardItemModel*> (proxyModel.sourceModel());
QModelIndex index = model->horizontalHeaderItem (0)->index ();实际上,我想从headerView中访问各个小部件。
发布于 2017-07-14 13:21:35
试试这个方法
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();
}https://stackoverflow.com/questions/45101391
复制相似问题