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. Determining the last currently visible QTableWidgetItem

Determining the last currently visible QTableWidgetItem

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.2k Views 2 Watching
  • 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.
  • l3u_L Offline
    l3u_L Offline
    l3u_
    wrote on last edited by
    #1

    Hi :-)

    I need to know the visible row number of QTableWidgetItems. Which is not the row(), as the first visible row is not necessarily row 0, and probably, there are hidden rows, which I need to skip when counting. I do this vis the following function:

    int RegistrationPage::visibleRow(const QTableWidgetItem *item) const
    {
        const auto *firstItem = m_playersList->itemAt(0, 0);
        if (firstItem == nullptr) {
            return -1;
        }
    
        const int firstRow = firstItem->row();
        const int targetRow = item->row();
    
        int visibleRow = 0;
        for (int row = firstRow; row < m_playersList->rowCount(); row++) {
            if (m_playersList->item(row, 0)->row() == targetRow) {
                return visibleRow;
            }
            visibleRow += isRowVisible(row);
        }
    
        return -1;
    }
    

    It iterates, beginning from the first visible row, over all following ones. But this wouldn't be necessary, as this also processes items that aren't inside the current viewport of the QTableWidget, I would only have to check up to the last visible item.

    Now, the question is: How can I determine the last visible item? I can't use itemAt() using the widget's height, as the height isn't the height of the viewport. Maybe I missed something in the docs, but I didn't find a function returning the geometry of the viewport. Also, I would like to get the last item that is completely visible at the bottom …

    Any hints? Thanks for all help in advace :-)

    Cheers, Tobias

    Pl45m4P 1 Reply Last reply
    0
    • l3u_L l3u_

      Hi :-)

      I need to know the visible row number of QTableWidgetItems. Which is not the row(), as the first visible row is not necessarily row 0, and probably, there are hidden rows, which I need to skip when counting. I do this vis the following function:

      int RegistrationPage::visibleRow(const QTableWidgetItem *item) const
      {
          const auto *firstItem = m_playersList->itemAt(0, 0);
          if (firstItem == nullptr) {
              return -1;
          }
      
          const int firstRow = firstItem->row();
          const int targetRow = item->row();
      
          int visibleRow = 0;
          for (int row = firstRow; row < m_playersList->rowCount(); row++) {
              if (m_playersList->item(row, 0)->row() == targetRow) {
                  return visibleRow;
              }
              visibleRow += isRowVisible(row);
          }
      
          return -1;
      }
      

      It iterates, beginning from the first visible row, over all following ones. But this wouldn't be necessary, as this also processes items that aren't inside the current viewport of the QTableWidget, I would only have to check up to the last visible item.

      Now, the question is: How can I determine the last visible item? I can't use itemAt() using the widget's height, as the height isn't the height of the viewport. Maybe I missed something in the docs, but I didn't find a function returning the geometry of the viewport. Also, I would like to get the last item that is completely visible at the bottom …

      Any hints? Thanks for all help in advace :-)

      Cheers, Tobias

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @l3u_

      Hi,

      maybe you can use these

      https://doc.qt.io/qt-5/qtablewidget.html#visualItemRect
      https://doc.qt.io/qt-5/qtablewidget.html#visualColumn
      https://doc.qt.io/qt-5/qtablewidget.html#visualRow

      https://doc.qt.io/qt-5/qtableview.html#columnViewportPosition
      https://doc.qt.io/qt-5/qtableview.html#rowViewportPosition

      to count your visible rows / cols.

      @l3u_ said in Determining the last currently visible QTableWidgetItem:

      I didn't find a function returning the geometry of the viewport

      https://doc.qt.io/qt-5/qabstractscrollarea.html#viewport

      This returns your viewport as QWidget.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      l3u_L 1 Reply Last reply
      3
      • Pl45m4P Pl45m4

        @l3u_

        Hi,

        maybe you can use these

        https://doc.qt.io/qt-5/qtablewidget.html#visualItemRect
        https://doc.qt.io/qt-5/qtablewidget.html#visualColumn
        https://doc.qt.io/qt-5/qtablewidget.html#visualRow

        https://doc.qt.io/qt-5/qtableview.html#columnViewportPosition
        https://doc.qt.io/qt-5/qtableview.html#rowViewportPosition

        to count your visible rows / cols.

        @l3u_ said in Determining the last currently visible QTableWidgetItem:

        I didn't find a function returning the geometry of the viewport

        https://doc.qt.io/qt-5/qabstractscrollarea.html#viewport

        This returns your viewport as QWidget.

        l3u_L Offline
        l3u_L Offline
        l3u_
        wrote on last edited by l3u_
        #3

        @Pl45m4 Thanks for the hint! This one does the trick:

        int RegistrationPage::visibleRow(const QTableWidgetItem *item) const
        {
            const auto *firstItem = m_playersList->itemAt(0, 0);
            if (firstItem == nullptr) {
                return -1;
            }
        
            const int targetRow = item->row();
            const int firstRow = firstItem->row();
        
            int lastRow = m_playersList->rowCount();
            const int viewportHeight = m_playersList->viewport()->height();
            const auto *lastItem = m_playersList->itemAt(0, viewportHeight - 1);
            if (lastItem != nullptr) {
                const bool completelyVisible = m_playersList->visualItemRect(lastItem).bottomLeft().y()
                                               <= viewportHeight;
                lastRow = lastItem->row() + completelyVisible;
            }
        
            int visibleRow = 0;
            for (int row = firstRow; row < lastRow; row++) {
                if (m_playersList->item(row, 0)->row() == targetRow) {
                    return visibleRow;
                }
                visibleRow += isRowVisible(row);
            }
        
            return -1;
        }
        
        1 Reply Last reply
        2

        • Login

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