Results 1 to 4 of 4

Thread: set different StyleSheet for tabs of the same bar

  1. #1
    Join Date
    Jan 2012
    Posts
    29
    Thanks
    7

    Default set different StyleSheet for tabs of the same bar

    Hi all, struggling trying to figure out how to set different StyleSheet for the tabs of the same bar. Specifically I would need to have some tabs of a size, and some others, actually only the last one at the moment, of a smaller size. However, it seems the last always and only applies, even if I specify it differently. So for example, the following does not solve:

    Qt Code:
    1. ui->tabWidget->setStyleSheet("QTabBar::tab { width: 120px; }");
    2. ui->tabWidget->setStyleSheet("QTabBar::tab:last { width: 20px; }");
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. ui->tabWidget->setStyleSheet("QTabBar::tab { width: 120px; }");
    2. ui->tabWidget->setStyleSheet("QTabBar::tab:last { width: 20px; }");
    To copy to clipboard, switch view to plain text mode 

    But I am trying with many other different way with no luck. Also doc did not help me with this, unfortunately.

    My guess is both the settings have to be specified with the same function.

    Thanks for any hint.

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanked 97 Times in 94 Posts

    Default Re: set different StyleSheet for tabs of the same bar

    I am not too familiar with style sheets, but it seems to me that you use QWidget::setStyleSheet() as if it were appending new style rules, while in fact it completely replaces the widget's stylesheet. Therefore I would try the following:
    Qt Code:
    1. ui->tabWidget->setStyleSheet("QTabBar::tab { width: 120px; }\nQTabBar::tab:last { width: 20px; }");
    To copy to clipboard, switch view to plain text mode 
    If you ever need to append a rule to an existing stylesheet you can try:
    Qt Code:
    1. widget->setStyleSheet(widget->styleSheet() + "\nMyNewRule")
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to yeye_olive for this useful post:

    dotjan (25th July 2012)

  4. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: set different StyleSheet for tabs of the same bar

    To my knowledge you can't style individual tabs in a tab bar, rather - styling the tabs based on state (selected, hovered etc).
    QTabBar::tab styles all the tabs in the tab bar.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #4
    Join Date
    Jan 2012
    Posts
    29
    Thanks
    7

    Default Re: set different StyleSheet for tabs of the same bar

    Quote Originally Posted by yeye_olive View Post
    Qt Code:
    1. ui->tabWidget->setStyleSheet("QTabBar::tab { width: 120px; }\nQTabBar::tab:last { width: 20px; }");
    To copy to clipboard, switch view to plain text mode 
    Yes it works, the new line does the job.

    Thanks a lot.

    Quote Originally Posted by high_flyer View Post
    To my knowledge you can't style individual tabs in a tab bar, rather - styling the tabs based on state (selected, hovered etc).
    QTabBar::tab styles all the tabs in the tab bar.
    I see, actually in my case I am lucky I need a different style for the last tab only, so I can QTabBar::tab:last

    Thanks.

Similar Threads

  1. QML tabs
    By user2704 in forum Newbie
    Replies: 0
    Last Post: 25th May 2012, 19:03
  2. how to interact with tabs in qt
    By rahulgogoi in forum Qt Programming
    Replies: 1
    Last Post: 8th August 2011, 13:24
  3. QTabWidgetexpand tabs
    By alexandernst in forum Newbie
    Replies: 1
    Last Post: 17th May 2010, 17:36
  4. Nice looking tabs
    By frikkasoft in forum Qt Programming
    Replies: 4
    Last Post: 6th November 2008, 15:15
  5. Tabs communication
    By therealjag in forum Qt Programming
    Replies: 6
    Last Post: 18th April 2006, 19:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.