+ Reply to Thread
Results 1 to 9 of 9

Thread: Problem in setting background image of a custom widget

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

    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. Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    17,080
    Thanks
    3
    Thanked 2,453 Times in 2,381 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Blog Entries
    1

    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?
    Please ask Qt related questions on the forum and not using private messages or visitor messages.

    wwWidgets - a set of Qt widgets for your applications

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

    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. Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    17,080
    Thanks
    3
    Thanked 2,453 Times in 2,381 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Blog Entries
    1

    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?
    Please ask Qt related questions on the forum and not using private messages or visitor messages.

    wwWidgets - a set of Qt widgets for your applications

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

    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. Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    172
    Thanks
    42
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    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. Join Date
    Sep 2008
    Location
    Bangladesh
    Posts
    17
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    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. Join Date
    Jan 2006
    Location
    Germany
    Posts
    1,472
    Thanks
    13
    Thanked 285 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    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 user says thank you to Lykurg for this useful post:

    Ferdous (3rd May 2009)

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

    Re: Problem in setting background image of a custom widget

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

+ Reply to Thread

Similar Threads

  1. Replies: 0
    Last Post: 6th April 2009, 02:20
  2. Problem reimplementing QSpinBox in a custom widget
    By hvengel in forum Qt Programming
    Replies: 1
    Last Post: 30th March 2006, 09:12
  3. Replies: 4
    Last Post: 24th March 2006, 23:50
  4. Problem with custom widget
    By jnana in forum Qt Programming
    Replies: 3
    Last Post: 15th March 2006, 12:55
  5. Replies: 16
    Last Post: 7th March 2006, 16: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