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. When add Q_OBJECT , the styleSheet is useless.

When add Q_OBJECT , the styleSheet is useless.

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 610 Views 2 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.
  • GaoboG Offline
    GaoboG Offline
    Gaobo
    wrote on last edited by
    #1

    Hey, I have a question.
    I have a class,

    class SettingsItem : public QWidget
    {
    public:
        explicit SettingsItem(QWidget* parent = nullptr)
            : QWidget(parent)
        {
            setProperty("widgetType", "settingsItem");
        }
    
        ~SettingsItem() 
        {
    
        }
    };
    

    I add it to a qwidget

        SettingsItem* m_item = new SettingsItem();
        ui->widget_4->layout()->addWidget(m_item);
    

    I set the QAppliaction style sheet like this

        QApplication a(argc, argv);
        QFile qssFile(":/MainWindow/qss");
        QByteArray baQss;
        if (qssFile.open(QIODevice::ReadOnly)) {
            baQss = qssFile.readAll();
        }
        a.setStyleSheet(baQss);
    
        MainWindow w;
        w.show();
        return a.exec();
    

    the qss file:

    QWidget[widgetType=settingsItem]
    {
    	border: 2px solid red;
    	border-radius: 2px;
    }
    

    when running, the settingsItem's border is change
    d8edaabd-0f97-415b-a576-99036b475c04-image.png

    but when i add "Q_OBJECT" to the SettingsItem

    class SettingsItem : public QWidget
    {
        Q_OBJECT
    public:
        explicit SettingsItem(QWidget* parent = nullptr)
            : QWidget(parent)
        {
            setProperty("widgetType", "settingsItem");
        }
    
        ~SettingsItem() 
        {
    
        }
    };
    

    This border is gone.Why?9fdb6506-d8cb-4e8b-8fad-7948026fe6f6-image.png
    And why don't i add Q_OBJECT, the styleSheet is in effect.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      It's a little known quirk of the stylesheets. When you subclass QWidget and your class has a meta object you need to implement paintEvent like this:

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

      It's documented here at the end of the table, but it's easy to miss. You don't have to do that when you're subclassing any other widget like line edit or button, just with plain QWidget.

      GaoboG 1 Reply Last reply
      5
      • Chris KawaC Chris Kawa

        It's a little known quirk of the stylesheets. When you subclass QWidget and your class has a meta object you need to implement paintEvent like this:

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

        It's documented here at the end of the table, but it's easy to miss. You don't have to do that when you're subclassing any other widget like line edit or button, just with plain QWidget.

        GaoboG Offline
        GaoboG Offline
        Gaobo
        wrote on last edited by
        #3

        @Chris-Kawa said in When add Q_OBJECT , the styleSheet is useless.:

        You don't have to do that when you're subclassing any other widget like line edit or button, just with plain QWidget.

        Thank u. But i don't understand this "You don't have to do that when you're subclassing any other widget like line edit or button, just with plain QWidget." .

        Chris KawaC 1 Reply Last reply
        0
        • GaoboG Gaobo

          @Chris-Kawa said in When add Q_OBJECT , the styleSheet is useless.:

          You don't have to do that when you're subclassing any other widget like line edit or button, just with plain QWidget.

          Thank u. But i don't understand this "You don't have to do that when you're subclassing any other widget like line edit or button, just with plain QWidget." .

          Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Gaobo I mean if you have this

          class Something : public QWidget
          

          you need that custom paintEvent, but if you have for example this

          class Something : public QLineEdit //or any other widget type
          

          then you don't.

          GaoboG 2 Replies Last reply
          0
          • Chris KawaC Chris Kawa

            @Gaobo I mean if you have this

            class Something : public QWidget
            

            you need that custom paintEvent, but if you have for example this

            class Something : public QLineEdit //or any other widget type
            

            then you don't.

            GaoboG Offline
            GaoboG Offline
            Gaobo
            wrote on last edited by
            #5

            @Chris-Kawa
            Oh, thank u. i understand. It's mean, if havn't a meta object, i don't need to custom paintEvent, right?

            1 Reply Last reply
            0
            • Chris KawaC Chris Kawa

              @Gaobo I mean if you have this

              class Something : public QWidget
              

              you need that custom paintEvent, but if you have for example this

              class Something : public QLineEdit //or any other widget type
              

              then you don't.

              GaoboG Offline
              GaoboG Offline
              Gaobo
              wrote on last edited by
              #6

              @Chris-Kawa
              Oh, I think the reason for that the styleSheet is in effect when i don't add Q_OBJECT is:

              1. when i not add Q_OBJECT, the "settingsItem" don't have parent, because it don't have Q_OBJECT, so ,the "settingsItem" background is itself background.
              2. when i add Q_OBJECT, and not "setAttribute(Qt::WA_StyledBackground)", it's background is the background of its parent object.
              3. the "WA_StyledBackground" is specify that it's background determined by style sheet.
              1 Reply Last reply
              0

              • Login

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