Results 1 to 3 of 3

Thread: static QEventLoop strange behavior

  1. #1
    Join Date
    Apr 2008
    Location
    Russia, Moscow
    Posts
    86
    Thanks
    2
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4

    Question static QEventLoop strange behavior

    I have this code:

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QtCore/QtGlobal>
    3. #include <QtCore/QtDebug>
    4. #include <QtCore/QEventLoop>
    5. #include <QtCore/QTimer>
    6.  
    7. #define USE_LOOP2 0
    8.  
    9. #if USE_LOOP2 == 1
    10. static QEventLoop *loop2=0;
    11. #endif
    12.  
    13. class Base
    14. {
    15. public:
    16. Base()
    17. {
    18. #if USE_LOOP2 == 1
    19. loop2 = new QEventLoop;
    20. #endif
    21. };
    22. virtual void foo() = 0;
    23. static QEventLoop * eventLoop()
    24. {
    25. #if USE_LOOP2 == 1
    26. return loop2;
    27. #else
    28. if (loop) {
    29. return loop;
    30. } else {
    31. return new QEventLoop(qApp);
    32. }
    33. #endif
    34. }
    35. virtual ~Base(){};
    36. private:
    37. #if USE_LOOP2 != 1
    38. static QEventLoop *loop;
    39. #endif
    40. };
    41.  
    42. #if USE_LOOP2 != 1
    43. QEventLoop *Base::loop=0;
    44. #endif
    45.  
    46. class A : public Base
    47. {
    48. public:
    49. A(){}
    50. virtual ~A(){}
    51. void foo()
    52. {
    53. qDebug() << "Run exec...";
    54. qDebug() << eventLoop()->exec();
    55. qDebug() << "Exit exec...";
    56. }
    57. };
    58.  
    59. int main(int argc, char *argv[])
    60. {
    61. QCoreApplication app(argc, argv);
    62. A a;
    63. QTimer::singleShot(6000, a.eventLoop(), SLOT(quit()));
    64. a.foo();
    65. QTimer::singleShot(6000, &app, SLOT(quit()));
    66. return app.exec();
    67. }
    To copy to clipboard, switch view to plain text mode 

    If i use global static pointer loop2 - all work. If i use static loop pointer as member for class Base - event never exit (in my other program exec() just return -1 value). For test you can change USE_LOOP2 to 0 or 1 and try yourself.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: static QEventLoop strange behavior

    assign the "new QEventLoop" to your variable loop in or before line 31...

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

    SABROG (29th July 2009)

  4. #3
    Join Date
    Apr 2008
    Location
    Russia, Moscow
    Posts
    86
    Thanks
    2
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4

    Default Re: static QEventLoop strange behavior

    Thanks, solved. My mistake, need more sleep.

Similar Threads

  1. Very strange behavior of QWidget in Qt Designer
    By THRESHE in forum Qt Programming
    Replies: 10
    Last Post: 13th August 2008, 16:19
  2. Replies: 16
    Last Post: 23rd May 2008, 10:12
  3. Strange QTcpSocket behavior (debugged with Ethereal)
    By mdecandia in forum Qt Programming
    Replies: 23
    Last Post: 28th May 2007, 20:44
  4. Strange behavior with Drag and Drop (not always drops)
    By Mona Shokrof in forum Qt Programming
    Replies: 1
    Last Post: 27th May 2007, 18:00
  5. QComboBox +SUSE10.0 +qt4.1 strange behavior
    By antonio.r.tome in forum Qt Programming
    Replies: 6
    Last Post: 20th March 2006, 17:49

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.