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. Correct style aware custom widgets don´t get the style applied anyway
Forum Updated to NodeBB v4.3 + New Features

Correct style aware custom widgets don´t get the style applied anyway

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 2.6k 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.
  • D Offline
    D Offline
    darkgaze
    wrote on last edited by
    #1

    It always worked until now. I don´t know what's going on but i got this code of a QPushButton customized:

    @
    class SmartModeButton : public QPushButton
    {

    private:

    public:

    SmartModeButton( ... );

    protected:

    void paintEvent(QPaintEvent *);

    };
    @

    And i have this method

    @

    SmartModeButton::SmartModeButton( ... ): QPushButton(parent_)
    {
    (...)

    setWindowFlags( Qt::Window | Qt::FramelessWindowHint );
    setAttribute(Qt::WA_QuitOnClose, true);
    setAttribute( Qt::WA_TranslucentBackground );

    }

    (...)

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

    Adding this makes it work:

    setStyleSheed("background-color:green");

    But using a stylesheet as follows does nothing. It doesn´t understand the name of the widget, but i don´t get any errors.

    @
    /* ------------------ MODE BUTTONS ------------------------------------------*/

    QPushButton
    { background-color: white; }

    SmartModeButton
    { background-color: green; }
    @

    It still shows a white background. So it understand that it´s a button... but not a SmartModeButton.

    Why?

    The funny thing, it used to work. I don´t know what i changed, but it has no sense for me.

    Thanks a lot.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sam
      wrote on last edited by
      #2

      For your SmartModeButton you can set an objectName

      @SmartModeButton->setObjectName("SmartModeButton");@

      and then in yout css file you can specify

      @QPushButton#SmartModeButton
      {
      background-color: green;
      }@

      1 Reply Last reply
      0
      • D Offline
        D Offline
        darkgaze
        wrote on last edited by
        #3

        Yeah. That´s an idea.

        BUT, and it´s a big BUT... if i do that, i cannot distinguish the different SmartModeButtons between them.

        I mean:

        I have 4 buttons from that class. Then i set the name for each of them so i can set :

        SmartModeButton#name1
        SmartModeButton#name2
        SmartModeButton#name3
        SmartModeButton#name4

        Because i need different backgrounds for them, but the same behaviour. But by doing that, then all buttons are the same.

        How would you do that?. And the big question: why did it stopped working??

        If you have a custom class the stylesheet should work with it´s name just adding the paintEvent that way.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          darkgaze
          wrote on last edited by
          #4

          Any answers please?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Sam
            wrote on last edited by
            #5

            For each instance of SmartModeButton you can set the objectName() and then accordingly you can change the background.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              darkgaze
              wrote on last edited by
              #6

              Sam, Thanks. but that´s the original question.

              If you read the original post here, maybe you could see what´s going on. That is what is not working for me. It used to work but now it doesn´t. Doesn´t make sense, but it´s true.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                Does your class declaration contain the Q_OBJECT macro? The reason I ask is that you are not showing it, but it is vital for this use case. If there is no Q_OBJECT macro, things like getting the class name for an object don't work properly, and thus style sheets also can't use that.

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  darkgaze
                  wrote on last edited by
                  #8

                  OK. that´s the answer i was thinking about.

                  YEAH. definitely that´s what i did.... i removed the Q_Object because i had nothing like slots or signals for my class, so i removed it.

                  THANKS A LOT

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

                    You're welcome.

                    Q_OBJECT does a lot more than just enable signals and slots. It is a core part of Qt's introspection mechanism. In general, I would advice to use the Q_OBJECT macro by default in every subclass you create of a QObject or class derived of QObject. That includes all widgets.

                    This way, you ensure that introspection features properly work. There are many scenarios where this is useful to have.

                    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