Results 1 to 5 of 5

Thread: App crashes when moving a DockWidget outside MainWindow

  1. #1
    Join Date
    Aug 2010
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Question App crashes when moving a DockWidget outside MainWindow

    Hi There, I have got an application that always crashes when having moved a dockwidget outside the mainwindow. When I leave the dockwidget at its iriginal Place on the left docking area the application will close fine.


    App ist build up with this code (Control will instantiate and destroy mainwindow):

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. // app will destroy Control
    6. // because it becomes Control's
    7. // parent object via constructur
    8. Control::getSingleton(&app);
    9. int ret = app.exec();
    10.  
    11. return ret;
    12. } // app Object will be destroyed (and its children)
    To copy to clipboard, switch view to plain text mode 

    IMHO the ~Control() Destructor will be called by the Application at last, so when finishing the application there will be (the destr. ist definitively called):

    Qt Code:
    1. /* singleton-private Constructor */
    2. Control::Control(QObject *parent) :
    3. QObject(parent),
    4. {
    5. ...
    6. if(!mainWindow)
    7. mainWindow = new MainWindow();
    8. ...
    9. }
    10.  
    11. Control::~Control()
    12. {
    13. ...
    14. if(mainWindow) {
    15. delete mainWindow; // will delete the ui including dockwidgets
    16. mainWindow = NULL;
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    The Problem now is that when I have App destroying the mainwindow there comes a segmentation fault along - ending always in qwidget.cpp...

    Qt Code:
    1. ....
    2. else if (!internalWinId() && isVisible()) {
    3. qApp->d_func()->sendSyntheticEnterLeave(this);
    4. ....
    To copy to clipboard, switch view to plain text mode 

    ...where the d_func() with its scoped pointer will fail.

    But: This only happens, when I have moved the DockWidet "Floating" away from its orignial Place in the main window. If I leave it there, everthing is ok.

    As someone may see the stacktrace shows that the Dockwidget will make use of the qapplication d_func(). Am I wrong, or why do at least two parents try to delete the Widget (Mainwindow and App) ?

    Qt Code:
    1. 0 QScopedPointer<QObjectData, QScopedPointerDeleter<QObjectData> >::data qscopedpointer.h 135 0x00ff01e6
    2. 1 qGetPtrHelper<QScopedPointer<QObjectData, QScopedPointerDeleter<QObjectData> > > qglobal.h 2314 0x008a63ff
    3. 2 QApplication::d_func qapplication.h 376 0x00eef9c4
    4. 3 ~QWidget qwidget.cpp 1438 0x008ec5f8
    5. 4 ~QDockWidget qdockwidget.cpp 1181 0x00c51fce
    6. 5 QObjectPrivate::deleteChildren qobject.cpp 1978 0x6a20f1b0
    7. 6 ~QWidget qwidget.cpp 1476 0x008ec6dc
    8. 7 ~QMainWindow qmainwindow.cpp 329 0x00c82888
    9. 8 ~MainWindow mainwindow.cpp 50 0x004018c0
    10. 9 ~Control control.cpp 83 0x00408feb
    11. 10 QObjectPrivate::deleteChildren qobject.cpp 1978 0x6a20f1b0
    12. 11 ~QObject qobject.cpp 975 0x6a20d0f9
    13. 12 ~QCoreApplication qcoreapplication.cpp 642 0x6a1fd127
    14. 13 ~QApplication qapplication.cpp 1118 0x008aa05e
    15. 14 qMain main.cpp 14 0x00401400
    16. 15 WinMain@16 qtmain_win.cpp 131 0x0040e612
    17. 16 main 0 0x0040e338
    To copy to clipboard, switch view to plain text mode 

    Any Comments ?

  2. #2
    Join Date
    May 2010
    Posts
    24
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: App crashes when moving a DockWidget outside MainWindow

    It seems MainWindow has already been destroyed by qApp when Control destructor is called (because MainWindow is a top-level window, qApp destroys it when it quits).

    Instead of that manual delete, you can set your Control object as MainWindow parent, it will be deleted or removed from Control children if qApp get to it first.
    And/or you can use a QPointer<MainWindow> instead of MainWindow* for Control::mainWindow, which would be set to 0 if MainWindow is deleted by qApp.

  3. #3
    Join Date
    Aug 2010
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: App crashes when moving a DockWidget outside MainWindow

    Good Idea, this works fine if I derive my Control Class from QWidget and have a widget Object in the main() routine which will destroy everything as the parent of all. Formerly it Control was a QObject so I can't set it as the QWidget Parent of MainWindow. Although my ideas might appear somewhat academic thank you for your reply.

    Is there a way to have a QObject-derived class as the parent of QMainWindow I do not understand so far.

    Regards, .. frank

  4. #4
    Join Date
    May 2010
    Posts
    24
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: App crashes when moving a DockWidget outside MainWindow

    Quote Originally Posted by franku View Post
    Is there a way to have a QObject-derived class as the parent of QMainWindow I do not understand so far.
    No, there isn't. There is a Q_ASSERT in setParent to prevent that.

    But since Control and QApplication are both singletons, and QApplication already controls the lifetime of mainWindow, you could simply let Control inherit from QApplication.

  5. The following user says thank you to alexisdm for this useful post:

    franku (17th August 2010)

  6. #5
    Join Date
    Aug 2010
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Smile Re: App crashes when moving a DockWidget outside MainWindow

    Ahh, you are right! This works well now. Though I am deleting the mainwindow still myself. Control is now the application .. yes ok. Thank you.

Similar Threads

  1. Replies: 1
    Last Post: 15th August 2010, 22:40
  2. Replies: 0
    Last Post: 24th May 2010, 13:19
  3. Replies: 3
    Last Post: 4th September 2009, 13:49
  4. DockWidget not moving inside QWidget ?
    By aurelius in forum Qt Programming
    Replies: 6
    Last Post: 3rd February 2009, 11:24
  5. DockWidget ?
    By allensr in forum Qt Programming
    Replies: 8
    Last Post: 31st January 2007, 00:58

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.