Issue
In my pyqt gui while a button is pressed the button's text color changes. And when the button is released the color goes back to its original self.
I have the code working like the following:
self.ui.pButton_save.pressed.connect(self.save_pressed)
self.ui.pButton_cancel.pressed.connect(self.cancel_pressed)
self.ui.pButton_save.released.connect(self.save_released)
self.ui.pButton_cancel.released.connect(self.cancel_released)
def save_pressed(self):
self.ui.pButton_save.setStyleSheet("color: white")
def cancel_pressed(self):
self.ui.pButton_cancel.setStyleSheet("color: white")
def save_released(self):
self.ui.pButton_save.setStyleSheet("color: green")
def cancel_released(self):
self.ui.pButton_cancel.setStyleSheet("color: red")
The code works fine. But as you can see there are so many lines for this simple task. Probably there is a cleaner (more Pythonic) way of doing it. Any advice?
Solution
This is the way to do it with Qt Style Sheet, the ones that handle the states like the pressed button.
self.ui.pButton_save.setStyleSheet(
"QPushButton:pressed{color: white} QPushButton{color: green}")
self.ui.pButton_cancel.setStyleSheet(
"QPushButton:pressed{color: white} QPushButton{color: red}")
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.