PDA

View Full Version : Problem in setting background image of a custom widget



Ferdous
27th April 2009, 12:55
I tried the following code but in vain.


setAutoFillBackground(true);
//QPalette palette = this->palette();
QPalette palette;
palette.setBrush(QPalette::Window, QBrush(QPixmap(":/Resources/topGradient.png")));
this->setPalette(palette);


I tried with stylesheet but doing so caused the child widgets (buttons, labels) inherit the background image. So it's not suitable for my need.


setStyleSheet("background-image: url(:/Resources/topGradient.png);");

Can you please tell me why the use of QPalette for setting background image did not work?

Thanks.

wysota
28th April 2009, 08:29
What does "didn't work" mean in this situation? What was the widget you were trying to apply the image on?

Ferdous
30th April 2009, 08:49
I meant by "didn't work" that the intended background image didn't show up.

I'm trying to apply it on my custom widget which inherits a QWidget.

wysota
30th April 2009, 09:12
Stylesheets won't work then and as for changing the palette, do you actually paint the background in your widget's paintEvent?

Ferdous
30th April 2009, 11:10
No. I didn't override paintEvent() method. Isn't palette change supposed to serve the purpose? I'm really confused.

vonCZ
30th April 2009, 13:46
Not sure if it's necessary in your case, but maybe: have a .qrc file? Image & path correctly listed there?

Ferdous
2nd May 2009, 10:11
Yes. Images and path to images are correctly listed in a .qrc file.

When I tried with overriding paintEvent(), the background image is shown. It seems that custom derived classes of QWidget can't have background image through the use of QPalette. Can anyone please verify my guess?

Lykurg
2nd May 2009, 11:01
Yes. Images and path to images are correctly listed in a .qrc file.

When I tried with overriding paintEvent(), the background image is shown. It seems that custom derived classes of QWidget can't have background image through the use of QPalette. Can anyone please verify my guess?

Times ago there was a problem: http://www.qtsoftware.com/developer/task-tracker/index_html?method=entry&id=166742. Now it's still back. The problem is, that the background is drawn (show your widget alone and resize it: for a short time you can see your image), but then it is covered by the background color. May you want to report this...

Working solution with style sheets:
#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <QtGui>

class MyWidget : public QWidget
{
public:
MyWidget() {}

protected:
void paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
};

#endif // MYWIDGET_H

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWidget *w = new MyWidget();
w->setStyleSheet("background-image: url(...);");
w->show();
return a.exec();
}

Ferdous
3rd May 2009, 07:09
Thanks! Now I can use stylesheet for setting background-image of a custom widget :)

kongkong163
10th November 2010, 03:03
Thanks! Now I can use stylesheet for setting background-image of a custom widget :)

hello, Did you resolved your problem with Lykurg suggestions ? I tried with Lykurg suggestion, but it still caused the child widgets (buttons, labels) inherit the background image.

MorrisLiang
10th November 2010, 03:12
hello, Did you resolved your problem with Lykurg suggestions ? I tried with Lykurg suggestion, but it still caused the child widgets (buttons, labels) inherit the background image.

It's because your stylesheet doesn't specify which widget to apply.
If you have a widget, and you call QWidget::setObjectName("myWidget"); to set its object name.
Your stylesheet is like this:

#myWidget{background-image: url(:/Resources/topGradient.png);}
Or if your widget is an object of Class MyLittleWidget
then it's like this:

MyLittleWidget{background-image: url(:/Resources/topGradient.png);}