Important: Please read the Qt Code of Conduct -

QMainWindow in QMdiArea does destroy the widget on Close

  • Hi,

    i have a QMdiArea and a class derivated from QMainWindow which in want to add to the AMdiArea.
    The Class derivated from QMainWindow Needs a toolbar and the stuff of the QMainWindow.
    But if i Close a instance from the QMainWindow derivate, the object ist deleted.
    But it should not !! How can i solve this ??

    class SWC_MainWindow_SubWin_t : public QMainWindow
    class SWC_MainWindow_t : public QMainWindow
    uint32 ui32_Init(void const * const vp_in, void * const vp_out);
    SWC_MainWindow_SubWin_t arr_SWC_MainWindow_SubWin[e_MAX_NUM_INSTRUMENTS];
    uint32 SWC_MainWindow_t::ui32_Init(void const * const vp_in, void * const vp_out)
    	for( ui32_counter = 0x00000000; ui32_counter < e_MAX_NUM_INSTRUMENTS; ui32_counter++)

    If i Close a SWC_MainWindow_SubWin_t then the destructor of SWC_MainWindow_SubWin_t is called.
    And on closing the application it crashes..

    Would be nice if anyone can help me.

    Best Regards,

  • Lifetime Qt Champion

    @TinTin82 said in QMainWindow in QMdiArea does destroy the widget on Close:


    The docs
    says it only be deleted if you do

    So i wonder if u have such code?

  • Hi mrjj,

    yes, thats right,
    I have added this line of code because i tought this could solve the Problem...
    I set the Attribut explicitly to false, so it sould not be deleted....

  • This is the callstack with a breakpoint int the destructor of SWC_MainWindow_SubWin_t.

    Thread #1 0 (Suspended : Breakpoint)	
    	SWC_MainWindow_SubWin_t::~SWC_MainWindow_SubWin_t() at SWC_MainWindow_SubWin.cpp:47 0x402e87	
    	QObjectPrivate::deleteChildren() at qobject.cpp:1.970 0xbc93f4	
    	QWidget::~QWidget() at qwidget.cpp:1.694 0x473291	
    	QMdiSubWindow::~QMdiSubWindow() at qmdisubwindow.cpp:2.287 0x56dca9	
    	QMdiSubWindow::~QMdiSubWindow() at qmdisubwindow.cpp:2.294 0x56dcdf	
    	qDeleteInEventHandler() at qobject.cpp:4.482 0xbcf44c	
    	QObject::event() at qobject.cpp:1.254 0xbc7f01	
    	QWidget::event() at qwidget.cpp:9.225 0x485eb4	
    	QMdiSubWindow::event() at qmdisubwindow.cpp:2.921 0x56fdc2	
    	QApplicationPrivate::notify_helper() at qapplication.cpp:3.799 0x4b8432	
    	QApplication::notify() at qapplication.cpp:3.762 0x4b8278	
    	QCoreApplication::notifyInternal2() at qcoreapplication.cpp:988 0xbea0fb	
    	QCoreApplication::sendEvent() at qcoreapplication.h:231 0xe46517	
    	QCoreApplicationPrivate::sendPostedEvents() at qcoreapplication.cpp:1.649 0xbeb347	
    	QEventDispatcherWin32::sendPostedEvents() at qeventdispatcher_win.cpp:1.294 0xbab3be	
    	QWindowsGuiEventDispatcher::sendPostedEvents() at qwindowsguieventdispatcher.cpp:81 0x76b42b	
    	qt_internal_proc(HWND__*, unsigned int, unsigned int, long)@16 at qeventdispatcher_win.cpp:443 0xba867a	
    	gapfnScSendMessage() at 0x775d62fa	
    	USER32!GetThreadDesktop() at 0x775d6d3a	
    	<...more frames...>	

  • Lifetime Qt Champion


    I think its some sort of owner delete.

    The docs says

    "The widget can be either a QMdiSubWindow or another QWidget (in which case the MDI area will create a subwindow and set the widget as the internal widget)."

    Since you are using mainwindow, its means its a Widget as its NOT a QMdiSubWindow . Hence a QMdiSubWindow is created for you and used for your widget.

    It also states that
    "When you create your own subwindow, you must set the Qt::WA_DeleteOnClose widget attribute if you want the window to be deleted when closed in the MDI area. If not, the window will be hidden and the MDI area will not activate the next subwindow."

    So if you are 100% sure this is disabled, im not sure why it should be removed.

  • Isn't it possible that the rison is because i derivate from a QMainWindow class ??
    May be there are some Slots connected on quit wich sould not ??

  • For a workarround i have overwritten the Close Event and ignored it.
    But in this case i can not Close any subwindow.

    The behavior sould be, when i Close one of the subwindows it schould turn to be not visible, but it should not be destructet.

  • Lifetime Qt Champion

    The qapplication has as setting that it will close when last top level widget (window) is closed but im not aware of any directly to qmainwindow.

    It could give from using a MainWindow as widget but in other use cases, there was no issues using QMainWin asa plain widgte.
    I have however, not tried with mdi as it looks wrong on win 10.

  • Lifetime Qt Champion

    If i do

    void MainWindow::on_pushButton_released() {
      QMdiArea * mdiArea = ui->mdiArea;
      QMdiSubWindow* subWindow1 = new QMdiSubWindow(mdiArea);
      QWidget* w = new mywid(subWindow1);

    The mywid is not deleted. In all other cases it was. If u let mdi create the subwindow by giving the widget directly.

    alt text

Log in to reply