0

In my main I try to set the stylesheet for my mainwidget (Subclass of QWidget). But unfortunately it does not apply. With qApp it works. Whats the difference? In the docs I cant find any.

ManuelSchneid3r
  • 15,850
  • 12
  • 65
  • 103

1 Answers1

0

You need to implement paintEvent in order for QSS styles to work in QWidget derived classes:

void mainwidget::paintEvent(QPaintEvent *)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

I don't think it works if you set the stylesheet on qApp, I did a test and it doesn't:

main.cpp:

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

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    qApp->setStyleSheet("QWidget{ background-color: red; } MyCustomWidget { background-color: blue; } ");
    Widget w;
    w.show();

    return a.exec();
}

mycustomwidget.h:

#ifndef MYCUSTOMWIDGET_H
#define MYCUSTOMWIDGET_H

#include <QWidget>
#include <QPaintEvent>
#include <QPainter>
class MyCustomWidget : public QWidget
{
    Q_OBJECT
public:
    explicit MyCustomWidget(QWidget *parent = 0);

signals:

public slots:

protected:
//    void paintEvent(QPaintEvent *e);

};

#endif // MYCUSTOMWIDGET_H

mycustomwidget.cpp:

#include "mycustomwidget.h"

#include <QStyleOption>

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

//void MyCustomWidget::paintEvent(QPaintEvent *)
//{
//    QStyleOption opt;
//    opt.init(this);
//    QPainter p(this);
//    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
//}

widget.h:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

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

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

widget.cpp:

#include "widget.h"
#include "ui_widget.h"

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

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

widget.ui:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Widget</class>
 <widget class="QWidget" name="Widget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>465</width>
    <height>392</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Widget</string>
  </property>
  <layout class="QGridLayout" name="gridLayout">
   <item row="0" column="1">
    <widget class="QWidget" name="widget_2" native="true">
     <property name="styleSheet">
      <string notr="true"/>
     </property>
    </widget>
   </item>
   <item row="0" column="2">
    <widget class="MyCustomWidget" name="widget" native="true">
     <property name="styleSheet">
      <string notr="true"/>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <customwidgets>
  <customwidget>
   <class>MyCustomWidget</class>
   <extends>QWidget</extends>
   <header>mycustomwidget.h</header>
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

Result:

enter image description here

Uncommenting the commented code generates the following result:

enter image description here

So I don't see how it works for you...maybe the problem is somewhere else, so show us the code.

Iuliu
  • 4,001
  • 19
  • 31