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. How to determine if cell of a QTableWidget is currently visible?

How to determine if cell of a QTableWidget is currently visible?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 1.3k 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.
  • P Offline
    P Offline
    Publicnamer
    wrote on last edited by Publicnamer
    #1

    I've tried using the visualItemRect for a cell, but it is always valid even if a cell is not visible.
    Surely there is a way to tell if a cell is currently visible?

        QTableWidgetItem *item = mytable->item(row, column);
        QRect rect = mytable->visualItemRect (item);
        if (rect.isValid()) { /* always */ }
    
    eyllanescE 1 Reply Last reply
    0
    • P Publicnamer

      I've tried using the visualItemRect for a cell, but it is always valid even if a cell is not visible.
      Surely there is a way to tell if a cell is currently visible?

          QTableWidgetItem *item = mytable->item(row, column);
          QRect rect = mytable->visualItemRect (item);
          if (rect.isValid()) { /* always */ }
      
      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by
      #2

      @Publicnamer It must be clear that not every cell is associated with a QTableWidgetItem, so my solution uses the concept of a cell and not a QTableWidgetItem. On the other hand I will assume that a cell is visible if at least a portion of it is visible. Considering the above, a possible solution is to use the headers to obtain the visualIndex and logicalIndex.

      #include <QApplication>
      #include <QHeaderView>
      #include <QTableWidget>
      #include <QTimer>
      #include <QVector>
      
      #include <QDebug>
      
      bool cellIsVisible(QTableWidget & tableWidget, int row, int column){
          if (tableWidget.rowCount() == 0 || tableWidget.columnCount() == 0)
              return false;
          if(row < 0 || row >= tableWidget.rowCount() || column < 0 || column >= tableWidget.columnCount())
              return false;
          QHeaderView *horizontal_header = tableWidget.horizontalHeader();
          QHeaderView *vertical_header = tableWidget.verticalHeader();
          int row_start = std::max(vertical_header->visualIndexAt(0), 0);
          int row_end = vertical_header->visualIndexAt(vertical_header->height());
          if(row_end == -1)
              row_end = tableWidget.rowCount() - 1;
          int column_start = std::max(horizontal_header->visualIndexAt(0), 0);
          int column_end = horizontal_header->visualIndexAt(horizontal_header->width());
          if(column_end == -1)
              column_end = tableWidget.columnCount() - 1;
          QVector<int> rows;
          for(int i=row_start; i <= row_end; i++){
              rows.push_back(vertical_header->logicalIndex(i));
          }
          QVector<int> columns;
          for(int j=column_start; j <= column_end; j++){
              columns.push_back(horizontal_header->logicalIndex(j));
          }
      
          return rows.contains(row) && columns.contains(column);
      }
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          QTableWidget tableWidget(10, 10);
          tableWidget.resize(640, 480);
          tableWidget.show();
      
          QTimer timer;
          timer.setInterval(500);
          QObject::connect(&timer, &QTimer::timeout, &tableWidget, [&tableWidget](){
              int row = 2;
              int column = 2;
              bool is_visible = cellIsVisible(tableWidget, row, column);
              qDebug() << row << column << is_visible;
          });
          timer.start();
      
          return a.exec();
      }
      

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      P 1 Reply Last reply
      1
      • eyllanescE eyllanesc

        @Publicnamer It must be clear that not every cell is associated with a QTableWidgetItem, so my solution uses the concept of a cell and not a QTableWidgetItem. On the other hand I will assume that a cell is visible if at least a portion of it is visible. Considering the above, a possible solution is to use the headers to obtain the visualIndex and logicalIndex.

        #include <QApplication>
        #include <QHeaderView>
        #include <QTableWidget>
        #include <QTimer>
        #include <QVector>
        
        #include <QDebug>
        
        bool cellIsVisible(QTableWidget & tableWidget, int row, int column){
            if (tableWidget.rowCount() == 0 || tableWidget.columnCount() == 0)
                return false;
            if(row < 0 || row >= tableWidget.rowCount() || column < 0 || column >= tableWidget.columnCount())
                return false;
            QHeaderView *horizontal_header = tableWidget.horizontalHeader();
            QHeaderView *vertical_header = tableWidget.verticalHeader();
            int row_start = std::max(vertical_header->visualIndexAt(0), 0);
            int row_end = vertical_header->visualIndexAt(vertical_header->height());
            if(row_end == -1)
                row_end = tableWidget.rowCount() - 1;
            int column_start = std::max(horizontal_header->visualIndexAt(0), 0);
            int column_end = horizontal_header->visualIndexAt(horizontal_header->width());
            if(column_end == -1)
                column_end = tableWidget.columnCount() - 1;
            QVector<int> rows;
            for(int i=row_start; i <= row_end; i++){
                rows.push_back(vertical_header->logicalIndex(i));
            }
            QVector<int> columns;
            for(int j=column_start; j <= column_end; j++){
                columns.push_back(horizontal_header->logicalIndex(j));
            }
        
            return rows.contains(row) && columns.contains(column);
        }
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
        
            QTableWidget tableWidget(10, 10);
            tableWidget.resize(640, 480);
            tableWidget.show();
        
            QTimer timer;
            timer.setInterval(500);
            QObject::connect(&timer, &QTimer::timeout, &tableWidget, [&tableWidget](){
                int row = 2;
                int column = 2;
                bool is_visible = cellIsVisible(tableWidget, row, column);
                qDebug() << row << column << is_visible;
            });
            timer.start();
        
            return a.exec();
        }
        
        P Offline
        P Offline
        Publicnamer
        wrote on last edited by
        #3

        @eyllanesc That's certainly an interesting approach.

        I ended up coding it so that I ask the table what item is currently at the bottom of the table rectangle.
        I found that when I scroll to the bottom, I get a null pointer for that item. At all other times I get an item pointer.

        eyllanescE 1 Reply Last reply
        0
        • P Publicnamer

          @eyllanesc That's certainly an interesting approach.

          I ended up coding it so that I ask the table what item is currently at the bottom of the table rectangle.
          I found that when I scroll to the bottom, I get a null pointer for that item. At all other times I get an item pointer.

          eyllanescE Offline
          eyllanescE Offline
          eyllanesc
          wrote on last edited by eyllanesc
          #4

          @Publicnamer Your approach is suitable if the table did not apply the sorting or when you hide rows and columns. Your logic is similar to the visualIndex but that may be different from the internal position so I use the logicalIndex.

          If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

          P 1 Reply Last reply
          0
          • eyllanescE eyllanesc

            @Publicnamer Your approach is suitable if the table did not apply the sorting or when you hide rows and columns. Your logic is similar to the visualIndex but that may be different from the internal position so I use the logicalIndex.

            P Offline
            P Offline
            Publicnamer
            wrote on last edited by
            #5

            @eyllanesc I'm not using sorting as far as I know. I assume you mean sorting the contents automatically?

            eyllanescE 1 Reply Last reply
            0
            • P Publicnamer

              @eyllanesc I'm not using sorting as far as I know. I assume you mean sorting the contents automatically?

              eyllanescE Offline
              eyllanescE Offline
              eyllanesc
              wrote on last edited by
              #6

              @Publicnamer Yes. I only point out the limitations of your solution for future readers. I am not saying that it is bad for you but I only warn the possible inconveniences for other users.

              If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

              1 Reply Last reply
              0

              • Login

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