Reputation: 1
What is the best way to change to color of the button when pushbutton is active. for example ; pushbutton passive:its color is gray push button active :its color is green
Upvotes: 0
Views: 4143
Reputation: 11300
Marek's answer is elegant. If you need to change more attributes than just the colors, subclass from PushButton and provide the required slot for pressed
. Here's an example in Python (using PySide) that displays two different texts on the button, depending on whether it is active or not:
class TogglePushButtonWidget(QPushButton):
"""Toggles between on and off text
Changes color when in on state"""
def __init__(self, parent, on, off):
super().__init__(parent)
self.on = on
self.off = off
self.state = True
self.rotate_state()
self.pressed.connect(self.toggle_state)
def toggle_state(self):
self.state = not self.state
if self.state:
self.setText(self.on)
self.connect_w.setStyleSheet('background: #bbffbb;')
else:
self.setText(self.off)
self.connect_w.setStyleSheet('')
Upvotes: 0
Reputation: 37890
setStyleSheet("QPushButton { background-color: gray; }\n"
"QPushButton:enabled { background-color: green; }\n");
You can apply this method on single button but I recommend to apply it on QAppliaction
so it will have effect on all QPushButton
s
https://qt-project.org/doc/qt-5/stylesheet-reference.html
Upvotes: 1