2014-10-05 32 views
4

我想用我的全球qss樣式與派生類。我知道我必須重寫paintEventstyle sheet referencehere)。Qt的樣式表在派生類在C++的命名空間(選擇)

void CustomWidget::paintEvent(QPaintEvent *) { 
    QStyleOption opt; 
    opt.init(this); // tried initFrom too, same result=>not working 
    QPainter p(this); 
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); 
} 

但是,它似乎沒有工作。與​​和下面的樣式表線I面對:

CDerived { background-color: black; } // no effect 
QWidget { background-color: black; } // works 

CDerived如上實現paintEvent。還有什麼我需要做的?

- 編輯/解決方案 -

感謝JK的提示我已經想通了。我上面的例子其實並不正確地反映我的情況。 我的真實課程駐留在一個C++名稱空間中(我的錯誤我錯過了)。所以我必須在qss中寫MyNamespace--CDerived。請參閱「Widgets inside C++ namespaces

後,我在這裏試圖JK的簡單的例子,我突然意識到我的錯誤!

正確的:

MyNamespace--CDerived { background-color: black; } // works, use -- for :: 

備註:SO Relateds問題(ab),但沒有回答這個特定問題。我的派生類駐留在C++名稱空間中。

+1

我不知道,如果它的我,但我在這裏找不到'opt.init()':http://qt-project.org/doc/qt-5/qstyleoption.html – msrd0 2014-10-06 19:15:04

+1

另見例子在這裏使用Qt StyleSheets:http://qt-project.org/doc/qt-5/stylesheet-examples.html – msrd0 2014-10-06 19:16:25

回答

0

令人奇怪的是....正常工作對我來說:

untitled.pro:

#------------------------------------------------- 
# 
# Project created by QtCreator 2014-10-07T11:34:54 
# 
#------------------------------------------------- 

QT  += core gui 

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 

TARGET = untitled 
TEMPLATE = app 


SOURCES += main.cpp\ 
     mainwindow.cpp \ 
    mywidget.cpp 

HEADERS += mainwindow.h \ 
    mywidget.h 

FORMS += mainwindow.ui 

mainwindow.h:

#ifndef MAINWINDOW_H 
#define MAINWINDOW_H 

#include <QMainWindow> 

namespace Ui { 
class MainWindow; 
} 

class MainWindow : public QMainWindow 
{ 
    Q_OBJECT 

public: 
    explicit MainWindow(QWidget *parent = 0); 
    ~MainWindow(); 

private: 
    Ui::MainWindow *ui; 
}; 

#endif // MAINWINDOW_H 

mainwindow.cpp:

#include "mainwindow.h" 
#include "ui_mainwindow.h" 

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 
} 

MainWindow::~MainWindow() 
{ 
    delete ui; 
} 

mainwindow.ui:

<?xml version="1.0" encoding="UTF-8"?> 
<ui version="4.0"> 
<class>MainWindow</class> 
<widget class="QMainWindow" name="MainWindow"> 
    <property name="geometry"> 
    <rect> 
    <x>0</x> 
    <y>0</y> 
    <width>400</width> 
    <height>300</height> 
    </rect> 
    </property> 
    <property name="windowTitle"> 
    <string>MainWindow</string> 
    </property> 
    <property name="styleSheet"> 
    <string notr="true"/> 
    </property> 
    <widget class="QWidget" name="centralWidget"> 
    <widget class="MyWidget" name="widget" native="true"> 
    <property name="geometry"> 
    <rect> 
     <x>70</x> 
     <y>30</y> 
     <width>201</width> 
     <height>121</height> 
    </rect> 
    </property> 
    <property name="styleSheet"> 
    <string notr="true"/> 
    </property> 
    <widget class="QPushButton" name="pushButton"> 
    <property name="geometry"> 
     <rect> 
     <x>30</x> 
     <y>20</y> 
     <width>75</width> 
     <height>23</height> 
     </rect> 
    </property> 
    <property name="text"> 
     <string>PushButton</string> 
    </property> 
    </widget> 
    </widget> 
    </widget> 
    <widget class="QMenuBar" name="menuBar"> 
    <property name="geometry"> 
    <rect> 
    <x>0</x> 
    <y>0</y> 
    <width>400</width> 
    <height>21</height> 
    </rect> 
    </property> 
    </widget> 
    <widget class="QToolBar" name="mainToolBar"> 
    <attribute name="toolBarArea"> 
    <enum>TopToolBarArea</enum> 
    </attribute> 
    <attribute name="toolBarBreak"> 
    <bool>false</bool> 
    </attribute> 
    </widget> 
    <widget class="QStatusBar" name="statusBar"/> 
</widget> 
<layoutdefault spacing="6" margin="11"/> 
<customwidgets> 
    <customwidget> 
    <class>MyWidget</class> 
    <extends>QWidget</extends> 
    <header>mywidget.h</header> 
    <container>1</container> 
    </customwidget> 
</customwidgets> 
<resources/> 
<connections/> 
</ui> 

mywidget.h:

#ifndef MYWIDGET_H 
#define MYWIDGET_H 

#include <QWidget> 

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

protected: 
    void paintEvent(QPaintEvent *e); 

}; 

#endif // MYWIDGET_H 

mywidget.cpp:

#include "mywidget.h" 

#include <QStyleOption> 
#include <QPainter> 

MyWidget::MyWidget(QWidget *parent) : 
    QWidget(parent) 
{ 
} 

void MyWidget::paintEvent(QPaintEvent *e) 
{ 
    Q_UNUSED(e) 

    QStyleOption opt; 
    opt.init(this); // tried initFrom too, same result=>not working 
    QPainter p(this); 
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); 
} 

main.cpp中:

#include "mainwindow.h" 
#include <QApplication> 

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 

    a.setStyleSheet("MyWidget { background-color: red; }"); 

    MainWindow w; 
    w.show(); 

    return a.exec(); 
} 
+0

是的奇怪,我正在測試Win 7,Qt 5.3。但是,你可以做一個簡短的交叉檢查。如果「註釋掉」MyWidget :: paintEvent()函數,它會刪除紅色嗎?今天晚些時候我會測試一個簡單的例子,好主意。 – 2014-10-07 10:19:46

+0

@HorstWalter是的,我試過了,如果你註釋掉paintEvent,那麼紅色將被刪除 – 2014-10-07 10:21:40

+2

感謝您的好提示我已經弄明白了。這是一個命名空間問題。我錯過了a)我的課程在命名空間中,b)「::」必須替換爲「 - 」。 – 2014-10-07 18:40:31