Nominate our 2022 Qt Champions!

[SOLVED] QTabWidget remove tab problem!

  • Hi,

    In my application I use a QTabWidget instance as the QMainWindow central area. The tabwidget contains n tabs.
    This slot is to remove a tab :

    void MainWindow::closeTab(int index)
    QWidget * tab = m_parentTabWidget->widget(index);
    //delete tab;

    When I close the last tab (m_parentTabWidget->count() == 1), the application crashes and I get the Error : Segmentation fault.

    help !

  • Is there a child widget for the TabWidget? Maybe the SegFault is from the orphan widget?

  • No, there is no child widget for TabWidget !

    The error occurs only when I remove the last tab. For example, suppose that
    I have three tab in the TabWidget. When i remove tab 1, there is no error.
    When i remove tab 2, there is no error. But the application crashes when i remove
    tab 3. Also, when i call the method clear(), to remove all tabs I have the some error !

  • Lifetime Qt Champion


    I would say: you don't have a widget at that index, it will be 1. It works twice because you have 3 widgets.

    A rule of thumb: never delete something that you havent check for its existence.

    Hope it helps

  • Hi,
    Even this code led to the some error :
    connect(this, SIGNAL(tabCloseRequested(int)),
    this, SLOT(closeTab(int)));

    void TabWidget::closeTab(int index)

    any suggestion ?

  • Use valgrind and check for memory leaks and null pointers.

    You can also check your code by Cppcheck.

  • Is it possible at all to have a tab widget with zero tabs?

    Maybe you just remove the tab widget altogether, or at least hide it, instead?

    Also, what if you change your code to:
    @void MainWindow::closeTab(int index)
    if((index < 0) || (index >= m_parentTabWidget->count()))

    If that doesn't reveal anything, make a DEBUG build and see where exactly it crashes...

  • Hi friends,

    bq. MuldeR wrote : Is it possible at all to have a tab widget with zero tabs?

    What made me crazy, is the fact that initially the TabWidget doesn't contain any tab and the application starts perfectly. The class document does not mention this trick. So, I have thought to my web Browser! When I close the last opened tab the browser close itself automatically! So I have already changed the TabWidget like this :

    void MainWindow::closeTab(int index)
    if(m_parentTabWidget->count() == 1)



    thnk you :)

    bq. Hostel wrote : Use valgrind and check for memory leaks and null pointers.

    It would be a good tool !
    Have you a good and simple quick-start other than the official one ?

    thanks. :)

  • I don't think you need necessarily need something like Valgrind here.

    Just make a DEBUG build of your app, link it with the DEBUG Qt libraries and then run your app in a debugger.

    As soon as it crashes, check the callstack...


    Anyway, if you are on Linux and want to try Valgrind, then see here:

    Though this is more for finding memory leaks, uninitialized memory reads and stuff...

    As long as your app does crash (reproducible), thing are usually straight forward in a "normal" debugger.

  • OK thanks. :)

  • Hello!
    I have the same problem and found this thread.
    Is there a better solution than just not deleting the last tab?
    I have a class which is derived from QTabWidget. If I try to close the last tab (with "removeTab" or "clear"), the app crashes because of a segfault.
    I dont think i handle some pointers wrong, because i can close every tab, as long as if there are more than just one.
    My TabWidget starts with zero tabs, so why it is not possible to delete the last one?
    Any suggestions?
    Thank you.

  • Hi there!

    I know this thread is quite old, but I encountered a similar problem.

    The issue for me was that I had registered a slot to handle the QTabWidget's 'currentChanged' signal.
    When the last tab is closed in a QTabWidget, the 'currentChanged' signal is emitted with -1 as the argument
    for the 'index' parameter, but the slot I had created to handle this signal was trying to access a tab (and no tabs exist
    at this point).

    So, I simply added the line:

    if (index < 0) return; 

    to the start of the slot, and voila! Problem solved!

    I hope that helps someone :)

Log in to reply