简体   繁体   中英

delete qtabwidget

my program keeps crashing with returncode 0. The cause is somewhere in my qtabwidget but I can't find the error.

 QTabWidget *layout_tabs;

// create tabs
void myclass::fill_tabs(void)
{
  kill_tabs(); // remove old tabs 
  layout_tabs = new QTabWidget();

  // program adds content into a few tabs, like:
  // widgets created, content created, put into layout, put into widget..
  layout_tabs->addTab(widget, "description");
  layout_tabs->addTab(widget2, "description2");

  layout_tabs->show();
}

void myclass::kill_tabs(void)
{
  if(layout_tabs==nullptr)
    return;
  layout_tabs->hide();

  QWidget *window;

  for ( int i=layout_tabs->count()-1; i>=0; --i)
    {
        window = layout_tabs->widget(i); // remember widget
        layout_tabs->removeTab(i); // remove tab
        free(window); // remove widget
    }

  free(layout_tabs); // remove qtabwidget
  layout_tabs=nullptr;
}

the filltabs() function is used a few times. The old tabwidget is destroyed and a new is created. It does not matter if I don't delete the tabwidget, but remove only the tabs. The program still exits with returncode 0.

You call free(layout_tabs) but you allocate it with operator new() . You should deallocate it with delete layout_tabs instead. I don't see how your window variable is allocated but you should check if it too should be deallocated with operator delete() , or if your QTabWidget owns its memory (ie if it is responsible for managing that memory).

Set the QApplication::quitOnLastWindowClosed to false.

#include <QApplication>

// ...

qApp->setQuitOnLastWindowClosed (false);

Or you can go and set the container for your tabs (the main window/ main widget) to have the property of Qt::WA_QuitOnClose set to false.

myWidget->setAttribute(Qt::WA_QuitOnClose, false);

Either of those should fix it. Also returning with "0" is not a crash. Zero typically indicates a normal exit.

http://qt-project.org/doc/qt-4.8/qapplication.html#quitOnLastWindowClosed-prop

http://qt-project.org/doc/qt-4.8/qt.html#WidgetAttribute-enum

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM