【问题标题】:Qt Styles: How to style a widget that uses a .ui generated class?Qt Styles:如何为使用 .ui 生成类的小部件设置样式?
【发布时间】:2010-01-27 17:59:20
【问题描述】:

在我的 .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;
}

有什么想法吗?

【问题讨论】:

    标签: qt styles themes


    【解决方案1】:

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

    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 文件中的对象名称。

    【讨论】:

      猜你喜欢
      • 2013-10-06
      • 2015-04-26
      • 2011-04-18
      • 1970-01-01
      • 2019-08-06
      • 2021-06-02
      • 2017-08-22
      • 1970-01-01
      • 2019-02-20
      相关资源
      最近更新 更多