Qt 样式:如何设置使用 .ui 生成类的小部件的样式?

发布于 2024-08-19 08:37:39 字数 2220 浏览 3 评论 0原文

在我的 .qss 文件中,我想指定小部件的背景颜色,该小部件使用 .ui 文件中生成的类,例如:

#ifndef SPLASH_H
#define SPLASH_H

#include <QWidget>

#include "ui_SplashView.h"

class Splash : public QWidget
{
Q_OBJECT
public:
    explicit Splash(QWidget *parent = 0);

signals:

public slots:

private:
    Ui::SplashView ui;
};

#endif // SPLASH_H

--

#include "splash.h"

Splash::Splash(QWidget *parent) : QWidget(parent)
{
    ui.setupUi(this);
}

ui_SplashView.h 文件本身如下所示:

QT_BEGIN_NAMESPACE

class Ui_SplashView
{
public:
    QPushButton *pushButton;

    void setupUi(QWidget *SplashView)
    {
    if (SplashView->objectName().isEmpty())
        SplashView->setObjectName(QString::fromUtf8("SplashView"));
    SplashView->resize(360, 640);
    pushButton = new QPushButton(SplashView);
    pushButton->setObjectName(QString::fromUtf8("pushButton"));
    pushButton->setGeometry(QRect(70, 30, 75, 23));

    retranslateUi(SplashView);

    QMetaObject::connectSlotsByName(SplashView);
} // setupUi

void retranslateUi(QWidget *SplashView)
{
    SplashView->setWindowTitle(QApplication::translate("SplashView", "Splash", 0, QApplication::UnicodeUTF8));
    pushButton->setText(QApplication::translate("SplashView", "PushButton", 0, QApplication::UnicodeUTF8));
} // retranslateUi

};

namespace Ui {
    class SplashView: public Ui_SplashView {};
} // namespace Ui

在我的 .qss 中,我已经尝试了以下方法,但都不起作用:

*#m_splashView {

背景颜色:蓝色; 这段

*#SplashView {
    background:yellow;
    background-color:blue;
}

*#Splash {
    background:yellow;
    background-color:blue;
}

*#splash {
    background:yellow;
    background-color:blue;
}

*#ui {
    background:yellow;
    background-color:blue;
}

Ui--SplashView {
    background:purple;
    background-color:blue;
}

Ui--Splash {
    background:purple;
    background-color:blue;
}

SplashView {
    background:purple;
    background-color:blue;
}

Splash {
    background:purple;
    background-color:blue;
}

代码可以工作,但它太通用了,我想专门针对 Splash 小部件:

QWidget {
    background:purple;
}

有什么想法吗?

In my .qss file I'd like to specify the background color of my widget that makes use of the generated class from the .ui file like:

#ifndef SPLASH_H
#define SPLASH_H

#include <QWidget>

#include "ui_SplashView.h"

class Splash : public QWidget
{
Q_OBJECT
public:
    explicit Splash(QWidget *parent = 0);

signals:

public slots:

private:
    Ui::SplashView ui;
};

#endif // SPLASH_H

--

#include "splash.h"

Splash::Splash(QWidget *parent) : QWidget(parent)
{
    ui.setupUi(this);
}

The ui_SplashView.h file itself looks like this:

QT_BEGIN_NAMESPACE

class Ui_SplashView
{
public:
    QPushButton *pushButton;

    void setupUi(QWidget *SplashView)
    {
    if (SplashView->objectName().isEmpty())
        SplashView->setObjectName(QString::fromUtf8("SplashView"));
    SplashView->resize(360, 640);
    pushButton = new QPushButton(SplashView);
    pushButton->setObjectName(QString::fromUtf8("pushButton"));
    pushButton->setGeometry(QRect(70, 30, 75, 23));

    retranslateUi(SplashView);

    QMetaObject::connectSlotsByName(SplashView);
} // setupUi

void retranslateUi(QWidget *SplashView)
{
    SplashView->setWindowTitle(QApplication::translate("SplashView", "Splash", 0, QApplication::UnicodeUTF8));
    pushButton->setText(QApplication::translate("SplashView", "PushButton", 0, QApplication::UnicodeUTF8));
} // retranslateUi

};

namespace Ui {
    class SplashView: public Ui_SplashView {};
} // namespace Ui

In my .qss I've tried the following, but none of them work:

*#m_splashView {

background-color:blue;
}

*#SplashView {
    background:yellow;
    background-color:blue;
}

*#Splash {
    background:yellow;
    background-color:blue;
}

*#splash {
    background:yellow;
    background-color:blue;
}

*#ui {
    background:yellow;
    background-color:blue;
}

Ui--SplashView {
    background:purple;
    background-color:blue;
}

Ui--Splash {
    background:purple;
    background-color:blue;
}

SplashView {
    background:purple;
    background-color:blue;
}

Splash {
    background:purple;
    background-color:blue;
}

This code works but it's too generic, I want to specifically target that Splash widget:

QWidget {
    background:purple;
}

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤还要旧人安 2024-08-26 08:37:39

找到了答案。自定义小部件需要实现此功能:

void Splash::paintEvent(QPaintEvent *)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

在 .qss 中我输入:

*#SplashView {
      background:red;
}

因为这是 .ui 文件中的对象名称。

Found the answer. The custom widget needs to implement this function:

void Splash::paintEvent(QPaintEvent *)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

In the .qss I put:

*#SplashView {
      background:red;
}

Since that's the object name in the .ui file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文