Hide close button of a tab in QTabWidget
-
I'm trying to hide/remove the close button of a tab in
QTabWidget
. There's a tab I callWelcome
orGetting started
. I don't want it to be closable.This is the code that I came up with:
auto tabBar = mCentralTabWidget->tabBar(); tabBar->tabButton(0, QTabBar::RightSide)->hide();
Get the
QTabBar
instance of theQTabWidget
, call thetabButton
method in other to return the close button widget and then callhide
. It works, but the problem is that now it has a weird spacing after the tab title:I was wondering, how to get rid of that spacing?
-
Instead of hiding the widget, remove it! :)
tabBar->tabButton(0, QTabBar::RightSide)->deleteLater(); tabBar->setTabButton(0, QTabBar::RightSide, 0);
(tested; works nicely on Linux)
This is essentially what
QTabBar
is doing on each tab any time you setQTabBar::tabsClosable
tofalse
. Ie have a look at https://github.com/qt/qtbase/blob/a37dd93defd91b79fb6730d0ff0515a66a0d3972/src/widgets/widgets/qtabbar.cpp#L2337Note, you should also look at how the code evaluates which side to close too sometime.
Cheers.
-
@Paul-Colby said in Hide close button of a tab in QTabWidget:
tabBar->tabButton(0, QTabBar::RightSide)->deleteLater();
tabBar->setTabButton(0, QTabBar::RightSide, 0);That is amazing, thank you very much.