Hide label text for Qt tabs without setting text to empty string
Asked Answered
V

3

5

I need a QTabWidget with icons only:

How can I hide the label text of a tab in Qt? I cannot set the text to an empty string (""), as I am using docked widgets ( QDockWidget ) and the label text is set automatically (and I need it if the widget is floating).

But in tabbed mode I just want to display the icons (of the tabs). Possible approaches:

  1. Font size to 0?
  2. I need to create my own bar class and override the paint event as here

Anything easier / cleaner?

--- Edit ---

Ok, the "set window title to empty string, and reset it the original text" approach works. I am using the topLevelChanged signal for this. However, it has some drawbacks, as the empty text still occupies some space. Another issue, with the text the tooltip is gone, and I cannot set it back.

Still some space consumed

What I am currently trying is something in-between the "text empty" and Prasad Silva's approach. I try to identify the text label inside the tab and set its size to 0, then reset it. It's slightly different, but would keep the text intact.

Btw, I see a line on top of my tabs, any idea what this is (where it comes from)? Edit: There seems to be no "easy way" (style sheet, attribute) for this, see Hiding bottom line in QTabBar

Maybe I will create the whole tab bar on my own, as the automatically generated stuff is just too hard to handle (agree with PS on this).

Verse answered 20/7, 2014 at 15:26 Comment(4)
Why exactly cannot you leave the string empty when using addTab?Carlocarload
Because the tab bar is generated automatically when using it with QDockWidget, and the label text is set automatically in that same step. qt-project.org/doc/qt-5.3/qtabwidget.htmlVerse
If you do not want to see text, why cannot you set empty text, and when you want to see it, set it back? I do not think there is anything in the API for this not so common case. Cannot you do it yourself the clearing and setting?Carlocarload
Yes, likely I could. But with many widgets this is tedious, and I was looking for an easier solution. But it might be my only chance.Verse
P
1

If you do not want to see the text, you can set it to an empty text after saving the current text, and when you want to see it again, restore it from the stored variable.

I do not think there is anything in the API for this not so common case, which means you will need to do it yourself.

Now, you could claim that it is tedious to do for many widgets, but on the other hand, you could write a simple hash define or inline function to do this repetitive work for you, which would only result a one-liner call, basically, which you would need to use anyway when changing the state.

Pathy answered 21/7, 2014 at 13:11 Comment(1)
Using the mentioned approach, however - as stated above - it has some drawbacks.Verse
P
3

This can not be done easily. Use empty text.

Psychoactive answered 20/7, 2014 at 19:54 Comment(1)
Same issue as above, problem is that I cannot set the text to Empty as it is not under my control. When using tabbed QDockWidgets the text is automatically set based on the title of the corresponding QDockWidget. If I set this to Empty, then there is no text when the widget is floating. I could set/reset the text in a topLevelChanged slot, but this becomes tedious with multiple QDockWidgets.Verse
E
3

The way I solved something like was to create a QDockWidget subclass that installed a QWidget subclass as the titlebar (via setTitleBarWidget). This gave me control over showing/hiding the text in the titlebar when the dock widget fires topLevelChanged, dockLocationChanged and visiblityChanged.

This is really a big hack to get around the fact that Qt has refused to expose a public API for the docking system. We have since moved on to a custom docking implementation due to these limitations.

Eatables answered 22/7, 2014 at 14:18 Comment(1)
It is indeed a huge hack, and even worse than just changing the string forth and back. Please do not do it.Carlocarload
P
1

If you do not want to see the text, you can set it to an empty text after saving the current text, and when you want to see it again, restore it from the stored variable.

I do not think there is anything in the API for this not so common case, which means you will need to do it yourself.

Now, you could claim that it is tedious to do for many widgets, but on the other hand, you could write a simple hash define or inline function to do this repetitive work for you, which would only result a one-liner call, basically, which you would need to use anyway when changing the state.

Pathy answered 21/7, 2014 at 13:11 Comment(1)
Using the mentioned approach, however - as stated above - it has some drawbacks.Verse

© 2022 - 2024 — McMap. All rights reserved.