I'm using Qt 5.15.2 on Windows. I have a QTableWidget
which has a bunch of QComboBox
es 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 QComboBox
es the stylesheet should apply to by using
QTableView QComboBox:hover{background: yellow;}
QTableView::item:hover{color: red;}
but that didn't help either. Any suggestions on how to solve this 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();
}
UPDATE
As suggested by this answer, using 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 also just confirmed that the whole issue described in my post is not reproducible with Qt versions prior to Qt 5.15, so I'm guessing this is a Qt regression bug.