Results 1 to 10 of 10

Thread: is it possible to stay on a user defined infinite loop?

  1. #1
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default is it possible to stay on a user defined infinite loop?

    Is it possible for a qtopia application to stay on an infiniteIloop from the time of its loading?

    When i called the loop in the constructor the window itself is no loading....

    Mahe2310

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: is it possible to stay on a user defined infinite loop?

    if you are calling an infinite loop in your constructor, ofcourse the widgwt never gets contructed...
    But it could be I missunderstood you.
    Post the code.

  3. #3
    Join Date
    Feb 2006
    Posts
    21
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Re: is it possible to stay on a user defined infinite loop?

    Doing an infinite loop in your constructor wouldn't work. The constructor needs to return. Assuming that the object being constructed is a QObject-derivative, you could do use the QTimer::singleShot method to allow the constructor to return and yet still call the method with your infinite loop:

    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. // Constructor
    6. MyWidget(QWidget *parent);
    7.  
    8. private slots:
    9. void MyInfiniteLoop();
    10. };
    11.  
    12. MyWidget::MyWidget(QWidget *parent) :
    13. QWidget(parent)
    14. {
    15. // Do whatever you need to do here.
    16.  
    17. // Use a zero-timeout single shot timer here to allow the
    18. // constructor to exit before MyInfiniteLoop gets called.
    19. QTimer::singleShot(0, this, SLOT(MyInfiniteLoop()));
    20. }
    21.  
    22. void MyWidget::MyInfiniteLoop()
    23. {
    24. while(true)
    25. {
    26. // Do something interesting
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: is it possible to stay on a user defined infinite loop?

    this is bad design, both methods.
    You should never call an infinite loop in a constructor.
    make the loop in a public method/slot and call normally after construction.
    There can be no reason for calling an infinite loop in a constructor.
    And in a Qt program, if you use an infinite loop, it would be best to use a QThread obeject to house it in its run() method.

  5. #5
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: is it possible to stay on a user defined infinite loop?

    Quote Originally Posted by Ben.Hines
    Doing an infinite loop in your constructor wouldn't work. The constructor needs to return. Assuming that the object being constructed is a QObject-derivative, you could do use the QTimer::singleShot method to allow the constructor to return and yet still call the method with your infinite loop:

    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. // Constructor
    6. MyWidget(QWidget *parent);
    7.  
    8. private slots:
    9. void MyInfiniteLoop();
    10. };
    11.  
    12. MyWidget::MyWidget(QWidget *parent) :
    13. QWidget(parent)
    14. {
    15. // Do whatever you need to do here.
    16.  
    17. // Use a zero-timeout single shot timer here to allow the
    18. // constructor to exit before MyInfiniteLoop gets called.
    19. QTimer::singleShot(0, this, SLOT(MyInfiniteLoop()));
    20. }
    21.  
    22. void MyWidget::MyInfiniteLoop()
    23. {
    24. while(true)
    25. {
    26. // Do something interesting
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

    thank you,
    That is a good method.

    Mahe2310

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: is it possible to stay on a user defined infinite loop?

    @Mahe2310
    Could you explain why it is you need an infinite loop in a costructor?
    I am sure if you explain what it is you want to acheive it can be done in a better way.

  7. #7
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: is it possible to stay on a user defined infinite loop?

    Quote Originally Posted by high_flyer
    @Mahe2310
    Could you explain why it is you need an infinite loop in a costructor?
    I am sure if you explain what it is you want to acheive it can be done in a better way.

    Hi High_Flyer,

    Myself want to handle the qtopia key events and some message queue events simultaneously.
    I mean, handle them always. So I thought of going for an infinite loop which check for a qt event first and then message event.

    Is it possible to call the MyWidget::event(QEvent*)?

    I will show how the loop should look like...
    Qt Code:
    1. while(1)
    2. {
    3. if(true == event(QEvent *evt) ) /* keyEvent detected */
    4. {
    5. MyEventHandler(event);
    6. }
    7. if( /* Messgae at Queue */ )
    8. {
    9. /* Handle the Message */
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: is it possible to stay on a user defined infinite loop?

    And how do you expect to receive events if you are inside an infinite loop?
    How about just processing events in a "normal way"? By subclassing or using an even filter, that is..

  9. #9
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: is it possible to stay on a user defined infinite loop?

    Quote Originally Posted by jpn
    And how do you expect to receive events if you are inside an infinite loop?
    How about just processing events in a "normal way"? By subclassing or using an even filter, that is..

    i didnt get that...
    can u explain it...

    mahe2310

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: is it possible to stay on a user defined infinite loop?

    But why do you want to start your endless loop in the contructor and not just call it normally?
    But even so it would not work.
    What jpn means is, if the main event loop is stuck in your endless loop, it will never have time to attend the events.
    You should use an event filter.
    http://doc.trolltech.com/4.1/qobject...allEventFilter

    The above link is to the normal Qt4, but look the same in QTopia docs, I think it should be the same or very similar.

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.