FANDOM


Say you want to add a image as a background to a label in your form, they way we would do this in Qt Designer is to set up a resource file and add the images from there for you to use wherever you like within your project. Here I will quickly show how you can set up a resource file (.qrc file) and how you add files to it to use.

Setting up a recource file and adding resourcesEdit

To do this we will need to go to our Resource Browser (if it is not showing already go into View -> Resource Browser).

From there we click the Edit Resource button (first from the left).

Then click the New Resource File button (first from left).

Enter a file name (for example resource.qrc) in the desired location, then hit Save.

From here we hit Add Prefix (fourth from the left) and enter a name (for example, images).

Then click Add Files (fifth from left) and add the resources you want.

Adding image to objectEdit

For this example lets say we want to add a background image to a label. To do this we would need to go into the Property Editor and go to styleSheet .

Click on the three dots to the right and then go to Add Resource -> background-image .

From there select the resource you created and then the file that pops up on the right that you loaded in earlier, then hit OK.

LoadResource















And there you have it! Your label should now have the image as it's background. Keep in mind you can change the stylesheets of more than just labels, and stylsheets can do more than just change background images.

Converting .qrc to .pyEdit

Now before we can get our resource working when trying to run our Python file, we need to convert our resource.qrc file to a .py file. Otherwise when we try to run our Python file currrently we will get a 

ModuleNotFoundError: No module named 'resources_rc'

error. So to do this we need to do a similar thing as to how we convert our .ui file to a .py. Using the following command:

pyrcc5 resource.qrc -o resource_rc.py

in Anaconda. Make sure your.py version is the same name as your .qrc version, otherwise you Python app won't be able to find it on import.

ReferencesEdit

Creating a resource file to use it in Qt Designer - Stackoverflow

Qt Creator and main window background image - Stackoverflow

PyQt - ImportError: No module named 'resource_rc' - Stackoverflow

Community content is available under CC-BY-SA unless otherwise noted.