Problem in setting background image of a custom widget
I tried the following code but in vain.
Code:
setAutoFillBackground(true);
//QPalette palette = this->palette();
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.
Code:
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.
Re: Problem in setting background image of a custom widget
What does "didn't work" mean in this situation? What was the widget you were trying to apply the image on?
Re: Problem in setting background image of a custom widget
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.
Re: Problem in setting background image of a custom widget
Stylesheets won't work then and as for changing the palette, do you actually paint the background in your widget's paintEvent?
Re: Problem in setting background image of a custom widget
No. I didn't override paintEvent() method. Isn't palette change supposed to serve the purpose? I'm really confused.
Re: Problem in setting background image of a custom widget
Not sure if it's necessary in your case, but maybe: have a .qrc file? Image & path correctly listed there?
Re: Problem in setting background image of a custom widget
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?
Re: Problem in setting background image of a custom widget
Quote:
Originally Posted by
Ferdous
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/...ntry&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:
Code:
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QtGui>
{
public:
MyWidget() {}
protected:
{
opt.init(this);
style
()->drawPrimitive
(QStyle::PE_Widget,
&opt,
&p,
this);
}
};
#endif // MYWIDGET_H
int main(int argc, char *argv[])
{
MyWidget *w = new MyWidget();
w->setStyleSheet("background-image: url(...);");
w->show();
return a.exec();
}
Re: Problem in setting background image of a custom widget
Thanks! Now I can use stylesheet for setting background-image of a custom widget :)
Re: Problem in setting background image of a custom widget
Quote:
Originally Posted by
Ferdous
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.
Re: Problem in setting background image of a custom widget
Quote:
Originally Posted by
kongkong163
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:
Code:
#myWidget{background-image: url(:/Resources/topGradient.png);}
Or if your widget is an object of Class MyLittleWidget
then it's like this:
Code:
MyLittleWidget{background-image: url(:/Resources/topGradient.png);}