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?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
17 Posts 8 Posters 120.2k Views 1 Watching
  • 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 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 last edited by
      #4

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

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DenisKormalev
        wrote on 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 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 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 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 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 last edited by
                  #10

                  Thanks Rahul

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mario
                    wrote on 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 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 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 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 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 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 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