Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why setStylesheet isn't working for my widget?
Forum Updated to NodeBB v4.3 + New Features

Why setStylesheet isn't working for my widget?

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 141 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Kirill GusarevK Offline
    Kirill GusarevK Offline
    Kirill Gusarev
    wrote on last edited by
    #1

    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
    

    Ъуъ

    Pl45m4P 1 Reply Last reply
    0
    • Kirill GusarevK Kirill Gusarev

      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
      
      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @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 the QWidget section

      • https://doc.qt.io/qt-6/stylesheet-reference.html

      and 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.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      4
      • Kirill GusarevK Kirill Gusarev has marked this topic as solved on
      • Pl45m4P Pl45m4 referenced this topic on

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved