Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Creating QModelIndex from QTableView row number
QtWS25 Last Chance

[Solved] Creating QModelIndex from QTableView row number

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 8.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maximus
    wrote on last edited by
    #1

    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 ;)


    Free Indoor Cycling Software - https://maximumtrainer.com

    1 Reply Last reply
    0
    • M Offline
      M Offline
      maximus
      wrote on last edited by
      #2

      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.


      Free Indoor Cycling Software - https://maximumtrainer.com

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved