开发者

Qt Styles: How to style a widget that uses a .ui generated class?

开发者 https://www.devze.com 2022-12-18 02:22 出处:网络
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:

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消