PDA

View Full Version : Background image for main window widget using css.



Enygma
22nd August 2007, 12:15
Hi. I`m having a problem with setting the background-image css attribute for my main window wich is a subclasses Widget built with the designer.

The main issue is that I use a MultiPageWidget and when I try to apply a background-image to the main window containing the multipagewidget, nothing happens.

If, on the other hand, I apply a background-color to the main window, it works.

Any help would be very much appreciated. :confused:

Thank you.

jpn
22nd August 2007, 12:24
Exactly how do you set these background images and colors? Do you embed the background image as a resource or could it be a problem with relative paths? And what is a MultiPageWidget?

Enygma
22nd August 2007, 14:30
Ok, so I have the background image in a resource file.

I have a widget as main window and on top of that, a MultiPageWidget. ( all built with the designer ).

Now, using a stylesheet I haven't managed to set the window ( widget ) background so that the surrounding multipagewidget`s area ( the visible place belonging to the main window widget that is behind ) would have the specified background-image.

After trying all the possibilities, I found a workaround... actually not a workaround... just something to make it work :P and it involves the following code, and not css ( what I wanted ).



QPixmap bg(":/images/background.png");
QPalette p(palette());
p.setBrush(QPalette::Background, bg);
setAutoFillBackground(true);
setPalette(p);


This is quite a standard way of setting an image for background, but I wanted the clean css way, not the coded way...

p.s.: This way It had a nice side-effect by displaying the background image trough the other controls like QGroupBox and QLabel that were contained in the MultiPageWidget. I expected them being opaque instead of transparent, but that will do.

The CSS I used in trying to set the background image was simple:


MainWindow {
background-image: url(:/images/background.png);
}


If I use a background-color: red; for example... I get the desired effect, only with a color, not an image.

Hope I was explicit enough.

Any ideeas?

jpn
22nd August 2007, 15:15
Does the class declaration of MainWindow contain Q_OBJECT macro? Could you paste the .qrc file?

Enygma
22nd August 2007, 17:04
class MainWindow : public QWidget, private Ui::MainWindowUi{
Q_OBJECT


<RCC>
<qresource>
...
<file>images/background.png</file>
...
</qresource>
</RCC>

I already said it worked using the pixmap so that means that the resource is ok.

:confused:

jpn
22nd August 2007, 17:29
I think you might have run into this problem:
http://trolltech.com/developer/task-tracker/index_html?method=entry&id=166742

Enygma
23rd August 2007, 08:10
2007-06-12 17:48 - Entry created: Task status changed to 'Open', Resolution set to 'Pending', Version found set to '4.3.0', Version for fix set to '4.3.1', Priority changed to '1 - Highest'
2007-06-13 17:17 - Task status changed to 'Active'
2007-06-14 15:11 - Resolution set to 'Fixed'
2007-06-14 15:11 - Task status changed to 'Closed'


I use Qt 4.3.1. No fix.

jpn
23rd August 2007, 09:06
Could you try what happens if you provide such paintEvent() as suggested in the "resolution"? I'm looking at qwidget.cpp of Qt 4.3.1 and see and empty paintEvent().

Enygma
23rd August 2007, 15:40
Tried it and worked.

Thumbs up! :D