Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved Finding if QPushButton isCheckable in subclass of QProxyStyle (drawPrimitive method)

    General and Desktop
    4
    7
    178
    Loading More Posts
    • 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.
    • Trilec
      Trilec last edited by

      Hi All,
      After much digging I cant seem to resolve this and would like to ask for help.
      My goal is to style the button(s) differently if its a checkable button and perhaps also using autoExclusive.
      Im also trying to use default for styling and had an interesting discovery that even though QtCreator did not have and buttons with default set it would show up as default (I had to manually toggle on and toggle in GUI to reset it)

      thanks for any help.

      void MyStyle::drawPrimitive(PrimitiveElement element,
                                             const QStyleOption *option,
                                             QPainter *painter,
                                             const QWidget *widget) const
      {
          QRect rect = option->rect;
          int state = option->state;
          QColor outline = this->outline(option->palette);
          QColor highlightedOutline = this->highlightedOutline(option->palette);
      
          switch (element) {
          case PE_PanelButtonCommand:
           {
                  painter->save();
      
                  bool isDefault = false;
                  bool isFlat = false;
                  bool isNormal = false;
                  bool isDown = (option->state & State_Sunken) || (option->state & State_On);
                  bool isEnabled = option->state & State_Enabled;
                  bool hasFocus = (option->state & State_HasFocus && option->state & State_KeyboardFocusChange);
      
                  QColor borderColour =  option->palette.base().color().lighter(108);
                  QBrush fillBrush = option->palette.brush(QPalette::Button);
      
                  if (const QStyleOptionButton *button = qstyleoption_cast<const QStyleOptionButton*>(option)) {
                      isDefault = (button->features & (QStyleOptionButton::DefaultButton | QStyleOptionButton::AutoDefaultButton));
                      isFlat = (button->features & QStyleOptionButton::Flat);
                      isNormal = (button->features & QStyleOptionButton::None);
      //
      //line I tried to get the  bool value but errors:: static_cast from type 'const QObject*' to type 'QPushButton*' casts away qualifiers
       //               if (widget && qobject_cast<QPushButton*>(widget)->isCheckable() )
       //               {  
       //                   borderColour = redtest();
       //               }
                  }
      
                  drawPlainRect(painter, rect, borderColour, 1, &fillBrush);
                  painter->restore();
      
             }
              break;
      

      If ts not in the computer it doesn't exist!...

      Pl45m4 JonB 2 Replies Last reply Reply Quote 0
      • Pl45m4
        Pl45m4 @Trilec last edited by Pl45m4

        @Trilec said in Finding if QPushButton isCheckable in subclass of QProxyStyle (drawPrimitive method):

        qobject_cast<QPushButton*>(widget)->isCheckable()

        You are casting a const QObject * to non-const QPushButton *, which is not possible this way.
        This causes your warning / error msg.


        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 Reply Quote 4
        • JonB
          JonB @Trilec last edited by

          @Trilec
          Just in case it's not clear from @Pl45m4's answer: you need to go qobject_cast<const QPushButton*>(widget)->isCheckable().

          1 Reply Last reply Reply Quote 0
          • Trilec
            Trilec last edited by

            @JonB said in Finding if QPushButton isCheckable in subclass of QProxyStyle (drawPrimitive method):

            qobject_cast<const QPushButton*>(widget)->isCheckable().

            thanks, I have changed to
            ```
            if (widget && qobject_cast<const QAbstractButton*>(widget)->isCheckable() )
            {
            //set stuff
            }

             it compiles fine however it crashes , The approach Im taking to get the isCheckable seems to be not the correct one,
            Im not sure if there is a better solution for getting to this value!, I can only think I need a further logic to see if indeed its a Pushbutton? so the pointer is valid (widget)

            If ts not in the computer it doesn't exist!...

            JonB B 2 Replies Last reply Reply Quote 0
            • JonB
              JonB @Trilec last edited by

              @Trilec
              So widget is not a QAbstractButton*. Obviously you can check for that without crashing, I don't know about your logic though.

              1 Reply Last reply Reply Quote 2
              • B
                Bonnie @Trilec last edited by Bonnie

                @Trilec
                I'm not sure about you logic. But if only looking at your code, the safer way is

                if (const QAbstractButton* button = qobject_cast<const QAbstractButton*>(widget))  //even if widget is NULL, this line won't crash
                {
                    if(button->isCheckable()) {
                        //set stuff
                    }
                }
                
                1 Reply Last reply Reply Quote 4
                • Trilec
                  Trilec last edited by

                  Thanks for the help,
                  the Logic was correct and the ;

                  const QAbstractButton* button = qobject_cast<const QAbstractButton*>(widget)
                  

                  worked,

                  If ts not in the computer it doesn't exist!...

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post