Results 1 to 8 of 8

Thread: Background-image in top level widget

  1. #1
    Join Date
    Mar 2011
    Posts
    82
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Background-image in top level widget

    I'm trying to set the background image to my top-level widget and it's not working as expected. Am I doing something wrong here or is it just that you can't set a background image to the top level widget?

    This is a (very) small example where my issue is clear; I have a QWidget (StyleTest) in whose constructor I try to set its stylesheet. It does nothing.
    I also instantiate another QWidget* (for testing) and I set the very same styleSheet to it. THAT ONE displays perfectly.

    What am I doing wrong here?

    main.cpp
    Qt Code:
    1. #include "styletest.h"
    2. #include <QtGui/QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. StyleTest w;
    8. w.show();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    .h
    Qt Code:
    1. #ifndef STYLETEST_H
    2. #define STYLETEST_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include "qwidget.h"
    6. #include "ui_styletest.h"
    7.  
    8. class StyleTest : public QWidget
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. StyleTest(QWidget *parent = 0, Qt::WFlags flags = 0);
    14. ~StyleTest();
    15.  
    16. private:
    17. Ui::StyleTestClass ui;
    18. };
    19.  
    20. #endif // STYLETEST_H
    To copy to clipboard, switch view to plain text mode 

    .cpp
    Qt Code:
    1. #include "styletest.h"
    2.  
    3. StyleTest::StyleTest(QWidget *parent, Qt::WFlags flags)
    4. : QWidget(parent, flags)
    5. {
    6. QString style = " QWidget { background: url(./picture.png); } ";
    7. setStyleSheet(style);
    8.  
    9. QWidget * w = new QWidget();
    10. w->setStyleSheet(style);
    11. w->show();
    12. }
    13.  
    14. StyleTest::~StyleTest()
    15. {
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 


    example.jpg


    as a side note background-image has the same output/result.

    Thanks for your help.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Background-image in top level widget

    Relative path to image... relative to where exactly?

  3. #3
    Join Date
    Mar 2011
    Posts
    82
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Background-image in top level widget

    Relative to the .vcproj file.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Background-image in top level widget

    No, relative to the current working directory of the running program wherever that might be.

  5. #5
    Join Date
    Mar 2011
    Posts
    82
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Background-image in top level widget

    Which is my cvproj file Path.

  6. #6
    Join Date
    Mar 2011
    Posts
    82
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Background-image in top level widget

    Update:

    If I remove the Q_OBJECT macro, the background-image works properly. Of course I lose important stuff like Signals and slots in the process, so this is not a good enough answer to my problem, but at least there's another piece to the puzzle.

    Any ideas?

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Background-image in top level widget

    The Q_OBJECT macro has nothing to do with the problem.

    The background cannot be shown if it cannot be found in the relative location you have given it. The location is relative to the current working directory of the running process, which is both a movable location and almost certainly not where you think it is much of the time. Put the background image in a resource or be more explicit about where it is to be found and your problem will go away.

  8. #8
    Join Date
    Mar 2011
    Posts
    82
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Background-image in top level widget

    If that's the case, how is it possible for one widget in the same application the stylesheet (hence the path) is working properly and in the other one it is not?

    My screen shot is a single application instance, not two. The relative path for both widgets is the same, but they do not yield the same result.

    And removing the Q_OBJECT *is* allowing me to see the background image.

Similar Threads

  1. Background image of widget in QWidgetStack
    By el33t in forum Qt Programming
    Replies: 9
    Last Post: 16th March 2011, 03:10
  2. Background image on a combined widget list
    By buffer in forum Qt Programming
    Replies: 2
    Last Post: 18th November 2010, 00:04
  3. Replies: 2
    Last Post: 6th October 2010, 14:25
  4. how to change BackGround image on a stacked widget
    By mcrahr in forum Qt Programming
    Replies: 3
    Last Post: 10th February 2010, 08:38
  5. Background image and semitransparent widget
    By asieriko in forum Qt Programming
    Replies: 3
    Last Post: 20th August 2007, 16:43

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.