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 Inherit QWidget cant set background color If use Q_OBJECT In Qt6.5
QtWS25 Last Chance

Why Inherit QWidget cant set background color If use Q_OBJECT In Qt6.5

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 592 Views
  • 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.
  • Q Offline
    Q Offline
    Quiccz
    wrote on last edited by
    #1

    If i use Q_OBJECT in InheritWidget, i can see fe944378-52de-482a-b910-7aaeb5aadb07-image.png

    if i dont use it, i can see
    27f00a89-ecb1-434f-884f-aaadfb606997-image.png

    Here is my code, I dont understand why my inherit widget cant set background color.

    #include <QWidget>
    #include "Mywidget.h"
    #include <QApplication>
    
    
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        MyWidget *mywidget = new MyWidget(nullptr);
        
        mywidget->SetSubWidget();
        mywidget->show();
        return app.exec();;
    }
    
    #pragma once
    #include <QWidget>
    
    class InheritWidget : public QWidget
    {
    Q_OBJECT
    public:
    	InheritWidget(QWidget* parent);
    };
    
    #include "InheritWidget.h"
    
    InheritWidget::InheritWidget(QWidget* parent)
    	:QWidget(parent)
    {
    
    }
    
    #pragma once
    #include <QWidget>
    
    class MyWidget: public QWidget
    {
    public:
        MyWidget(QWidget *parent);
        void SetSubWidget();
    };
    
    #include "Mywidget.h"
    #include "InheritWidget.h"
    MyWidget::MyWidget(QWidget* parent)
    : QWidget(parent)
    {
        this->setGeometry(0,0, 300, 300);
        this->setWindowFlags(Qt::FramelessWindowHint);
        this->setStyleSheet("background-color:black;");
        this->setAutoFillBackground(true);
    }
    
    void MyWidget::SetSubWidget()
    {
        InheritWidget*subWidget = new InheritWidget(this);
        subWidget->setGeometry(100, 100, 100, 100);
        subWidget->setStyleSheet("background-color:yellow;");
        subWidget->setAutoFillBackground(true);
        subWidget->show();
    
        QWidget*ssubWidget = new QWidget(this);
        ssubWidget->setGeometry(0, 0, 100, 100);
        ssubWidget->setStyleSheet("background-color:green;");
        ssubWidget->setAutoFillBackground(true);
        ssubWidget->show();
    }
    
    Christian EhrlicherC 1 Reply Last reply
    0
    • Q Quiccz

      If i use Q_OBJECT in InheritWidget, i can see fe944378-52de-482a-b910-7aaeb5aadb07-image.png

      if i dont use it, i can see
      27f00a89-ecb1-434f-884f-aaadfb606997-image.png

      Here is my code, I dont understand why my inherit widget cant set background color.

      #include <QWidget>
      #include "Mywidget.h"
      #include <QApplication>
      
      
      
      int main(int argc, char *argv[])
      {
          QApplication app(argc, argv);
      
          MyWidget *mywidget = new MyWidget(nullptr);
          
          mywidget->SetSubWidget();
          mywidget->show();
          return app.exec();;
      }
      
      #pragma once
      #include <QWidget>
      
      class InheritWidget : public QWidget
      {
      Q_OBJECT
      public:
      	InheritWidget(QWidget* parent);
      };
      
      #include "InheritWidget.h"
      
      InheritWidget::InheritWidget(QWidget* parent)
      	:QWidget(parent)
      {
      
      }
      
      #pragma once
      #include <QWidget>
      
      class MyWidget: public QWidget
      {
      public:
          MyWidget(QWidget *parent);
          void SetSubWidget();
      };
      
      #include "Mywidget.h"
      #include "InheritWidget.h"
      MyWidget::MyWidget(QWidget* parent)
      : QWidget(parent)
      {
          this->setGeometry(0,0, 300, 300);
          this->setWindowFlags(Qt::FramelessWindowHint);
          this->setStyleSheet("background-color:black;");
          this->setAutoFillBackground(true);
      }
      
      void MyWidget::SetSubWidget()
      {
          InheritWidget*subWidget = new InheritWidget(this);
          subWidget->setGeometry(100, 100, 100, 100);
          subWidget->setStyleSheet("background-color:yellow;");
          subWidget->setAutoFillBackground(true);
          subWidget->show();
      
          QWidget*ssubWidget = new QWidget(this);
          ssubWidget->setGeometry(0, 0, 100, 100);
          ssubWidget->setStyleSheet("background-color:green;");
          ssubWidget->setAutoFillBackground(true);
          ssubWidget->show();
      }
      
      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      Because style sheets are propagated to the children (if the children a correct QMetaObjects).

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      Q 1 Reply Last reply
      4
      • Christian EhrlicherC Christian Ehrlicher

        Because style sheets are propagated to the children (if the children a correct QMetaObjects).

        Q Offline
        Q Offline
        Quiccz
        wrote on last edited by
        #3

        @Christian-Ehrlicher Is there a way to set the background color of a child widget?

        Christian EhrlicherC 1 Reply Last reply
        0
        • Q Quiccz

          @Christian-Ehrlicher Is there a way to set the background color of a child widget?

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Quiccz said in Why Inherit QWidget cant set background color If use Q_OBJECT In Qt6.5:

          Is there a way to set the background color of a child widget?

          If you use css on the parent then you have to set the background color of the child also with css.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          Q 1 Reply Last reply
          0
          • JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @Quiccz
            Not sure exactly why, and not sure if you are saying this behaviour is to do with specific 6.5 version.

            One thing: you are inheriting from QWidget and you use stylesheet with setAutoFillBackground

            Warning: Use this property with caution in conjunction with Qt Style Sheets. When a widget has a style sheet with a valid background or a border-image, this property is automatically disabled.

            The other thing comes from How to Change the Background Color of QWidget

            Note: If you subclass a custom widget from QWidget, then in order to use the StyleSheets you need to provide a paintEvent to the custom widget :

            Only @Christian-Ehrlicher knows how/whether this applies here....

            1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @Quiccz said in Why Inherit QWidget cant set background color If use Q_OBJECT In Qt6.5:

              Is there a way to set the background color of a child widget?

              If you use css on the parent then you have to set the background color of the child also with css.

              Q Offline
              Q Offline
              Quiccz
              wrote on last edited by
              #6

              @Christian-Ehrlicher Thanks, i use QPalette works

              JoeCFDJ 1 Reply Last reply
              0
              • Q Quiccz

                @Christian-Ehrlicher Thanks, i use QPalette works

                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by JoeCFD
                #7

                @Quiccz Or your class inherits QLabel or QFrame and you can simply set background with stylesheet.

                JonBJ 1 Reply Last reply
                0
                • JoeCFDJ JoeCFD

                  @Quiccz Or your class inherits QLabel or QFrame and you can simply set background with stylesheet.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @JoeCFD
                  But the OP's does no, it derives directly from QWidget. Hence I don't know how they manage without overriding the paintEvent(), per my previous post, because I recently found I did have to do that.

                  JoeCFDJ 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @JoeCFD
                    But the OP's does no, it derives directly from QWidget. Hence I don't know how they manage without overriding the paintEvent(), per my previous post, because I recently found I did have to do that.

                    JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by
                    #9

                    @JonB https://codebrowser.dev/qt5/qtbase/src/widgets/kernel/qwidget.cpp.html
                    it is empty
                    void QWidget::paintEvent(QPaintEvent *)
                    {
                    }

                    JonBJ 1 Reply Last reply
                    0
                    • JoeCFDJ JoeCFD

                      @JonB https://codebrowser.dev/qt5/qtbase/src/widgets/kernel/qwidget.cpp.html
                      it is empty
                      void QWidget::paintEvent(QPaintEvent *)
                      {
                      }

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #10

                      @JoeCFD
                      I know that. The issue is from the Qt wiki, https://wiki.qt.io/How_to_Change_the_Background_Color_of_QWidget, Using Style Sheet sub-heading:

                      Note: If you subclass a custom widget from QWidget, then in order to use the StyleSheets you need to provide a paintEvent to the custom widget :

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

                      And (at least I thought) I recall having to do this, recently. I don't know how come the OP does not need same. There you are.

                      1 Reply Last reply
                      0
                      • Q Quiccz referenced this topic on
                      • Q Quiccz has marked this topic as solved on

                      • Login

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