PDA

View Full Version : Applying a backgroung image to a widget



Stanfillirenfro
15th February 2012, 16:37
Hello!
I apologize to disturb you. I am facing a problem of setting a background image to my widget.
I have applied the following function, but the widgest has turned to black completely:
QPalette palette;
QString appDir = QApplication::applicationDirPath();
palette.setBrush(this->backgroundRole(),QBrush(QImage(appDir +"/Background.png")));
this->setPalette(palette);
Does anyone has a idea how to solve the problem?
I will be greatfull to everyone who would help me solving this problem:
-either by telling me why the widget has turned to black
-or by recommending me aonther function.
Many thanks in advance.

AlexSudnik
15th February 2012, 21:00
Try to use QPalette::Window as a color role.

Stanfillirenfro
15th February 2012, 22:28
Thanks!
I have a picture from my desktop and it is this picture that I wanted to apply as background.
QPalette::Window is not working in my case.
Thanks anyway!

AlexSudnik
15th February 2012, 22:45
You mean , you're still having the same result ? Are you sure your path is correct (try smth. like "D:/image.png") ? It's weird because it works just fine for me.

Stanfillirenfro
15th February 2012, 23:36
Hallo AlexSudnik. The matter is that my window turns completely black when I apply the image, so that the characters dissapear. If there is a solution to overcome the black color, the whole problem will be solve. Do you have an idea how to get it right please?

AlexSudnik
16th February 2012, 09:00
It looks like the problem is in the image's path (or your image has different name ) : i've moved the image from the folder and...as you say widget's background has turned black.Try to check the file's name spelling or this:

palette.setBrush( QPalette::Window , QBrush(QImage( "Background.png" ) ) );

and make sure that Background.png is placed where an executable is.

It must work , if it's not...share the code.

Stanfillirenfro
16th February 2012, 09:49
Hello AlexSudnik!
I am greatfull to you for your help. It is fine now. I have followed your advise and moved the image where the executable is. It is working now.
To sumarize, the widget has turned black because the image was separated from the executable in another folder.
One more time many thanks for your help.

RENOLD
17th February 2012, 10:36
Use stylesheet to apply background image to a widget...