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. QMainWindow in QMdiArea does destroy the widget on Close

QMainWindow in QMdiArea does destroy the widget on Close

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 2 Posters 3.8k 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.
  • T Offline
    T Offline
    TinTin82
    wrote on last edited by
    #1

    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
    {
    	Q_OBJECT
    ...
    };
    
    
    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++)
    	{
    	    arr_SWC_MainWindow_SubWin[ui32_counter].ui32_Init((e_INSTRUMENTS_t)ui32_counter);
    
    
    		arr_SWC_MainWindow_SubWin[ui32_counter].setParent(p_QMdiArea);
    		arr_SWC_MainWindow_SubWin[ui32_counter].setWindowFlags(Qt::SubWindow);
    
    		arr_SWC_MainWindow_SubWin[ui32_counter].setAttribute(Qt::WA_DeleteOnClose,false);
    		p_QMdiArea->addSubWindow(&arr_SWC_MainWindow_SubWin[ui32_counter]);
    
    
    		//arr_SWC_MainWindow_SubWin[ui32_counter].setParent(p_QMdiArea);
    
    
    	}
    ...
    

    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,
    TinTin

    mrjjM 1 Reply Last reply
    0
    • T TinTin82

      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
      {
      	Q_OBJECT
      ...
      };
      
      
      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++)
      	{
      	    arr_SWC_MainWindow_SubWin[ui32_counter].ui32_Init((e_INSTRUMENTS_t)ui32_counter);
      
      
      		arr_SWC_MainWindow_SubWin[ui32_counter].setParent(p_QMdiArea);
      		arr_SWC_MainWindow_SubWin[ui32_counter].setWindowFlags(Qt::SubWindow);
      
      		arr_SWC_MainWindow_SubWin[ui32_counter].setAttribute(Qt::WA_DeleteOnClose,false);
      		p_QMdiArea->addSubWindow(&arr_SWC_MainWindow_SubWin[ui32_counter]);
      
      
      		//arr_SWC_MainWindow_SubWin[ui32_counter].setParent(p_QMdiArea);
      
      
      	}
      ...
      

      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,
      TinTin

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      addSubWindow

      The docs
      http://doc.qt.io/qt-4.8/qmdiarea.html#addSubWindow
      says it only be deleted if you do
      subWindow1->setAttribute(Qt::WA_DeleteOnClose);

      So i wonder if u have such code?

      1 Reply Last reply
      1
      • T Offline
        T Offline
        TinTin82
        wrote on last edited by
        #3

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

        mrjjM 1 Reply Last reply
        0
        • T Offline
          T Offline
          TinTin82
          wrote on last edited by
          #4

          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	
          	0x3d0d02	
          	USER32!GetThreadDesktop() at 0x775d6d3a	
          	<...more frames...>	
          
          
          1 Reply Last reply
          0
          • T TinTin82

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

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @TinTin82

            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.

            1 Reply Last reply
            1
            • T Offline
              T Offline
              TinTin82
              wrote on last edited by
              #6

              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 ??

              1 Reply Last reply
              0
              • T Offline
                T Offline
                TinTin82
                wrote on last edited by
                #7

                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.

                mrjjM 1 Reply Last reply
                0
                • T TinTin82

                  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.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  2
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi
                    If i do

                    void MainWindow::on_pushButton_released() {
                    
                      QMdiArea * mdiArea = ui->mdiArea;
                      QMdiSubWindow* subWindow1 = new QMdiSubWindow(mdiArea);
                      QWidget* w = new mywid(subWindow1);
                      w->show();
                      subWindow1->setWidget(w);
                      subWindow1->resize(300,100);
                      subWindow1->setAttribute(Qt::WA_DeleteOnClose,false);
                      subWindow1->show();
                      mdiArea->addSubWindow(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

                    1 Reply Last reply
                    2

                    • Login

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