Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. PyQt6 adding mouseReleaseEvent handler changes button color
Forum Updated to NodeBB v4.3 + New Features

PyQt6 adding mouseReleaseEvent handler changes button color

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 2 Posters 725 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Captain Haddock
    wrote on last edited by
    #1

    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())
    
    JonBJ 1 Reply Last reply
    0
    • C Captain Haddock

      @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).

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #4

      @Captain-Haddock
      My mistake: I never meant self.mouseReleaseEvent(event) (of course infinite recursion), I always meant super().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 be self.repaint() not self.update().

      C 1 Reply Last reply
      1
      • C Captain Haddock

        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())
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @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?

        C 1 Reply Last reply
        0
        • JonBJ JonB

          @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?

          C Offline
          C Offline
          Captain Haddock
          wrote on last edited by
          #3

          @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).

          JonBJ 1 Reply Last reply
          0
          • C Captain Haddock

            @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).

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #4

            @Captain-Haddock
            My mistake: I never meant self.mouseReleaseEvent(event) (of course infinite recursion), I always meant super().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 be self.repaint() not self.update().

            C 1 Reply Last reply
            1
            • JonBJ JonB

              @Captain-Haddock
              My mistake: I never meant self.mouseReleaseEvent(event) (of course infinite recursion), I always meant super().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 be self.repaint() not self.update().

              C Offline
              C Offline
              Captain Haddock
              wrote on last edited by
              #5

              @JonB Doh! Thanks so much super() does it .. and the update() was a legacy mistake (beginner)

              1 Reply Last reply
              0
              • C Captain Haddock has marked this topic as solved on

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved