How to set a stylesheet for a widget without affecting your child widgets in Pyqt5?

Asked

Viewed 33 times

3

I am creating a window with Pyqt5 Framework and need to add to it a background image. For this, I am setting your stylesheet thus:

mainWindow.setStyleSheet('background-image: url("./MyImage.png");')

The problem is that the image is added to the MainWindow and for your widgets children, such as the QLineEdit and the QPushButton. I would like to know how I can make this change in stylesheet window, without affecting the others widgets.

1 answer

4


You can name the widget and specify in the QSS:

QMainWindow#janelaEspecial {background-image: url("./MyImage.png");}

Everything you inherit from Object in Qt can be named with setObjectName

mainWindow.setObjectName('janelaEspecial')

And in this case you can set this in the global CSS/QSS if you prefer, by merging multiple settings in one place.

Also remembering that the QSS is a "ruse" of Qt, you can also use native methods and define a Qbrush with a background Qpixmap, without using styles.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.