QTableView horizo​​ntalHeaderItem无效QModelIndex

问题描述:

我有一个QTableView和一个QSortFilterProxyModel它有一个QStandardItemModel作为sourcemodel。我真的很绝望,因为我得到一个无效的QModelIndex为下面的代码。无效意味着索引的列和行是-1,并且当我想要获取indexWidget时,我会将null作为小部件。 我真的不知道QModelIndex中的行和列会有什么期望。QTableView horizo​​ntalHeaderItem无效QModelIndex

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

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

+0

如果这是一个标题项目,还有什么你期待什么呢?我认为模型指数值是合理的。 – vahancho

+0

那么你有另一种访问headerItem小部件的方式吗? – bob

+0

嗯......我不知道。你为什么需要这个? – vahancho

试试这个方法

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模型 –