Important: Please read the Qt Code of Conduct -

[SOLVED] Deleting last tab from QTabWidget

  • Hi,
    I found two topics with similar problem that unfortunately don't solve my problem.
    My program have :

    QList<MyTab*> tabList; - when I add a new tab ui->tabWidget->addTab(newTab), I also add that tab to that list.

    tabWidget - while starting program it contains no tabs, tabs are added meanwhile.

    Tabs have bottom to close a tab (tab widget - closable is set to true)

    And I have:
    @connect(ui->tabWidget,SIGNAL(tabCloseRequest(int)), this, SLOT(closeTab(int)));@

    In closeTab(int index):
    @MyTab *tempTab;

    When I close last tab I get error:
    "Microsoft Visual C++ Runtime Library
    This application has request the Runtime to terminate it in a unusual way.
    Please contact..."

    And in app output:
    "ASSERT failure in QList<T>::operator[]: "index out of range", file ... /QtCore/qlist.h, line 460
    Invalid parameter passed to C runtime function.
    Invalid parameter passed to C runtime function."

    I don't know where problem is, because I thought that I don't cause the situation that index is out of range.

  • Hi,

    bq. Removes the first item in the list and returns it. This is the same as takeAt(0). This function assumes the list is not empty. To avoid failure, call isEmpty() before calling this function.

    That is what is stated, so do so! That will probably solve your crash.
    The thing I would like to mention is that is't not a good practice to keep a QList and a QTabWidget togeter. Then both are holding the data.

  • You mean: checking if a list is empty or not before taking a tab from the list?
    like that:
    @if(!tabList.isEmpty()) tempTab=tabList.takeAt(index)@
    If yes, it doesn't work.

    My QList contains pointers to objects of class MyTab - it's my class that keeps layout, my functions, some data etc.
    TabWidget was created with QtCreator (designer).

    I create a tab when I get signal that someone chose to open it by double clicking. So my slot looks:
    MyTab *newTab=new TabChat(ui->tabWidget, stringText);
    ui->tabWidget->addTab(newTab, newTab->getTabName());

    I start using Qt, so maybe I don;t know that there is better way to keep this tabs to have simple access to it.
    If there is, I will be grateful for any advice :D

  • I have changed my code and now deleting tabs looks like
    @MyTab *tempTab;
    Problem is that, when I have eg. two opened tabs, I delete one and it is deleted (I put qDebug()<<"Object deleted" in destructor).
    Next when I try to delete last opened tab I also see that last tab was deleted but after deleting I get segmentation fault, and program stops at line 713 in qlist.h:
    711 template <typename T>
    712 Q_OUTOFLINE_TEMPLATE QList<t>::QList(const QList<t> &l)
    713 : d(l.d) //here
    How can I check what causes that fault if it happens after deleting last tab.

  • I solved the problem.
    I found that after deleting every tab, one of connection is activated
    @connect(ui->tabWidget, SIGNAL(currentChanged(int)), this,SLOT(showValues(int)));@
    so everything worked fine until I deleted last tab, because then the slot was activated but it tries to work with empty list.

Log in to reply