2010-01-27 45 views
0

在我.qss文件,我想我的指定窗口小部件的背景颜色,使得使用生成的类从的.ui文件,如:Qt样式:如何样式化使用.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 { 

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; 
} 

此代码的作品,但它太普通,我要明确指定飞溅小部件:

QWidget { 
    background:purple; 
} 

任何想法?

回答

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