QSplitter QSS hover失效的解决办法

原创
2022-12-16
5084
0

关于QSplitter在样式表中鼠标悬浮失效的解决方法:

#include "widget.h"
#include <QSplitter>
#include <QTextEdit>
#include <QVBoxLayout>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    QVBoxLayout* layout = new QVBoxLayout(this);
    QWidget *leftWidget = new QWidget;
    QWidget *rightWidget = new QWidget;

    QVBoxLayout *leftLayout = new QVBoxLayout(leftWidget);
    QVBoxLayout *rightLayout = new QVBoxLayout(rightWidget);
    leftLayout->addWidget(new QTextEdit);
    rightLayout->addWidget(new QTextEdit);

    QSplitter *splitter = new QSplitter;
    splitter->addWidget(leftWidget);
    splitter->addWidget(rightWidget);

    layout->addWidget(splitter);

    this->setStyleSheet("QSplitter::handle{"
                            "background-color: rgb(100, 100, 100);"
                        "}"
                        "QSplitter::handle:hover{"
                            "background-color: rgb(200, 100, 100);"
                        "}"
                        "QSplitter::handle:pressed{"
                            "background-color: rgb(70, 70, 70);"
                        "}");
}

Widget::~Widget()
{

}

程序运行效果如下:

此时QSplitter鼠标悬浮的样式是失效的,
解决办法是添加下面的代码

splitter->handle(1)->setAttribute(Qt::WA_Hover, true);

添加后效果如下:

不会飞的纸飞机
扫一扫二维码,了解我的更多动态。

下一篇文章:自定义QGraphicsItem实现平移、改变尺寸和旋转