PyQt: How to use :hover in button-stylesheet

Viewed 1845

I know how to use QPushButton:hover in the stylesheet for my QMainWindow(). However for only one single button I want to have a different :hover result. Is there a way to use the :hover-function while setting the stylesheet for the specific button?

I tried

self.contact_button = QPushButton(self)
self.contact_button.setText("Contact buyer")
self.contact_button.setStyleSheet(" background-color: #181818; border: 1px solid black; "
                                  ":hover: { color: yellow };")

but it doesn't work.

1 Answers

You cannot mix selectors with generic declarations without brackets.

You also have a couple of typos: there should not be a colon after the selector, and there should not be a semicolon after the brackets (if you add any rule after that you'll get again an invalid stylesheet), so you need to be more careful with your syntax.

    self.contact_button.setStyleSheet("""
        QPushButton {
            background-color: #181818; 
            border: 1px solid black;
        }
        QPushButton:hover {
            color: yellow;
        }
    """)

In general, always look at the terminal/prompt/logger. In your case, your code shows the following, which is a clear symptom of wrong syntax:

StdErr: Could not parse stylesheet of object 0x9ce3a08

Related