C++命名空间中的派生类(选择器)中的Qt样式表

5

我希望能够在派生类中使用我的全局qss样式表。我了解到我需要重写paintEvent样式表参考,或这里)。

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++命名空间中(我的错误是我漏掉了这一点)。因此,在qss中必须写成MyNamespace--CDerived。请参见“C++命名空间内的小部件

在我尝试了JK在这里提供的简单示例之后,我突然意识到了我的错误!

正确的:

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

备注:相关的SO问题(a, b),但没有回答这个特定的问题。我的派生类位于C++命名空间中。


1
我不知道是不是我的问题,但在这里http://qt-project.org/doc/qt-5/qstyleoption.html中我找不到`opt.init()`。 - msrd0
1
另请参见使用Qt样式表的示例:http://qt-project.org/doc/qt-5/stylesheet-examples.html - msrd0
1个回答

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

是的,很奇怪,我正在Win 7上测试,使用Qt 5.3。不过,你能做一个简短的交叉检查吗?如果你将MyWidget :: paintEvent()函数“注释掉”,它会去除红色吗?今天晚些时候我也会测试这样一个简单的例子,好主意。 - Horst Walter
@HorstWalter 是的,我试过了,如果你注释掉paintEvent,红色就会被移除。 - Jacob Krieg
2
感谢您的提示,我已经解决了问题。这是一个命名空间问题。我错过了以下两点:a)我的类在一个命名空间中;b)"::"必须替换为"--"。 - Horst Walter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接