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. cannot change the QWidget background image.
Qt 6.11 is out! See what's new in the release blog

cannot change the QWidget background image.

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 2.1k 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.
  • Q Offline
    Q Offline
    qtpi
    wrote on last edited by
    #1

    Hey fellas, I've tried to change the background image of a QWidget, so I simply subclass-ed QWidget, and call this->setStyleSheet("QWidget {background-image: url(c:/mybackground.png) }"); in the initializaiton function. it seems nothing has happened.

    But then I found out accidentally that if I deleted the declaration of Macro Q_OBJECT in the declaration of the class, the background will be actually replaced by the desired image. But I need this Macro in a normal QApplication.

    how could it happen? is there any possibility to change the background image via the "this pointer"?

    thnx in advance!

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      hi
      You dont need to subclass QWidget to set stylesheet
      ui->somewidget->setStyleSheet will do 100% the same
      as
      this->setStyleSheet inside a subclass.

      What is wrong is most likely the path "c:/"
      You are pointing to c:. not sure it will load it.
      Normally you would add a qresource file
      using
      New->Qt->Qt resource file
      To that new file via right click and Add existing file you add your bg image.
      then syntax is then
      background-image: url(:/bg.png);
      ( with rest as is.) notice the ":/"

      please see this sample
      https://www.dropbox.com/s/d06k4tpmwgapajc/bg.zip?dl=0

      right click the widget to see the stylesheet. There is menu when in
      Designer.

      Q 1 Reply Last reply
      1
      • mrjjM mrjj

        hi
        You dont need to subclass QWidget to set stylesheet
        ui->somewidget->setStyleSheet will do 100% the same
        as
        this->setStyleSheet inside a subclass.

        What is wrong is most likely the path "c:/"
        You are pointing to c:. not sure it will load it.
        Normally you would add a qresource file
        using
        New->Qt->Qt resource file
        To that new file via right click and Add existing file you add your bg image.
        then syntax is then
        background-image: url(:/bg.png);
        ( with rest as is.) notice the ":/"

        please see this sample
        https://www.dropbox.com/s/d06k4tpmwgapajc/bg.zip?dl=0

        right click the widget to see the stylesheet. There is menu when in
        Designer.

        Q Offline
        Q Offline
        qtpi
        wrote on last edited by
        #3

        @mrjj hey, thnx for the reply. not familiar with qt designer though, i'll have a look.

        mrjjM 1 Reply Last reply
        0
        • Q qtpi

          @mrjj hey, thnx for the reply. not familiar with qt designer though, i'll have a look.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @qtpi
          Well I hope your version have the StyleSheet editor. very handy for
          crafting stylesheets and see the effect at once.

          Q 1 Reply Last reply
          0
          • mrjjM mrjj

            @qtpi
            Well I hope your version have the StyleSheet editor. very handy for
            crafting stylesheets and see the effect at once.

            Q Offline
            Q Offline
            qtpi
            wrote on last edited by
            #5

            @mrjj said:

            hi I tried to add qt source file. oddly enough the same thing happened, once I deleted the Q_OBJECT Macro, the background will be set correctly, if this Macro is declared the background will not be changed.

            I'll try the qt designer afterwards. I'll give a feed back afterwards.

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @qtpi said:

              Q_OBJECT

              You will most likely want this so i hope the qrc works for you.

              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