Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Hide close button of a tab in QTabWidget
Forum Update on Monday, May 27th 2025

Hide close button of a tab in QTabWidget

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 5.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mr Gisa
    wrote on last edited by Mr Gisa
    #1

    I'm trying to hide/remove the close button of a tab in QTabWidget. There's a tab I call Welcome or Getting 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 the QTabWidget, call the tabButton method in other to return the close button widget and then call hide. It works, but the problem is that now it has a weird spacing after the tab title:

    0_1520239233638_6d7789c5-4826-49be-8568-688dbf57e405-image.png

    I was wondering, how to get rid of that spacing?

    1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #2

      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 set QTabBar::tabsClosable to false. Ie have a look at https://github.com/qt/qtbase/blob/a37dd93defd91b79fb6730d0ff0515a66a0d3972/src/widgets/widgets/qtabbar.cpp#L2337

      Note, you should also look at how the code evaluates which side to close too sometime.

      Cheers.

      1 Reply Last reply
      4
      • M Offline
        M Offline
        Mr Gisa
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved