0

I have a QTableView and a QSortFilterProxyModel which has a QStandardItemModel as sourcemodel. I'm really desperate because I get an invalid QModelIndex for the following code. Invalid means that column and row of the index is -1 and when I want to get an indexWidget I get null as a widget. I really don't know actually what to expect as row and column from the QModelIndex.

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

I actually want to access individual widgets from the headerView.

easysaesch
  • 159
  • 1
  • 14

1 Answers1

0

Try it this method

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

later

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();
}
Vahagn Avagyan
  • 750
  • 4
  • 16