Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. How can i change the background of a single tab?
Forum Updated to NodeBB v4.3 + New Features

How can i change the background of a single tab?

Scheduled Pinned Locked Moved Unsolved Qt 6
7 Posts 2 Posters 108 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 last edited by
    #1

    Hi all!

    hope this is the correct forum..

    i am facing some issues in trying to change the background color of a single tab in pyqt6.

    my code is this:

    self.tabWidget.findChild(QtWidgets.QWidget, "myTab").setStyleSheet("background-color: red;")
    

    any help is greatly appreciated!

    JonBJ 1 Reply Last reply
    0
    • I Igor86

      Hi all!

      hope this is the correct forum..

      i am facing some issues in trying to change the background color of a single tab in pyqt6.

      my code is this:

      self.tabWidget.findChild(QtWidgets.QWidget, "myTab").setStyleSheet("background-color: red;")
      

      any help is greatly appreciated!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote last edited by
      #2

      @Igor86
      And what is the issue with your code? And when you say "tab" are you meaning the "page"/"body" of a tab or the "header"/"title" of a tab?

      1 Reply Last reply
      0
      • I Offline
        I Offline
        Igor86
        wrote last edited by
        #3

        @JonB the tab stays the same, no red background. what i need is just the tab header/title to have a red background. not the page. the code above has no effect at all.. the tab exists, and the child is also found...

        JonBJ 1 Reply Last reply
        0
        • I Igor86

          @JonB the tab stays the same, no red background. what i need is just the tab header/title to have a red background. not the page. the code above has no effect at all.. the tab exists, and the child is also found...

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote last edited by JonB
          #4

          @Igor86
          And exactly what widget have you set and retrieved as myTab? We don't know!

          I believe you need to be looking in a QTabBar for your "title" tabs. I think setTabTextColor(index, color) will change an individual tab's title/text color. From a stylesheet you probably need to set QTabBar::tab { background-color: red; }. That would set color on all tabs, I don't know about an individual one from stylesheet.

          I 1 Reply Last reply
          0
          • JonBJ JonB

            @Igor86
            And exactly what widget have you set and retrieved as myTab? We don't know!

            I believe you need to be looking in a QTabBar for your "title" tabs. I think setTabTextColor(index, color) will change an individual tab's title/text color. From a stylesheet you probably need to set QTabBar::tab { background-color: red; }. That would set color on all tabs, I don't know about an individual one from stylesheet.

            I Offline
            I Offline
            Igor86
            wrote last edited by
            #5

            @JonB said in How can i change the background of a single tab?:

            And exactly what widget have you set and retrieved as myTab? We don't know!

            self.tabWidget.findChild(QtWidgets.QWidget, "myTab").setStyleSheet("background-color: red;")
            

            self.tabWidget is a QTabWidget.
            it finds the child with the name "myTab" and that is a Tab. Unfortunately changing the text is not enough for me, as i need to change the background. and youre right, the stylesheet applies to all tabs, not just one..

            JonBJ 1 Reply Last reply
            0
            • I Igor86

              @JonB said in How can i change the background of a single tab?:

              And exactly what widget have you set and retrieved as myTab? We don't know!

              self.tabWidget.findChild(QtWidgets.QWidget, "myTab").setStyleSheet("background-color: red;")
              

              self.tabWidget is a QTabWidget.
              it finds the child with the name "myTab" and that is a Tab. Unfortunately changing the text is not enough for me, as i need to change the background. and youre right, the stylesheet applies to all tabs, not just one..

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote last edited by JonB
              #6

              @Igor86
              So as I understand it QTabBar has methods to set individual tab text (foreground) color but not background. And stylesheet QTabBar::tab { background-color: red; } does what you want but sets all tabs.

              Read through the (very long) QTabWidget, setting colour of tab itself. I did not digest it all, there may be approaches there. @J.Hilk's "cheaty" QTabBar::tab [whatsThis="aTabWithErrors"]{ may work but leveraging the whatsThis property. There are various other possibilities discussed there, I will leave you to investigate them.

              Another possibility: the QTabBar you can access from QTabWidget has tabButton() & setTabButton() methods to access/set a QWidget for each tab. Assuming that is the widget for the "tab" itself you could presumably access the existing one or replace with your own where you can set its background color, either from stylesheet or code?

              https://forum.qt.io/topic/59018/background-color-of-a-specific-tab-of-qtabbar-qtabwidget also seems to discuss this. And the final post at https://forum.qt.io/post/822309 by @Dariusz claims to be working Python code overriding def paintEvent() to achieve just what you want?

              1 Reply Last reply
              0
              • I Offline
                I Offline
                Igor86
                wrote last edited by
                #7

                i have overridden the paint event to do what i need. thank you very much for your help! it works now

                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