You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Qt 5.15.2中QTableView::item与单元格QComboBox样式冲突致自动滚动问题

Qt 5.15.2 QTableWidget嵌入QComboBox悬停时自动滚动异常问题

在Windows系统下使用Qt 5.15.2开发,创建了一个包含多个QComboBox作为单元格部件的QTableWidget,应用了如下样式表:

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

出现的问题:显示表格后,当鼠标悬停在最后可见行的QComboBox上时,表格会自动向下滚动;但悬停在该行的普通QTableWidgetItem上时无此异常。移除任意一个hover样式可恢复正常,但会丢失自定义样式效果。

尝试使用更明确的样式选择器,问题仍未解决:

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

示例代码如下:

#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();
}

更新
使用setAutoScroll(false)可规避自动滚动问题,但会导致表格失去方向键切换当前项时的自动滚动功能。经确认,Qt 5.15之前版本无此问题,推测是Qt的回归bug。

内容的提问来源于stack exchange,提问作者linuxfever

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 00:53:14