PyQt5 - setting background color and background image

108
January 26, 2021, at 04:40 AM

I wonder how do I set background color AND background image in Python GUI app using PyQt5. I can't figure out how to set them simultaneously. I tried doing

self.window.setStyleSheet("* {color: qlineargradient(spread:pad, x1:0 y1:0, x2:1 y2:0, stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));"
                  "background: qlineargradient( x1:0 y1:0, x2:1 y2:0, stop:0 #ffc982, stop:1 #ff9982);}; background-image: url(image.png); background-repeat: no-repeat")

but it is not working. I'm getting “could not parse stylesheet” error. Obviously, image is in the same direction as code.
Also, when I set only only background image it displays "shadow"
Do you know how to fix these problems? Thanks

Answer 1

There are two problems:

  1. there is a typo in the second line, as you added a ;} at the end of the background, which makes the stylesheet invalid;
  2. using the "* "universal selector (which is almost the same as using QWidget) means that all widgets will use the properties declared for it, and since you're probably setting the stylesheet on a QMainWindow (which inherits from QWidget), the image background is shown for both the main window and its central widget; the universal selector should be used with care, and especially avoided for top level widgets;

So, besides fixing the typo, you should apply the background only for the widget you're interested into. A good solution could be to set the object name of the central widget (if it's not already set, for instance when using a Designer file) and use the appropriate selector in the stylesheet. I also recommend you to use better format and indentation on stylesheets, as it will makes them more readable, allowing you to find syntax errors more easily.

self.window.centralWidget().setObjectName('centralWidget')
self.window.setStyleSheet('''
    QWidget#centralWidget {
        color: qlineargradient(spread:pad, x1:0 y1:0, x2:1 y2:0, 
               stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));
        background: qlineargradient(x1:0 y1:0, x2:1 y2:0, 
                    stop:0 #ffc982, stop:1 #ff9982);
        background-image: url(image.png);
        background-repeat: no-repeat;
    }''')
READ ALSO
How to take column from two table in whereExists clause in laravel 5.5

How to take column from two table in whereExists clause in laravel 5.5

I have three table 1)allcasefileofuserrequest 2)allscanfileofuserrequest 3)userrequestforcasecopies

88
Recursive select does not return date list [closed]

Recursive select does not return date list [closed]

Want to improve this question? Add details and clarify the problem by editing this post

101
Cannot publish new release to Google Play because of Sensitive app permissions

Cannot publish new release to Google Play because of Sensitive app permissions

The problem I'm facing is that I cannot create any new releases on any of the Google Play release tracks (internal, alpha, beta, production) since one of the old releases required location permissons

74
Convert datatime column to 18-digit LDAP timestamp

Convert datatime column to 18-digit LDAP timestamp

I have a csv file with AD users and their accountExpires attributeThe accountExpires values are currently in human readable format, but I want to convert it into 18-digit LDAP format so that I can import the file to change the date

94