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;
-
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-constQPushButton *
, which is not possible this way.
This causes your warning / error msg. -
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;
-
@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)
-
@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)
-
@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
I'm not sure about you logic. But if only looking at your code, the safer way isif (const QAbstractButton* button = qobject_cast<const QAbstractButton*>(widget)) //even if widget is NULL, this line won't crash { if(button->isCheckable()) { //set stuff } }