QTableView horizontalHeaderItem无效QModelIndex
问题描述:
我有一个QTableView
和一个QSortFilterProxyModel
它有一个QStandardItemModel
作为sourcemodel。我真的很绝望,因为我得到一个无效的QModelIndex
为下面的代码。无效意味着索引的列和行是-1,并且当我想要获取indexWidget时,我会将null作为小部件。 我真的不知道QModelIndex中的行和列会有什么期望。QTableView horizontalHeaderItem无效QModelIndex
QStandardItemModel* model = static_cast<QStandardItemModel*> (proxyModel.sourceModel());
QModelIndex index = model->horizontalHeaderItem (0)->index();
我实际上想从headerView中访问单个窗口小部件。
答
试试这个方法
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();
}
+0
您需要正确创建TableView模型 –
+0
这是我的代码示例,我可以将示例TableView模型 –
如果这是一个标题项目,还有什么你期待什么呢?我认为模型指数值是合理的。 – vahancho
那么你有另一种访问headerItem小部件的方式吗? – bob
嗯......我不知道。你为什么需要这个? – vahancho