Jacob Jensen
Jacob Jensen

Reputation: 115

Qt: Setting the background image using stylesheets doesn't work with resources

I'm new to Qt and having some issues! Using QtCreator, I created a simple window that i'm now trying to set the background image of.

Searching around lead me to believe i should use QT stylesheets and after trying some examples I managed to get a background image working by using:

this->setStyleSheet(" background-image: url(C:/test.jpg)");

in the constructor of the main image. However this url reference is obviously quite bad, so I tried using resources. My qrc looks like this:

<RCC>
    <qresource prefix="/images">
        <file alias="background.jpg">image2.jpg</file>
    </qresource>
</RCC>

Now trying to set the stylesheet with

this->setStyleSheet(" background-image: url(:/images/image2.jpg)");

or

this->setStyleSheet("background-image: url(:/background.jpg)");

or any combination of the two will not work!

One wierd thing i did discover is that when I go to the designer window and go to the properties window and set stylesheet of the main window to

 background-image: url(:/images/image2.jpg)

it actually displays the background image in the designer preview! but as soon as I go to launch the application the background image is gone!

Can anyone explain why I'm having these problems? Thanks!

Upvotes: 2

Views: 9083

Answers (2)

Thomas
Thomas

Reputation: 13

To use an image in qt, you must add it to a .qrc file.

Upvotes: 1

Brian McKinnon
Brian McKinnon

Reputation: 34

I think all you needed to do was add a semicolon inside the quotes.

this->setStyleSheet(" background-image: url(C:/test.jpg);");

Upvotes: 1

Related Questions