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. QFrame setStyleSheet background-image not working on Windows
QtWS25 Last Chance

QFrame setStyleSheet background-image not working on Windows

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 2 Posters 355 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.
  • R Offline
    R Offline
    retconx
    wrote on last edited by retconx
    #1

    Hi,

    frame = QFrame()
    frame.setStyleSheet("background-image:url(<path-to-png-image>); background-position:center; background-repeat:no-repeat;")

    is working on MacOs but not on Windows.

    Does anyone has an idea, why?

    jsulmJ 1 Reply Last reply
    0
    • R retconx

      Hi,

      frame = QFrame()
      frame.setStyleSheet("background-image:url(<path-to-png-image>); background-position:center; background-repeat:no-repeat;")

      is working on MacOs but not on Windows.

      Does anyone has an idea, why?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @retconx said in QFrame setStyleSheet background-image not working on Windows:

      Does anyone has an idea, why?

      Well, what exactly is path-to-png-image? Where is the image stored?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      R 1 Reply Last reply
      0
      • jsulmJ jsulm

        @retconx said in QFrame setStyleSheet background-image not working on Windows:

        Does anyone has an idea, why?

        Well, what exactly is path-to-png-image? Where is the image stored?

        R Offline
        R Offline
        retconx
        wrote on last edited by retconx
        #3

        @jsulm
        It is stored in an folder "pictures" in the main application folder:

        basedir = os.path.dirname(__file__)
        os.path.join(basedir, "pictures", "image.png")

        path-to-png-image is a .png-file

        jsulmJ 2 Replies Last reply
        0
        • R retconx

          @jsulm
          It is stored in an folder "pictures" in the main application folder:

          basedir = os.path.dirname(__file__)
          os.path.join(basedir, "pictures", "image.png")

          path-to-png-image is a .png-file

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @retconx You did not answer first question.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          R 1 Reply Last reply
          0
          • jsulmJ jsulm

            @retconx You did not answer first question.

            R Offline
            R Offline
            retconx
            wrote on last edited by retconx
            #5

            @jsulm
            path-to-png-image is os.path.join(basedir, "pictures", "image.png")

            image.png is an usual .png-image file

            1 Reply Last reply
            0
            • R retconx

              @jsulm
              It is stored in an folder "pictures" in the main application folder:

              basedir = os.path.dirname(__file__)
              os.path.join(basedir, "pictures", "image.png")

              path-to-png-image is a .png-file

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @retconx Did you make sure the path is valid?
              You also did not show how you put the path into the style-sheet.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              R 1 Reply Last reply
              0
              • jsulmJ jsulm

                @retconx Did you make sure the path is valid?
                You also did not show how you put the path into the style-sheet.

                R Offline
                R Offline
                retconx
                wrote on last edited by
                #7

                @jsulm The original code line is:
                frame.setStyleSheet("background-image:url(" + os.path.join(basedir, "pictures", "image.png") + "); background-position:center; background-repeat:no-repeat;")

                Pasting the output of print(os.path.join(basedir, "pictures", "image.png")) to Windows Explorer opens the picture. So the path should be valid.

                jsulmJ 1 Reply Last reply
                0
                • R retconx

                  @jsulm The original code line is:
                  frame.setStyleSheet("background-image:url(" + os.path.join(basedir, "pictures", "image.png") + "); background-position:center; background-repeat:no-repeat;")

                  Pasting the output of print(os.path.join(basedir, "pictures", "image.png")) to Windows Explorer opens the picture. So the path should be valid.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @retconx I'm not sure but maybe the problem is that Windows paths contain back-slashes? Try to put the same path but with forward slashes directly (just to see whether it makes a difference).

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  R 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @retconx I'm not sure but maybe the problem is that Windows paths contain back-slashes? Try to put the same path but with forward slashes directly (just to see whether it makes a difference).

                    R Offline
                    R Offline
                    retconx
                    wrote on last edited by
                    #9

                    @jsulm No difference, unfortunately.
                    That is acutally the reason why I use os.path.join in order to get backslashes for Windows an forwardslashes for Mac/Linux.
                    And as I said, the path with backslashes works in Windows Explorer.

                    Maybe it has to do with any QFrame properties (shape, style...) which have to be set in Windows to show background-images? I will try a bit...

                    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