Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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;
    


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



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



  • @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)


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



  • @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
        }
    }
    


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

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

    worked,


Log in to reply