Autoscrolling issue when using Qt stylesheet for QTableView::item and QComboBox cellWidgets
-
wrote on 6 Apr 2023, 08:56 last edited by gserm 4 Jun 2023, 09:20
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:
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.
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.
-
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:
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.
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.
wrote on 6 Apr 2023, 16:01 last edited by CPPUIX 4 Dec 2023, 06:17@gserm I have a workaround that might work for you, I used an
eventFilter
, and enabledauto scrolling
onkey press
, and disabled it onkey 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/2