Results 1 to 3 of 3

Thread: entering and leaving a new event loop will not perform the deferred deletion ?

  1. #1
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    3

    Question entering and leaving a new event loop will not perform the deferred deletion ?

    Hi All,

    In QT document of deleteLater ,it said:

    Note that entering and leaving a new event loop (e.g., by opening a modal dialog) will not perform the deferred deletion; for the object to be deleted, the control must return to the event loop from which deleteLater() was called.

    I am not quite understand what it means, there is an article http://agateau.wordpress.com/2010/09...ctdeletelater/
    ,but it is still confusing me .

    could somebody give me an example for this ?

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: entering and leaving a new event loop will not perform the deferred deletion ?

    The event loop counts "levels" of nesting. When you call exec() on the application object in your main, the level is "1". When you open a modal dialog with a call to QDialog::exec() the level rises to "2". If you open another modal dialog from this one, the level will rise to "3". When you close the dialog it will fall back to "2". If you're on level "2" and you call deleteLater() on some object and then you open the dialog which causes the level to rise, you can't be certain that this dialog will not want to interact with the object you want deleted hence you can't delete it just yet so Qt defers deleting that object until the level drops back to "2".
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    govi1 (3rd February 2012)

  4. #3
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    3

    Default Re: entering and leaving a new event loop will not perform the deferred deletion ?

    Great! That is the best answer I have found !!,Thank you very much ))

    I write a test program for it:

    Qt Code:
    1. #include <QApplication>
    2. #include <QString>
    3. #include <QTimer>
    4. #include <QDebug>
    5. #include <QMetaObject>
    6. #include <QThread>
    7. #include <QDialog>
    8.  
    9. class Object : public QObject
    10. {
    11. Q_OBJECT
    12. public:
    13. Object(){qDebug()<<"** Object contruct **";}
    14. ~Object(){qDebug()<<"** Object destruct **";}
    15. };
    16.  
    17. class DialogLevel3 : public QDialog
    18. {
    19. Q_OBJECT
    20. public:
    21. DialogLevel3():QDialog(){
    22. setWindowTitle("DialogLevel3");
    23. QTimer::singleShot(1000, this, SLOT(f()));
    24. }
    25. public slots:
    26. void f(){
    27. qDebug()<<"[ DialogLevel3"<<QThread::currentThread() <<" ]";
    28. }
    29. };
    30.  
    31.  
    32. class DialogLevel2 : public QDialog
    33. {
    34. Q_OBJECT
    35. public:
    36. DialogLevel2():QDialog(){
    37. setWindowTitle("DialogLevel2");
    38. }
    39. public slots:
    40. void init(){
    41. object = new Object;
    42. qDebug()<<"starting eventloop level2";
    43. QTimer::singleShot(1000, this, SLOT(f()));
    44. exec();
    45. qDebug()<<"return to level1";
    46. }
    47. void f(){
    48. qDebug()<<"[ DialogLevel2"<<QThread::currentThread()<<" ]";
    49. qDebug()<<"call object->deleteLater";
    50. object->deleteLater();
    51. DialogLevel3 dlg2;
    52. qDebug()<<"starting eventloop level3";
    53. dlg2.exec();
    54. qDebug()<<"return to level2";
    55. }
    56. private:
    57. Object *object;
    58. };
    59.  
    60.  
    61.  
    62.  
    63. int main(int argc,char* argv[])
    64. {
    65. QApplication app(argc, argv);
    66. qDebug()<<"[ mainthread"<<QThread::currentThread()<<" ]";
    67. DialogLevel2 dlg1;
    68. QMetaObject::invokeMethod(&dlg1, "init",Qt::QueuedConnection);
    69. qDebug()<<"starting eventloop level1";
    70. app.exec();
    71. }
    72.  
    73. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    Last edited by govi1; 3rd February 2012 at 16:39.

Similar Threads

  1. Replies: 4
    Last Post: 6th August 2011, 01:40
  2. Replies: 1
    Last Post: 22nd July 2010, 09:16
  3. Replies: 10
    Last Post: 15th January 2010, 14:35
  4. Replies: 0
    Last Post: 23rd October 2008, 12:43
  5. Replies: 4
    Last Post: 3rd March 2008, 22:15

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.