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? A problem about Q_OBJECT and setStyleSheet().
QtWS25 Last Chance

Why? A problem about Q_OBJECT and setStyleSheet().

Scheduled Pinned Locked Moved Solved General and Desktop
qobjectsetstylesheetstyesheet
5 Posts 2 Posters 228 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.
  • J Offline
    J Offline
    JaderoChan
    wrote on last edited by
    #1

    A tiny code example follow:

    //main.cpp
    #include "mainwidget.h"
    #include <QtWidgets/QApplication>
    
    int main(int argc, char* argv[])
    {
        QApplication a(argc, argv);
        MainWidget w;
        w.show();
        return a.exec();
    }
    
    // mainwidget.h
    #pragma once
    
    #include <qwidget.h>
    
    #define USE_MASK_WIDGET_BY_CLASS
    
    class MaskWidget : public QWidget
    {
        Q_OBJECT
    
    public:
        MaskWidget(QWidget* parent = nullptr) :
            QWidget(parent)
        {
            setStyleSheet("background-color:#88000000;");
            if (parentWidget())
            {
                QRect rect = parentWidget()->geometry();
                rect.setX(0);
                rect.setY(0);
                setGeometry(rect);
            }
    
            show();
        }
    
        ~MaskWidget() {}
    };
    
    class MainWidget : public QWidget
    {
        Q_OBJECT
    
    public:
        MainWidget(QWidget* parent = nullptr);
        ~MainWidget();
    
    protected slots:
        void onButtonClicked();
    };
    
    // mainwidget.cpp
    #include "mainwidget.h"
    
    #include <qpushbutton.h>
    
    static QWidget* getMaskWidget(QWidget* parent = nullptr)
    {
        auto wgt = new QWidget(parent);
    
        wgt->setStyleSheet("background-color:#88000000;");
        if (wgt->parentWidget())
        {
            QRect rect = wgt->parentWidget()->geometry();
            rect.setX(0);
            rect.setY(0);
            wgt->setGeometry(rect);
        }
    
        return wgt;
    }
    
    MainWidget::MainWidget(QWidget* parent) :
        QWidget(parent)
    {
        setFixedSize(600, 400);
    
        auto getBtn = new QPushButton("Set mask widget", this);
        connect(getBtn, &QPushButton::clicked, this, &MainWidget::onButtonClicked);
    }
    
    MainWidget::~MainWidget()
    {}
    
    void MainWidget::onButtonClicked()
    {
    #ifdef USE_MASK_WIDGET_BY_CLASS
        auto wgt = new MaskWidget(this);
    #else
        auto wgt = getMaskWidget(this);
    #endif
        wgt->show();
    }
    

    I want to implement a simple mask widget.
    You can try above code, you‘ll observe the mask not effect when define the USE_MASK_WIDGET_BY_CLASS macro. And i observe the key of this problem is the Q_OBJECT macro, while you define it at MaskWidget class , the mask will not effect, and has effect if you not define it.

    So, why? Will the Q_OBJECT affect the setStyleSheet() fucntion?

    If you can clear my mind, thanks to much.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #2

      Hi and welcome to devnet,

      You have to provide an implementation of paintEvent and use the Q_OBJECT macro when styling a custom widget like you do.

      You have the code in the stylesheet reference under QWidget. It's juste above the list of properties part.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi and welcome to devnet,

        You have to provide an implementation of paintEvent and use the Q_OBJECT macro when styling a custom widget like you do.

        You have the code in the stylesheet reference under QWidget. It's juste above the list of properties part.

        J Offline
        J Offline
        JaderoChan
        wrote on last edited by
        #3

        @SGaist Thank you, it work. And is all widget that use Q_OBJECT and setStyleSheet custom widget need override the paintEvent?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I am not sure to understand your phrase correctly.

          So the rule is: if you create a QWidget subclass where you want to use style sheets, then it needs both the Q_OBJECT macro and the paintEvent reimplementation.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 1 Reply Last reply
          1
          • SGaistS SGaist

            I am not sure to understand your phrase correctly.

            So the rule is: if you create a QWidget subclass where you want to use style sheets, then it needs both the Q_OBJECT macro and the paintEvent reimplementation.

            J Offline
            J Offline
            JaderoChan
            wrote on last edited by
            #5

            @SGaist Thank you, I get it.

            1 Reply Last reply
            0
            • J JaderoChan 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