PyQt6 adding mouseReleaseEvent handler changes button color
-
When I add a mouseReleaseEvent handler method to my custom QPushButton, when I then press and release the button it looses the background color I assigned it. Why?
How do I get the button to keep the color I assigned to it?I reproduced the behavior without my custom painting code in a small example here:
import sys from PyQt6 import QtWidgets class MyButton(QtWidgets.QPushButton): def __init__(self, *args, **kwargs): super(MyButton, self).__init__(*args, **kwargs) def mouseReleaseEvent(self, event): self.update() self.parent().mouseReleaseEvent(event) class TestWindow(QtWidgets.QWidget): def __init__(self): QtWidgets.QWidget.__init__(self) self.resize(200, 400) layout = QtWidgets.QVBoxLayout() btn = MyButton("PRESS ME") btn.setStyleSheet("background-color: silver") layout.addWidget(btn) self.setLayout(layout) if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) ex = TestWindow() ex.show() sys.exit(app.exec())
-
@JonB removing parent() just causes recursion depth exceeded. I get where your coming from, but still not sure what I should do .. did you happen to try with the code I provided (please).
@Captain-Haddock
My mistake: I never meantself.mouseReleaseEvent(event)
(of course infinite recursion), I always meantsuper().mouseReleaseEvent(event)
... :)...Which, just tested, does of course work fine, as it always should do.... [And yours with
parent()
does not work right, equally unsurprising....]I don't know what you think are achieving, but the
self.update()
is/should be a waste of time.... As it stands you don't want it there at all, and if you want anything for some reason for immediate painting it would beself.repaint()
notself.update()
. -
When I add a mouseReleaseEvent handler method to my custom QPushButton, when I then press and release the button it looses the background color I assigned it. Why?
How do I get the button to keep the color I assigned to it?I reproduced the behavior without my custom painting code in a small example here:
import sys from PyQt6 import QtWidgets class MyButton(QtWidgets.QPushButton): def __init__(self, *args, **kwargs): super(MyButton, self).__init__(*args, **kwargs) def mouseReleaseEvent(self, event): self.update() self.parent().mouseReleaseEvent(event) class TestWindow(QtWidgets.QWidget): def __init__(self): QtWidgets.QWidget.__init__(self) self.resize(200, 400) layout = QtWidgets.QVBoxLayout() btn = MyButton("PRESS ME") btn.setStyleSheet("background-color: silver") layout.addWidget(btn) self.setLayout(layout) if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) ex = TestWindow() ex.show() sys.exit(app.exec())
@Captain-Haddock said in PyQt6 adding mouseReleaseEvent handler changes button color:
self.parent().mouseReleaseEvent(event)
Why on
parent()
?? Sounds like the button will never see a release event, and remains in whatever style it takes on when pressed but not released, which might ignore your color? Remove.parent()
here, any better? -
@Captain-Haddock said in PyQt6 adding mouseReleaseEvent handler changes button color:
self.parent().mouseReleaseEvent(event)
Why on
parent()
?? Sounds like the button will never see a release event, and remains in whatever style it takes on when pressed but not released, which might ignore your color? Remove.parent()
here, any better?@JonB removing parent() just causes recursion depth exceeded. I get where your coming from, but still not sure what I should do .. did you happen to try with the code I provided (please).
-
@JonB removing parent() just causes recursion depth exceeded. I get where your coming from, but still not sure what I should do .. did you happen to try with the code I provided (please).
@Captain-Haddock
My mistake: I never meantself.mouseReleaseEvent(event)
(of course infinite recursion), I always meantsuper().mouseReleaseEvent(event)
... :)...Which, just tested, does of course work fine, as it always should do.... [And yours with
parent()
does not work right, equally unsurprising....]I don't know what you think are achieving, but the
self.update()
is/should be a waste of time.... As it stands you don't want it there at all, and if you want anything for some reason for immediate painting it would beself.repaint()
notself.update()
. -
@Captain-Haddock
My mistake: I never meantself.mouseReleaseEvent(event)
(of course infinite recursion), I always meantsuper().mouseReleaseEvent(event)
... :)...Which, just tested, does of course work fine, as it always should do.... [And yours with
parent()
does not work right, equally unsurprising....]I don't know what you think are achieving, but the
self.update()
is/should be a waste of time.... As it stands you don't want it there at all, and if you want anything for some reason for immediate painting it would beself.repaint()
notself.update()
.@JonB Doh! Thanks so much super() does it .. and the update() was a legacy mistake (beginner)
-