PDA

View Full Version : background-image on a set of widgets



asieriko
31st May 2008, 13:38
Hello,
I've created a widget and I want to have an image on its background. But when I try to do it, the image is also draw on each child widget.
I've tried with designer but It doesn't work either.
I've been able to not drawing again the image on the child widgets, drawing a new image on them.

I'll paste the code and show you some shoots.



#include <QApplication>
#include <QListWidget>
#include <QGridLayout>
#include <QWidget>

int main(int argc, char ** argv)
{
QApplication app( argc, argv );

QWidget widget(0);
widget.setStyleSheet("background-image: url(/home/asier/imagenes/fondos/14999-Midsommarblomster.jpg)");

QGridLayout *leftLayout = new QGridLayout();
leftLayout->setSpacing (0);

for( int row = 0; row < 2; row++ ) {
for( int col = 0; col < 2; col++ ) {

QListWidget *wi = new QListWidget();
wi->setStyleSheet("background-image: url(/home/asier/imagenes/fondos/Abetal.jpg);background-attachment: fixed");

leftLayout->addWidget( wi, row +1, col );

leftLayout->setRowStretch( row + 1, 1 );
}
}

widget.setLayout(leftLayout);

widget.show();
return app.exec();
}


In the first shoot you can be how the image is also drawn on each widget, the second shows the image on the background, but with a new one for each widget. And the last one, shows the desinger with blank widgets, but the preview draws again the image for each of them.

What can I do to have the image on the background, and not on each widget?
I don't know if It is my fault, is not implemented or if it is a bug?

Thank you in advance!
Asier

Tavit
31st May 2008, 14:08
Hi,
I think this may be help you.
Better to go for QLabel.you will get any kind of image as back ground.You can set a Qpixmap on Each label prosperity in your each widget on designer itself.

asieriko
31st May 2008, 15:38
Hi, Thanks.

But I need QListViews, I need lists.
The only problem is that the whole widget background-image is repetated again on each child widget.
I mean, Transparents Lists that woul make possible to view the image that is in the background.

asieriko
31st May 2008, 17:02
I've solved it.
I don't think this is the best way, but it works.

As I noticed that when I draw a background on the widgets, the Whole background was not draw on them, I've set a transparent background image for the widgets and It does the trick.


wi->setStyleSheet("background-image: url(/home/asier/transparent.png)");

Now I've to adapt it to a more complex widget. That at the first try does not work. But I hope, it will.
If you have any other idea for doing this, please tell me.

Thanks

jryannel
22nd May 2010, 07:59
It would have been easier using http://doc.qt.nokia.com/4.7-snapshot/qwidget.html#autoFillBackground-prop. Widgets don't draw their background for performance reasons. Setting a stylesheet will automaticall turn this on for my knowledge.