2014-10-05 5 views
4

전역 qss 스타일 시트를 파생 클래스와 함께 사용하고 싶습니다. paintEvent (style sheet reference 또는 here)을 재정의해야한다는 것을 알고 있습니다.C++ 네임 스페이스 (선택자)의 파생 클래스에있는 Qt 스타일 시트

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

그러나 작동하지 않는 것 같습니다. CDerived:QWidget 및 다음 스타일 시트 라인으로 내가 직면

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

CDerived 위와 같이 paintEvent을 구현합니다. 내가해야 할 일은 뭐니?

- 편집/솔루션 - 내가 그것을 알아 낸 JK의 힌트에

감사합니다. 위의 예는 실제로 내 시나리오를 올바르게 반영하지 못합니다. 내 실제 클래스는 C++ 네임 스페이스에 있습니다 (실수를 저도 생각했습니다). 그래서 MyNamespace--CDerived을 qss에 써야합니다. "Widgets inside C++ namespaces"

여기서 JK의 간단한 예제를 시도한 후에 갑자기 실수를 깨달았습니다. 올바른

:

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

비고 : SO 질문 (a, b)를 Relateds하지만,이 특정 질문에 대한 대답과 함께. 내 파생 클래스는 C++ 네임 스페이스에 있습니다.

+1

모르겠어요 경우 그 날, 나는 있지만, 여기에서'opt.init()'를 찾을 수 없습니다 : http://qt-project.org/doc/qt-5/qstyleoption.html – msrd0

+1

또한보십시오 Qt StyleSheets를 사용하는 경우 : http://qt-project.org/doc/qt-5/stylesheet-examples.html – msrd0

답변

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

주 윈도우 .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() 함수를 "주석 처리"하면 붉은 색이 제거됩니까? 나중에 나는 좋은 생각처럼 간단한 예제를 테스트 할 것이다. –

+0

@HorstWalter 페인트 이벤트를 주석 처리하면 붉은 색이 제거됩니다. 시도했습니다. –

+2

감사합니다. 힌트를 주셔서 감사합니다. 그것은 네임 스페이스 문제였습니다. 나는 그 클래스가 네임 스페이스에 있고 b) "::"가 "-"로 바뀌어야한다는 것을 놓쳤습니다. –