QProxyStyle, drawControl , CE_PushButton , extra styles
-
Hi all,
someone else might be tackling this and finding it difficult with Qt6 and having multi styles within a control StyleSheet of via QProxyStyle usage , hopefully this can help others find clarity, and also to doublecheck with those more knowledgeable if this is a reasonable starting point.Issue:
I'm trying to have a QPushbutton contain different styles that can be controlled from QTcreator by default you can only access normal and flat ( flat being the checkbox can select in the properties window of the QPushButton) but I would like to ideally have multiple styles currently I'm only interested in a third ( thus I'm adding a bool)Solution:
There are a few solutions, one is to use other checkboxes to control the selecting of a style for example perhaps pushButton->isDefault() (but bear in mind this has other functionality) or another is to add within the QTCreator a dynamic property value ( it's the green "+" icon above the properties view.)
in this case I have set this to a name "newDesign" with a bool type (one of the nice things is that it generally falls under the button properties as dynamic properties are at the end) then you need to check this property value when applying styling:Stylesheet:
QPushButton[newDesign="true"] { background-color: green; }
QProxyStyle drawControl
void CharcoalStyle::drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const { switch (element) { case CE_PushButton: { auto buttonOption = const_cast<QStyleOptionButton*>(qstyleoption_cast<const QStyleOptionButton*>(option)); auto pushButton = qobject_cast<QPushButton*>(const_cast<QWidget*>(widget)); //is a valid pushbutton and has valid pushbutton options if (buttonOption && pushButton) { // prepare and load existing defaults QFont font = pushButton->font(); Qt::Alignment alignment = Qt::Alignment(static_cast<int>(buttonOption->features) & Qt::AlignHorizontal_Mask); // int fontSize = pushButton->fontInfo().pointSize(); //if you want to get them individually // QString familyName = pushButton->fontInfo().family(); //if you want to get them individually // testing concept for using the features to control various drawing capabilities enum BUTTONFEATURES { NormalDesign = 0, FlatDesign = 1, NewDesign = 2, }; int buttonFeatures = NormalState; QColor buttonBgColor = NormalButtonBg; QColor buttonFgColor = NormalButtonFg; QColor buttonBorderColor = NormalButtonBorder; QColor buttonHoverBgColor = NormalButtonFg.lighter(110); painter->save(); painter->setRenderHint(QPainter::Antialiasing, true); painter->setFont(font); if (buttonOption-property("newDesign").toBool()) { buttonFeatures = NewDesign; buttonBgColor = FlatButtonBg; buttonFgColor = FlatButtonFg; } // .. other tests for style of flat and normal ( as new design comes first this has preference)
any comments welcome
Curtis
-
T TrilecStudios has marked this topic as solved on