2010-01-27 38 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文件對象名稱。

相關問題