How to define push button pressed colour through QPalette in Fusion Style?
-
Is there any way to change the
QPushButton
pressed colour throughQPalette
inFusion
style? My application depends on the colour ofQPushButton
to indicate its current state. However, the change in colour between the pressed and non-pressed state ofQPushButton
is almost negligible, it's very hard to say if aQPushButton
is pressed or not because except for a very slight change in the button colour, nothing else changes. Changing the colour of eachQPushButton
when it is pressed is a big task.QPalette
has things likeQPalette::Base
andQPalette::AlternateBase
and so many other options but I can't seem to figure out what palette combination (i.e. ColorGroup and ColorRole) is used forQPushButton
pressed state? -
@CJha said in How to define push button pressed colour through QPalette in Fusion Style?:
My application depends on the colour of QPushButton to indicate its current state
This is a terrible idea and should be changed. Any reason why you don't get the state directly?
-
@VRonin It's an application that controls hardware and the hardware can be in different states. I use foreground colour (i.e. button colour) to indicate the current state of the hardware. Even if I don't use the colours to indicate the change in hardware state, still the change between the pressed and un-pressed state of the button in fusion style is negligible and this can cause confusion for users.
-
Hi,
What kind of state are your button representing ?
It might not be the best control for that. Maybe the use of icons would make things clearer. -
@CJha I am using background color for this. Different states can be defined for button events(Enter, pressed, released, etc). Simply attach different background colors to the states. If the states change too fast, use timer to add a bit delay while hardware change is much slower.
-
@SGaist My buttons are representing hardware states of equipment that is used for electrophysiological measurements and is going to be used in labs and industry. My application controls hardware which can be in different states, the controls in these different states remain almost the same, but their functionality changes because of the state change of the hardware. To make these changes clear to the user I am using different colours for the
QPushButton
.Even if I keep the same colour for the
QPushButton
for all the states of the hardware, still the change in pressed vs un-pressed state of theQPushButton
inFusion
style is very small and can be easily missed. This can lead to confusion in users mind as to if a button is currently pressed or not. Since it is a scientific/industrial application there is no room for any kind of confusion. I do not want to useWindowsVista
orMacOS
style as these are very restrictive, especially if the background is dark then the button colour doesn't change and it remains light coloured but its text changes to a light colour making all the button text unreadable. I have to implement dark mode in my application because it is also going to be used in the darkroom (fluorescent imaging, etc).All I am looking for is a way to have a proper distinguishing factor between button pressed vs un-pressed state. Previously, I had implemented this by using a singleton in my application and then doing the following on my application startup (after
setupui
is called inQMainWindow
,Sole
is my singleton class object):void Sole::connectAllPushButtonsToSlotsInSole() { QWidgetList allWidgets = qApp->allWidgets(); for(QWidget* wid : allWidgets) { QString str = wid->metaObject()->className(); if(str == "QPushButton") { QPushButton* btn = dynamic_cast<QPushButton*>(wid); bool chkbl = btn->property("checkable").toBool(); if(chkbl) { connect(btn, &QPushButton::toggled, this, &Sole::priS_pushButtonToggled); } else { connect(btn, &QPushButton::pressed, this, &Sole::priS_pushButtonPressed); connect(btn, &QPushButton::released, this, &Sole::priS_pushButtonReleased); } } } } void Sole::priS_pushButtonPressed() { QPushButton* sender = qobject_cast<QPushButton*>(QObject::sender()); sender->setPalette(darkBtnPal); QFont font = sender->font(); font.setItalic(true); //set font to italic sender->setFont(font); } void Sole::priS_pushButtonReleased() { QPushButton* sender = qobject_cast<QPushButton*>(QObject::sender()); sender->setPalette(lightBtnPal); QFont font = sender->font(); font.setItalic(false); //set font to normal (i.e. undo italic) sender->setFont(font); } void Sole::priS_pushButtonToggled(bool toggled) { QPushButton* sender = qobject_cast<QPushButton*>(QObject::sender()); if(toggled) { sender->setPalette(darkBtnPal); QFont font = sender->font(); font.setItalic(true); //set font to italic sender->setFont(font); } else { sender->setPalette(lightBtnPal); QFont font = sender->font(); font.setItalic(false); //set font to normal (i.e. undo italic) sender->setFont(font); } }
Given my recent experience with Singletons, I am trying to get rid of it and so I am looking for a new way to achieve the same level of distinguishing factor between pressed vs un-pressed state of
QPushButton
. -
Not knowing how the application looks like I can't really comment on your use of color.
However, I still encourage you to consider using icons to convey more information to your users.
QIcon supports sets of images for different states that may make your application easier to read and follow.