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++ 命名空间中(我的错误我错过了)。所以我必须写MyNamespace--CDerived在qss中。请参阅“ C++ 命名空间中的小部件

在这里尝试过JK的简单示例后,我突然意识到自己的错误!

正确一:

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

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

4

1 回答 1

0

很奇怪……对我来说效果很好:

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

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

我的小部件.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();
}
于 2014-10-07T08:43:10.587 回答