Why setStylesheet isn't working for my widget?
-
I wrote my own widget (MyWidget class), inherited from QWidget. In the class I described only the overridden virtual function mousePressEvent, which prints debugging information to the console when the mouse is clicked on the widget.
An instance of MyWidget is created in the MainWindow constructor. There, CSS is applied to the MyWidget widget, which should paint the background of the widget red... But nothing is painted over. Why?
If instead of MyWidget you create an instance of the QWidget class, then setStyleSheet perfectly paints the widget red.mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: Ui::MainWindow *ui; //protected: // virtual void mousePressEvent(QMouseEvent *event); }; class MyWidget : public QWidget { Q_OBJECT public: MyWidget(QWidget *parent = nullptr); ~MyWidget(); protected: virtual void mousePressEvent(QMouseEvent *event); }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); MyWidget *widget = new MyWidget(this); widget->setStyleSheet("background-color: red;"); } MainWindow::~MainWindow() { delete ui; } //void MainWindow::mousePressEvent(QMouseEvent *event) //{ // qDebug() << "MainWindow::mousePressEvent"; //} MyWidget::MyWidget(QWidget *parent) : QWidget(parent) { } MyWidget::~MyWidget() { } void MyWidget::mousePressEvent(QMouseEvent *event) { qDebug() << "MyWidget::mousePressEvent"; }
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>791</width> <height>457</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralwidget"> <widget class="QPushButton" name="pushButton_rectangle"> <property name="geometry"> <rect> <x>10</x> <y>10</y> <width>101</width> <height>24</height> </rect> </property> <property name="text"> <string>Прямоугольник</string> </property> </widget> <widget class="QPushButton" name="pushButton_triangle"> <property name="geometry"> <rect> <x>120</x> <y>10</y> <width>81</width> <height>24</height> </rect> </property> <property name="text"> <string>Треугольник</string> </property> </widget> <widget class="QPushButton" name="pushButton_ellipse"> <property name="geometry"> <rect> <x>210</x> <y>10</y> <width>71</width> <height>24</height> </rect> </property> <property name="text"> <string>Эллипс</string> </property> </widget> <widget class="QPushButton" name="pushButton_connection"> <property name="geometry"> <rect> <x>310</x> <y>10</y> <width>71</width> <height>24</height> </rect> </property> <property name="text"> <string>Связь</string> </property> </widget> <widget class="QPushButton" name="pushButton_move"> <property name="geometry"> <rect> <x>410</x> <y>10</y> <width>91</width> <height>24</height> </rect> </property> <property name="text"> <string>Переместить</string> </property> </widget> <widget class="QPushButton" name="pushButton_delete"> <property name="geometry"> <rect> <x>510</x> <y>10</y> <width>71</width> <height>24</height> </rect> </property> <property name="text"> <string>Удалить</string> </property> </widget> <widget class="QPushButton" name="pushButton_load"> <property name="geometry"> <rect> <x>630</x> <y>10</y> <width>71</width> <height>24</height> </rect> </property> <property name="text"> <string>Загрузить</string> </property> </widget> <widget class="QPushButton" name="pushButton_save"> <property name="geometry"> <rect> <x>710</x> <y>10</y> <width>71</width> <height>24</height> </rect> </property> <property name="text"> <string>Сохранить</string> </property> </widget> <widget class="QGraphicsView" name="graphicsView_canvas"> <property name="geometry"> <rect> <x>10</x> <y>50</y> <width>771</width> <height>361</height> </rect> </property> </widget> </widget> <widget class="QMenuBar" name="menubar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>791</width> <height>22</height> </rect> </property> </widget> <widget class="QStatusBar" name="statusbar"/> </widget> <resources/> <connections/> </ui>
main.cpp
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
TESTDELETE16123.pro
QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++17 # You can make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ main.cpp \ mainwindow.cpp HEADERS += \ mainwindow.h FORMS += \ mainwindow.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target
-
I wrote my own widget (MyWidget class), inherited from QWidget. In the class I described only the overridden virtual function mousePressEvent, which prints debugging information to the console when the mouse is clicked on the widget.
An instance of MyWidget is created in the MainWindow constructor. There, CSS is applied to the MyWidget widget, which should paint the background of the widget red... But nothing is painted over. Why?
If instead of MyWidget you create an instance of the QWidget class, then setStyleSheet perfectly paints the widget red.mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: Ui::MainWindow *ui; //protected: // virtual void mousePressEvent(QMouseEvent *event); }; class MyWidget : public QWidget { Q_OBJECT public: MyWidget(QWidget *parent = nullptr); ~MyWidget(); protected: virtual void mousePressEvent(QMouseEvent *event); }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); MyWidget *widget = new MyWidget(this); widget->setStyleSheet("background-color: red;"); } MainWindow::~MainWindow() { delete ui; } //void MainWindow::mousePressEvent(QMouseEvent *event) //{ // qDebug() << "MainWindow::mousePressEvent"; //} MyWidget::MyWidget(QWidget *parent) : QWidget(parent) { } MyWidget::~MyWidget() { } void MyWidget::mousePressEvent(QMouseEvent *event) { qDebug() << "MyWidget::mousePressEvent"; }
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>791</width> <height>457</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralwidget"> <widget class="QPushButton" name="pushButton_rectangle"> <property name="geometry"> <rect> <x>10</x> <y>10</y> <width>101</width> <height>24</height> </rect> </property> <property name="text"> <string>Прямоугольник</string> </property> </widget> <widget class="QPushButton" name="pushButton_triangle"> <property name="geometry"> <rect> <x>120</x> <y>10</y> <width>81</width> <height>24</height> </rect> </property> <property name="text"> <string>Треугольник</string> </property> </widget> <widget class="QPushButton" name="pushButton_ellipse"> <property name="geometry"> <rect> <x>210</x> <y>10</y> <width>71</width> <height>24</height> </rect> </property> <property name="text"> <string>Эллипс</string> </property> </widget> <widget class="QPushButton" name="pushButton_connection"> <property name="geometry"> <rect> <x>310</x> <y>10</y> <width>71</width> <height>24</height> </rect> </property> <property name="text"> <string>Связь</string> </property> </widget> <widget class="QPushButton" name="pushButton_move"> <property name="geometry"> <rect> <x>410</x> <y>10</y> <width>91</width> <height>24</height> </rect> </property> <property name="text"> <string>Переместить</string> </property> </widget> <widget class="QPushButton" name="pushButton_delete"> <property name="geometry"> <rect> <x>510</x> <y>10</y> <width>71</width> <height>24</height> </rect> </property> <property name="text"> <string>Удалить</string> </property> </widget> <widget class="QPushButton" name="pushButton_load"> <property name="geometry"> <rect> <x>630</x> <y>10</y> <width>71</width> <height>24</height> </rect> </property> <property name="text"> <string>Загрузить</string> </property> </widget> <widget class="QPushButton" name="pushButton_save"> <property name="geometry"> <rect> <x>710</x> <y>10</y> <width>71</width> <height>24</height> </rect> </property> <property name="text"> <string>Сохранить</string> </property> </widget> <widget class="QGraphicsView" name="graphicsView_canvas"> <property name="geometry"> <rect> <x>10</x> <y>50</y> <width>771</width> <height>361</height> </rect> </property> </widget> </widget> <widget class="QMenuBar" name="menubar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>791</width> <height>22</height> </rect> </property> </widget> <widget class="QStatusBar" name="statusbar"/> </widget> <resources/> <connections/> </ui>
main.cpp
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
TESTDELETE16123.pro
QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++17 # You can make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ main.cpp \ mainwindow.cpp HEADERS += \ mainwindow.h FORMS += \ mainwindow.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target
@Kirill-Gusarev said in Why setStylesheet isn't working for my widget?:
But nothing is painted over. Why?
You need to implement the
paintEvent
handler in your custom widget class, as described here in theQWidget
sectionand then add this code:
QStyleOption opt; opt.initFrom(this); QPainter p(this); style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
Otherwise it can't process the StyleSheet.
-
K Kirill Gusarev has marked this topic as solved on
-
P Pl45m4 referenced this topic on