Search code examples
pythonpyside6pyqt6

How to give a button both a background color and a hover color?


I want my button to have a blue background, then a green background when you hover the mouse over it.

This is my code:

self.button.setStyleSheet("background-color:blue")
self.button.setStyleSheet("QPushButton::hover"
                          "{"
                          "background-color:green;"
                          "}")

How can I make these two lines work at the same time?


Solution

  • Just join the qt stylesheets:

    self.button.setStyleSheet(
        """
        QPushButton{
            background-color:blue;
        }
        QPushButton::hover{
            background-color:green;
        }
        """
    )