Results 1 to 11 of 11

Thread: Problem in setting background image of a custom widget

  1. #1
    Join Date
    Sep 2008
    Location
    Bangladesh
    Posts
    17
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem in setting background image of a custom widget

    I tried the following code but in vain.

    Qt Code:
    1. setAutoFillBackground(true);
    2. //QPalette palette = this->palette();
    3. QPalette palette;
    4. palette.setBrush(QPalette::Window, QBrush(QPixmap(":/Resources/topGradient.png")));
    5. this->setPalette(palette);
    To copy to clipboard, switch view to plain text mode 


    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.

    Qt Code:
    1. setStyleSheet("background-image: url(:/Resources/topGradient.png);");
    To copy to clipboard, switch view to plain text mode 

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

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2008
    Location
    Bangladesh
    Posts
    17
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2008
    Location
    Bangladesh
    Posts
    17
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

  6. #6
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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?

  7. #7
    Join Date
    Sep 2008
    Location
    Bangladesh
    Posts
    17
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default 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?

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problem in setting background image of a custom widget

    Quote Originally Posted by Ferdous View Post
    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:
    Qt Code:
    1. #ifndef MYWIDGET_H
    2. #define MYWIDGET_H
    3.  
    4. #include <QtGui>
    5.  
    6. class MyWidget : public QWidget
    7. {
    8. public:
    9. MyWidget() {}
    10.  
    11. protected:
    12. void paintEvent(QPaintEvent *)
    13. {
    14. opt.init(this);
    15. QPainter p(this);
    16. style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
    17. }
    18. };
    19.  
    20. #endif // MYWIDGET_H
    21.  
    22. int main(int argc, char *argv[])
    23. {
    24. QApplication a(argc, argv);
    25. MyWidget *w = new MyWidget();
    26. w->setStyleSheet("background-image: url(...);");
    27. w->show();
    28. return a.exec();
    29. }
    To copy to clipboard, switch view to plain text mode 

  9. The following 2 users say thank you to Lykurg for this useful post:

    Ferdous (3rd May 2009), m.nour (18th January 2011)

  10. #9
    Join Date
    Sep 2008
    Location
    Bangladesh
    Posts
    17
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem in setting background image of a custom widget

    Thanks! Now I can use stylesheet for setting background-image of a custom widget

  11. #10
    Join Date
    Oct 2010
    Posts
    12
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Problem in setting background image of a custom widget

    Quote Originally Posted by Ferdous View Post
    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.

  12. #11
    Join Date
    Apr 2010
    Posts
    98
    Thanks
    19
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem in setting background image of a custom widget

    Quote Originally Posted by kongkong163 View Post
    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:
    Qt Code:
    1. #myWidget{background-image: url(:/Resources/topGradient.png);}
    To copy to clipboard, switch view to plain text mode 
    Or if your widget is an object of Class MyLittleWidget
    then it's like this:
    Qt Code:
    1. MyLittleWidget{background-image: url(:/Resources/topGradient.png);}
    To copy to clipboard, switch view to plain text mode 
    It's not the goodbye that hurts,but the flashback that follow.

Similar Threads

  1. Replies: 0
    Last Post: 6th April 2009, 01:20
  2. Problem reimplementing QSpinBox in a custom widget
    By hvengel in forum Qt Programming
    Replies: 1
    Last Post: 30th March 2006, 08:12
  3. Replies: 4
    Last Post: 24th March 2006, 22:50
  4. Problem with custom widget
    By jnana in forum Qt Programming
    Replies: 3
    Last Post: 15th March 2006, 11:55
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.