Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is it possible to set a background image to a widget?
QtWS25 Last Chance

Is it possible to set a background image to a widget?

Scheduled Pinned Locked Moved General and Desktop
17 Posts 8 Posters 119.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kamalakshantv
    wrote on 21 Oct 2010, 14:03 last edited by
    #1

    How can we place a background image to a QWidget? Can it be set using styles such that all widgets have the same background.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DenisKormalev
      wrote on 21 Oct 2010, 16:01 last edited by
      #2

      The easiest way is to write small stylesheet and apply it to application (or only to needed widgets, if it should not be applied for all widgets). Also you can use style technology and draw it manually.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kamalakshantv
        wrote on 21 Oct 2010, 16:07 last edited by
        #3

        I had tried it but the background image was not getting displayed. Can you provide the syntax for adding a background image to a QWidget.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          genjix
          wrote on 21 Oct 2010, 16:28 last edited by
          #4

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

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DenisKormalev
            wrote on 21 Oct 2010, 16:46 last edited by
            #5

            I thik this one will fit better
            @
            qApp->setStyleSheet("QWidget {background-image: url(./image.png) }");
            @

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mario
              wrote on 22 Oct 2010, 06:13 last edited by
              #6

              and if you just want the background on a particular widget use:
              @myWidget->setStyleSheet("background-image: url(./image.png)")@

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kamalakshantv
                wrote on 22 Oct 2010, 09:57 last edited by
                #7

                Thank you, I had tried the same before also and then it was not working - now its working. May be I had not built it properly or did some silly mistake then.

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  kamalakshantv
                  wrote on 25 Oct 2010, 10:34 last edited by
                  #8

                  I am applying the below style using Qt Designer on the root widget

                  QWidget {background-image: url(./image.png) }

                  Now in this case all the child widgets also have the same image as background image. How to modify the above to have it only configured as a background image to the root widget.

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    Lord
                    wrote on 25 Oct 2010, 11:34 last edited by
                    #9

                    Give name to your widget by setObjectname().

                    eg:
                    @
                    QPushButton *okButton = new QPushButton;
                    okButton->setObjectname(QObject::tr(okButton));
                    @
                    in stylesheet,
                    @
                    QPushButton#okButton
                    {
                    color: gray
                    }
                    @
                    now only okButton will be gray.

                    Same thing is applicable in your case. Hope it helps!

                    [edit: Code highlighting / Denis Kormalev]

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      kamalakshantv
                      wrote on 25 Oct 2010, 11:37 last edited by
                      #10

                      Thanks Rahul

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mario
                        wrote on 25 Oct 2010, 12:17 last edited by
                        #11

                        Please also check out the docs for style sheets at:
                        "http://doc.qt.nokia.com/4.7/stylesheet.html":http://doc.qt.nokia.com/4.7/stylesheet.html

                        1 Reply Last reply
                        0
                        • H Offline
                          H Offline
                          houmingc
                          wrote on 5 Nov 2014, 13:37 last edited by
                          #12
                          MainWindow w;
                          w.setStyleSheet("background-image:url(./file/left.png)");
                          w.show();
                          

                          I did setStyleSheet on MainWindow, it doesn't seem to work

                          1 Reply Last reply
                          0
                          • F Offline
                            F Offline
                            Francknos
                            wrote on 5 Nov 2014, 13:42 last edited by
                            #13

                            MainWindow w; w.setStyleSheet(“background-image:url(:/file/left.png)”); w.show();
                            WITH ::::::: and if you have a ui file. put setStyleSheet after ui->setupUI(this);

                            1 Reply Last reply
                            0
                            • H Offline
                              H Offline
                              houmingc
                              wrote on 5 Nov 2014, 14:06 last edited by
                              #14

                              i have left.png and right.png in my resource folder under /file
                              which is the correct command?

                              1 Reply Last reply
                              0
                              • H Offline
                                H Offline
                                houmingc
                                wrote on 5 Nov 2014, 14:33 last edited by
                                #15

                                @
                                Error:Could not parse stylesheet of object 0xbfdb6220
                                ui->setupUi(this);
                                setStyleSheet("background-image:url(:/left.png;"
                                "background-repeat:no-repeat;");
                                @

                                [edit: added missing coding tags @ SGaist]

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on 5 Nov 2014, 20:50 last edited by
                                  #16

                                  Your style sheet code is wrong, you are missing a closing parenthesis.

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  0
                                  • H Offline
                                    H Offline
                                    houmingc
                                    wrote on 6 Nov 2014, 02:35 last edited by
                                    #17

                                    I think the link below can solve the problem but i can't access my jpg in resources file.

                                    palette->setBrush(QPalette::Background,(new QBrush((new QPixmap(":/1.jpg")))));

                                    http://www.codeprogress.com/cpp/libraries/qt/showQtExample.php?index=143&key=QMainWindowSetBackgroundImage

                                    1 Reply Last reply
                                    0

                                    • Login

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved