Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide label text for Qt tabs without setting text to empty string

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).

like image 879
Horst Walter Avatar asked Jul 20 '14 15:07

Horst Walter


3 Answers

This can not be done easily. Use empty text.

like image 117
Silicomancer Avatar answered Nov 14 '22 17:11

Silicomancer


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.

like image 28
Prasad Silva Avatar answered Nov 14 '22 15:11

Prasad Silva


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.

like image 1
lpapp Avatar answered Nov 14 '22 17:11

lpapp