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

标签 c++ qtstylesheets qt5.3

我想将全局 qss 样式表与派生类一起使用。我知道我必须覆盖 paintEvent(style sheet referencehere)。

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 的提示,我已经弄明白了。我上面的例子实际上没有正确反射(reflect)我的场景。 我的真实类位于 C++ 命名空间中(我的错误是我错过了它)。 所以我必须在 qss 中编写 MyNamespace--CDerived。参见“Widgets inside C++ namespaces

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

正确的一个:

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

备注:相关 SO 问题(ab),但没有对此特定问题的答案。我的派生类驻留在 C++ namespace 中。

最佳答案

这很奇怪....它对我来说很好用:

无标题.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

主窗口.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

主窗口.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>

我的部件.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

我的小部件.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);
}

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

关于c++ - C++ 命名空间中的派生类中的 Qt 样式表(选择器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26206492/

相关文章:

C++ Vector,从另一个线程崩溃的 push_back?

python - boost::python:如何覆盖静态属性?

qt - QScrollArea清除背景颜色

c++ - C++中的快速排序

c++ - 身份矩阵/零矩阵 : do they allocate?

qt - QDockWidget float /关闭按钮悬停图像

c++ - 左填充在 QListWidget 中不起作用

c++ - qt 如何使用按钮动态添加包含一些小部件的组框?

qt - 如何查看 QTest 结果