Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How can I put a background image in the first tab of a QTabWidget?
QtWS25 Last Chance

How can I put a background image in the first tab of a QTabWidget?

Scheduled Pinned Locked Moved Unsolved Qt for Python
13 Posts 2 Posters 2.9k 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.
  • I Offline
    I Offline
    Igor86
    wrote on 21 Sept 2022, 07:19 last edited by
    #1

    Hi all!

    I would need to put a background image, stretched as background of the first tab of my QTabWidget. Ideally in the designer, but I think it cannot be done in the designer, so via code would be ok too..

    i tried these things based on what I found online, but none worked:

    self.tabWidget.setStyleSheet("background-image: url(:/test.png);")
    self.tabWidget.setStyleSheet("border-image: url(:/test.png);")
    

    any help is greatly appreciated..

    Thank you!

    J 1 Reply Last reply 21 Sept 2022, 07:38
    0
    • I Igor86
      21 Sept 2022, 07:19

      Hi all!

      I would need to put a background image, stretched as background of the first tab of my QTabWidget. Ideally in the designer, but I think it cannot be done in the designer, so via code would be ok too..

      i tried these things based on what I found online, but none worked:

      self.tabWidget.setStyleSheet("background-image: url(:/test.png);")
      self.tabWidget.setStyleSheet("border-image: url(:/test.png);")
      

      any help is greatly appreciated..

      Thank you!

      J Offline
      J Offline
      JonB
      wrote on 21 Sept 2022, 07:38 last edited by
      #2

      @Igor86
      What type is self.tabWidget? A QTabWidget? If you want it on the first widget in the tab widget then that is what you need to put it on (or even on the first tab if that is what you mean?). Also, have you set up your Python program so that it includes the resources you need for your image file?

      1 Reply Last reply
      0
      • I Offline
        I Offline
        Igor86
        wrote on 21 Sept 2022, 08:53 last edited by
        #3

        @JonB
        Thanks for your answer!
        self.tabwidget is a QTabWidget...

        My goal is to have the image as background of the whole first Tab.

        I have not set up the python program so that it includes the resources needed.. actually I didnt even know I need to do that. I was thinking that it can be accessed by adding the path to the image...

        Thank you

        J 1 Reply Last reply 21 Sept 2022, 09:08
        0
        • I Igor86
          21 Sept 2022, 08:53

          @JonB
          Thanks for your answer!
          self.tabwidget is a QTabWidget...

          My goal is to have the image as background of the whole first Tab.

          I have not set up the python program so that it includes the resources needed.. actually I didnt even know I need to do that. I was thinking that it can be accessed by adding the path to the image...

          Thank you

          J Offline
          J Offline
          JonB
          wrote on 21 Sept 2022, 09:08 last edited by
          #4

          @Igor86
          So try putting your style sheet onto the QWidget you place as the first tab, not on the QTabWidget itself.

          Your path is a "resource path" because it begins with :/. Setting up resources for a Python Qt is not so easy as you do not build an executable. It can be done if you want, but you would have to read how. If you are happy to do this by an external file instead, specify the full path to that file (/..., but not :/...).

          1 Reply Last reply
          0
          • I Offline
            I Offline
            Igor86
            wrote on 21 Sept 2022, 09:13 last edited by
            #5

            Thank you very much! will try and report back!

            1 Reply Last reply
            0
            • I Offline
              I Offline
              Igor86
              wrote on 21 Sept 2022, 09:37 last edited by
              #6

              @JonB

              unfortunately it is not working. I tried it via code and via designer:

              if I set a background-color: #112233; it works. but background-image: url("/test.png"); does not work.
              the image file is saved in the same folder as the script itself.

              886aee30-f28c-4d7a-8bfc-0fbc5ff1baef-image.png

              J 1 Reply Last reply 21 Sept 2022, 09:48
              0
              • I Igor86
                21 Sept 2022, 09:37

                @JonB

                unfortunately it is not working. I tried it via code and via designer:

                if I set a background-color: #112233; it works. but background-image: url("/test.png"); does not work.
                the image file is saved in the same folder as the script itself.

                886aee30-f28c-4d7a-8bfc-0fbc5ff1baef-image.png

                J Offline
                J Offline
                JonB
                wrote on 21 Sept 2022, 09:48 last edited by JonB
                #7

                @Igor86
                And the full path to your file which I stressed you will need is the /test.png that you have written there? I very much doubt it....!

                Get the path working first for a QImage or a QLabel or QPushButton with image or similar, so that you know it works. Then try it with the QWidget.

                1 Reply Last reply
                1
                • I Offline
                  I Offline
                  Igor86
                  wrote on 21 Sept 2022, 10:39 last edited by Igor86
                  #8

                  thank you, I got it working now..

                  but somehting is not entirely right:

                  i applied it to the "tab" widget, but also 2 QLabels contained in the Qwidget got the same background..

                  i'd expect the two squares to be either empty, or transparent..

                  (i did it via code, not in the designer, like this:

                  self.tabWidget.findChild(QWidget, "tab").setStyleSheet("background-image: url(\"" + os.path.dirname(__file__) + "/test.png\");")
                  

                  a2a0e53c-77b2-42b4-a5e6-7b3ea0cb5004-image.png

                  J 1 Reply Last reply 21 Sept 2022, 10:46
                  0
                  • I Igor86
                    21 Sept 2022, 10:39

                    thank you, I got it working now..

                    but somehting is not entirely right:

                    i applied it to the "tab" widget, but also 2 QLabels contained in the Qwidget got the same background..

                    i'd expect the two squares to be either empty, or transparent..

                    (i did it via code, not in the designer, like this:

                    self.tabWidget.findChild(QWidget, "tab").setStyleSheet("background-image: url(\"" + os.path.dirname(__file__) + "/test.png\");")
                    

                    a2a0e53c-77b2-42b4-a5e6-7b3ea0cb5004-image.png

                    J Offline
                    J Offline
                    JonB
                    wrote on 21 Sept 2022, 10:46 last edited by JonB
                    #9

                    @Igor86
                    I guess unqualified background-image: url(...) applies the background image to all descendants of the QWidget.

                    Try QWidget { background-image: url(...); } on it. Does that restrict it to your QWidget, or does it still inherit to QLabels because they are derived from QWidget?

                    If the latter try #FirstPage { background-image: url(...); } and set your QWidget's object name to FirstPage, which you can do at the top of the widget's properties you show in Designer.

                    1 Reply Last reply
                    1
                    • I Offline
                      I Offline
                      Igor86
                      wrote on 21 Sept 2022, 10:47 last edited by
                      #10

                      @JonB

                      I updated my previosu post at the same time as you answered here..

                      J 1 Reply Last reply 21 Sept 2022, 10:48
                      0
                      • I Igor86
                        21 Sept 2022, 10:47

                        @JonB

                        I updated my previosu post at the same time as you answered here..

                        J Offline
                        J Offline
                        JonB
                        wrote on 21 Sept 2022, 10:48 last edited by
                        #11

                        @Igor86
                        In what way does that affect what I said to try?

                        1 Reply Last reply
                        0
                        • I Offline
                          I Offline
                          Igor86
                          wrote on 21 Sept 2022, 10:50 last edited by
                          #12

                          In no way, it was just an information..

                          I tryied what you said, and QWidget {... did not work, but the #FirstPage worked perfectly.. thank you very much for all your help!

                          J 1 Reply Last reply 21 Sept 2022, 11:01
                          1
                          • I Igor86
                            21 Sept 2022, 10:50

                            In no way, it was just an information..

                            I tryied what you said, and QWidget {... did not work, but the #FirstPage worked perfectly.. thank you very much for all your help!

                            J Offline
                            J Offline
                            JonB
                            wrote on 21 Sept 2022, 11:01 last edited by
                            #13

                            @Igor86
                            Good. If you had set its object tab to tab per your code you could obviously have used that for its name/selector, but I think FirstTab is clearer.

                            You might find that if you don't want to give it an object name .QWidget { background-image: url(...); } might have worked. That has a . (dot) before the widget class name. Per https://doc.qt.io/qt-5/stylesheet-syntax.html#selector-types (which you might want to read for these stylesheet things)

                            Class Selector .QPushButton Matches instances of QPushButton, but not of its subclasses.

                            This is equivalent to *[class~="QPushButton"].

                            But then if you did add any further QWidgets onto that page they would be affected.

                            1 Reply Last reply
                            0

                            2/13

                            21 Sept 2022, 07:38

                            11 unread
                            • Login

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