[Solved] Creating QModelIndex from QTableView row number



  • I would like to create a QModelIndex from a row number in a QTableView.

    I know you can get the row from the QModelIndex (index.row())
    I'm wondering if you can go the opposite way?
    Reason I need a QModelIndex is that i'm drawing a custom QWidget on top of a QTableView and I need the function "visualRect(QModelIndex)" of QTableView in order to find the position on the screen.

    I tried adding a custom method in my QAbstractTableModel like this:
    @QModelIndex IntervalTableModel::getIndexAtRow(int row) {

    qDebug() << "getIndexAtRow";
    QModelIndex index(row, 0); // not valid constructor..
    return index;
    

    }@

    I would use this code from the main file here :
    @
    void WorkoutCreator::restoreRepeatWidgetInterface()
    {

    foreach (RepeatWidget *wid, lstRepeatWidget)
    {
        int firstRow = wid->firstRow;
        int lastRow = wid->lastRow;
    
        QModelIndex firstIndex =  intervalModel->getIndexAtRow(wid->firstRow);
    
        /// TODO: redraw widget on top of QTableView
    }
    

    }
    @

    Thanks last post for a while sorry for all the posts ;)



  • Solved, it was explained in the docs :
    I will try this

    To obtain a model index that refers to an existing item in a model, call QAbstractItemModel::index() with the required row and column values, and the model index of the parent. When referring to top-level items in a model, supply QModelIndex() as the parent index.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.