[SOLVED] List of visible rows in the viewport of QTableView

  • Hi,

    Is there anyway I can get the list of visible rows in the viewport of QTableView. Currently in the viewportEvent() of QTableView I can get the firstRow and the lastRow by using

    QModelIndex firstRowIndex = indexAt(QPoint(viewport()->rect().x()+5,viewport()->rect().y()+5));
    QModelIndex lastRowIndex = indexAt(QPoint(viewport()->rect().x()+5,viewport()->rect().height()-5));

    The requirement is to iterate/update something specific to the visible Rows in the viewport. If the some rows between the first/last row are hidden then I cannot use a for loop which will consume more time (e.g firstRow = 10, lastRow = 9500; )


  • Lifetime Qt Champion


    IIRC, you can use visualRect to build that list.

    Hope it helps

  • Hi SGaist,

    Thanks for your reply, with visualRect I need to pass the QModelIndex. In order to get the list of rows/items in the viewport of the tableView I now calculate using the height eg.

    int height = 0;
    while (height < viewport()->rect().height())  // viewport() of tableView
      qDebug() << indexAt(QPoint(viewport()->rect().x(),height)).row());
      height += 25;

  • Lifetime Qt Champion

    Are you sure about 25 ? You have to be sure that your rows are 50px high otherwise you might miss some

  • Yes, this approach is temporary and I too don't like to do it this way. In fact for my requirement this does not make any difference even if I get the list of rows in the visible viewport(). I'll try to explain in brief what I am trying to achieve.

    I came across an article : Widgets on a header which works perfect for a horizontalHeaderView and also so a verticalHeaderView with few rows. Currently I am implementing the same for a VerticalHeaderView where I have a panel of buttons (eg add/delete) when we click on these button it add/delete a row.

    The only issue right now is performance, if the model is filled with a lot of rows then creating new panels is a time consuming process. My initial idea was to create the instance of these panel only for the visible rows in the viewport but it creates a lag/flickering when the viewport gets updated while scrolling the tableView.

    Another approach is to paint/render a fake panel and create a new instance when the mouse hover/enter the verticalHeader (don't know if this is even possible, with tableView cells this is easy to implement with delegate).

    Thanks for your time !

  • Lifetime Qt Champion

    What do you need to render exactly ?

  • Are you sure indexAt() for every row is not slower then just iterating between indexes and checking that row is not hidden?

Log in to reply

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