How to insert a space between two tabs in a QTabWidget?
Asked Answered
P

2

10

I'd like to insert a space to separate the second tab from the third. Is this possible? I've tried some stylesheets but as this widget is kind of new to me I've had no success so far.

Psychochemical answered 3/2, 2013 at 19:9 Comment(0)
S
19

Yes, it's possible, but not so obvious. Here's one way I came up with some time ago.

Whenever you need a space between two tabs, insert a new tab between the tabs and make the new tab disabled by calling QTabWidget::setTabEnabled(int index, bool enable). This way the new tab will no longer be possible to interact with (unless the tab is the only one present). You will also need to set the following style sheet on your QTabWidget:

 QTabBar::tab:disabled {
    width: 100px;
    color: transparent;
    background: transparent;
 }

This style sheet makes the new tab invisible (actually it makes all disabled tabs invisible, so unless that's OK, this method will not work for you). You may adjust the width of the space by changing the value for width in the style sheet.

The result:

QTabWidget with space between tabs

Skull answered 3/2, 2013 at 21:36 Comment(1)
You should get +100 for this hack!Moltke
G
4

If you don't want to create an invisible tab as in the accepted answer, here's a way to do it using only style sheets. Similarly, adjust the pixel amount for more or less spacing between tabs.

QTabBar::tab {
    margin-left:2px;
    margin-right:2px;
}

enter image description here

QTabBar::tab {
    margin-left:40px;
    margin-right:40px;
}

enter image description here

Glasser answered 7/5, 2019 at 2:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.