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. Finding if QPushButton isCheckable in subclass of QProxyStyle (drawPrimitive method)

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

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 513 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.
  • TrilecT Offline
    TrilecT Offline
    Trilec
    wrote on last edited by
    #1

    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!...

    Pl45m4P JonBJ 2 Replies Last reply
    0
    • TrilecT Trilec

      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;
      
      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @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
      4
      • TrilecT Trilec

        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;
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @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
        0
        • TrilecT Offline
          TrilecT Offline
          Trilec
          wrote on last edited by
          #4

          @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!...

          JonBJ B 2 Replies Last reply
          0
          • TrilecT Trilec

            @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)
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @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
            2
            • TrilecT Trilec

              @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)
              B Offline
              B Offline
              Bonnie
              wrote on last edited by Bonnie
              #6

              @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
              4
              • TrilecT Offline
                TrilecT Offline
                Trilec
                wrote on last edited by
                #7

                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
                0

                • Login

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