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. Autoscrolling issue when using Qt stylesheet for QTableView::item and QComboBox cellWidgets
Forum Updated to NodeBB v4.3 + New Features

Autoscrolling issue when using Qt stylesheet for QTableView::item and QComboBox cellWidgets

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 269 Views 1 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.
  • G Offline
    G Offline
    gserm
    wrote on 6 Apr 2023, 08:56 last edited by gserm 4 Jun 2023, 09:20
    #1

    I'm using Qt 5.15.2 on Windows. I have a QTableWidget which has a bunch of QComboBoxes as cell widgets. The application uses the following stylesheet:

    QComboBox:hover{background: yellow;}
    QTableView::item:hover{color: red;}
    

    Now when showing the table and hovering over a QComboBox on the last visible row, the table starts auto-scrolling down, like so:

    buggy.gif

    This doesn't happen if I hover over a normal QTableWidgetItem on the last visible row as shown on the image.

    If I remove either of the hover keyword from the stylesheet, then things work fine but I lose my style. My guess is that there is a conflict between QTableView::item and QComboBox when it's used as a cell widget so I've tried being more explicit about which QComboBoxes the stylesheet should apply to by using

    QTableView QComboBox:hover{background: yellow;}
    QTableView::item:hover{color: red;}
    

    but that didn't help either. Using

    table->setAutoScroll(false)
    

    works around the issue of the autoscrolling. Unfortunately, the table loses the ability to autoscroll when the current item changes with the directional arrows as shown below.

    no_scroll.gif

    I've confirmed that this issue is not reproducible with Qt versions prior to Qt 5.15, so I'm guessing this is a Qt regression bug? Can anyone suggest a workaround please?

    Here is the code I've used for this example:

    #include <QTableWidget>
    #include <QBoxLayout>
    #include <QComboBox>
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        qApp->setStyleSheet(
                    "QComboBox{background: yellow;}"
                    "QTableView::item:hover{color: red;}"
                            );
    
        auto rows = 100;
        auto cols = 5;
        auto table = new QTableWidget(rows, cols);
    
        for(auto i = 0; i != rows; ++i)
        {
            for(auto j = 0; j != cols; ++j)
            {
                if(j == 0)
                {
                    auto item = new QTableWidgetItem("hello");
                    table->setItem(i, j, item);
                }
                else
                {
                    auto cb = new QComboBox();
                    cb->addItems(QStringList() << "Item1" << "Item2");
                    table->setCellWidget(i, j, cb);
                }
            }
        }
    
        table->setMinimumSize(800,600);
        table->show();
        return a.exec();
    }
    

    I've also filed a bug with Qt but haven't heard anything back yet.

    C 1 Reply Last reply 6 Apr 2023, 16:01
    0
    • G gserm
      6 Apr 2023, 08:56

      I'm using Qt 5.15.2 on Windows. I have a QTableWidget which has a bunch of QComboBoxes as cell widgets. The application uses the following stylesheet:

      QComboBox:hover{background: yellow;}
      QTableView::item:hover{color: red;}
      

      Now when showing the table and hovering over a QComboBox on the last visible row, the table starts auto-scrolling down, like so:

      buggy.gif

      This doesn't happen if I hover over a normal QTableWidgetItem on the last visible row as shown on the image.

      If I remove either of the hover keyword from the stylesheet, then things work fine but I lose my style. My guess is that there is a conflict between QTableView::item and QComboBox when it's used as a cell widget so I've tried being more explicit about which QComboBoxes the stylesheet should apply to by using

      QTableView QComboBox:hover{background: yellow;}
      QTableView::item:hover{color: red;}
      

      but that didn't help either. Using

      table->setAutoScroll(false)
      

      works around the issue of the autoscrolling. Unfortunately, the table loses the ability to autoscroll when the current item changes with the directional arrows as shown below.

      no_scroll.gif

      I've confirmed that this issue is not reproducible with Qt versions prior to Qt 5.15, so I'm guessing this is a Qt regression bug? Can anyone suggest a workaround please?

      Here is the code I've used for this example:

      #include <QTableWidget>
      #include <QBoxLayout>
      #include <QComboBox>
      #include <QApplication>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          qApp->setStyleSheet(
                      "QComboBox{background: yellow;}"
                      "QTableView::item:hover{color: red;}"
                              );
      
          auto rows = 100;
          auto cols = 5;
          auto table = new QTableWidget(rows, cols);
      
          for(auto i = 0; i != rows; ++i)
          {
              for(auto j = 0; j != cols; ++j)
              {
                  if(j == 0)
                  {
                      auto item = new QTableWidgetItem("hello");
                      table->setItem(i, j, item);
                  }
                  else
                  {
                      auto cb = new QComboBox();
                      cb->addItems(QStringList() << "Item1" << "Item2");
                      table->setCellWidget(i, j, cb);
                  }
              }
          }
      
          table->setMinimumSize(800,600);
          table->show();
          return a.exec();
      }
      

      I've also filed a bug with Qt but haven't heard anything back yet.

      C Offline
      C Offline
      CPPUIX
      wrote on 6 Apr 2023, 16:01 last edited by CPPUIX 4 Dec 2023, 06:17
      #2

      @gserm I have a workaround that might work for you, I used an eventFilter, and enabled auto scrolling on key press, and disabled it on key release. here's how I did it:

      main.cpp

      #include <QApplication>
      #include <QTableWidget>
      #include <QComboBox>
      #include <QObject>
      #include <QEvent>
      #include "myEventFilter.h"
      
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          qApp->setStyleSheet(
              "QComboBox{background: yellow;}"
              "QTableView::item:hover{color: red;}"
              );
      
          auto rows = 100;
          auto cols = 5;
          auto table = new QTableWidget(rows, cols);
      
          for(auto i = 0; i != rows; ++i)
          {
              for(auto j = 0; j != cols; ++j)
              {
                  if(j == 0)
                  {
                      auto item = new QTableWidgetItem("hello");
                      table->setItem(i, j, item);
                  }
                  else
                  {
                      auto cb = new QComboBox();
                      cb->addItems(QStringList() << "Item1" << "Item2");
                      table->setCellWidget(i, j, cb);
                  }
              }
          }
      
          table->setMinimumSize(800,600);
          table->setAutoScroll(false);
      
          myEventFilter *filter = new myEventFilter();
          table->installEventFilter(filter);
          filter->t=table;
      
          table->show();
          
          return a.exec();
      }
      

      myEventFilter.h

      #ifndef MYEVENTFILTER_H
      #define MYEVENTFILTER_H
      
      #include <QObject>
      #include <QEvent>
      #include <QTableWidget>
      
      class myEventFilter : public QObject
      {
          Q_OBJECT
      
      public:
          myEventFilter (QObject *parent = nullptr) {};
          QTableWidget *t;
      
      protected:
          bool eventFilter(QObject *obj, QEvent *event) override
          {
              if(event->type() == QEvent::KeyPress)
              {
                  t->setAutoScroll(true);
              }
              if(event->type() == QEvent::KeyRelease)
              {
                  t->setAutoScroll(false);
              }
      
              return QObject::eventFilter(obj,event);
          };
      };
      
      #endif // MYEVENTFILTER_H
      
      
      1 Reply Last reply
      0

      1/2

      6 Apr 2023, 08:56

      • Login

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