Results 1 to 6 of 6

Thread: QTimer::singleShot(0, ... confusion

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QTimer::singleShot(0, ... confusion

    Hi,

    I'm a little bit confused about the usage of

    Qt Code:
    1. QTimer::singleShot(0, this, SLOT(doSomething()));
    To copy to clipboard, switch view to plain text mode 

    The document states that "a QTimer with a timeout of 0 will time out as soon as all the events in the window system's event queue have been processed". So does this mean that it is essentially the same as

    Qt Code:
    1. QMetaObject::invokeMethod(this, "doSomething", Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 

    or does it mean that in the case of an currently empty event queue the slot will be called directly?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: QTimer::singleShot(0, ... confusion

    The two calls are essientially the same. There is a tiny technical difference but it doesn't influence the final effect.

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTimer::singleShot(0, ... confusion

    Every system has a event loop where events are processed. Say like
    application:rocessEvents()
    {
    // process event list..
    }

    QTimer::singleShot(0, this, SLOT(doSomething()));
    Now the place where you write the above statement is inside some processing event.
    When this loop is done, processEvents will be called again and in that the doSomething() will be executed.

    So this is like calling doSomething in the next event loop, rather than calling it immediately.
    Hope you get the idea.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: QTimer::singleShot(0, ... confusion

    Quote Originally Posted by aamer4yu View Post
    So this is like calling doSomething in the next event loop, rather than calling it immediately.
    So is the other call... The technical difference is the order of calls. Timers are checked after all events are processed, so if you have such code:
    Qt Code:
    1. class Object : public QObject {
    2. Q_OBJECT
    3. public slots:
    4. void funcA() { qDebug() << Q_FUNC_INFO; }
    5. void funcB() { qDebug() << Q_FUNC_INFO; }
    6. };
    To copy to clipboard, switch view to plain text mode 

    and you do this:
    Qt Code:
    1. Object *o = new Object;
    2. QMetaObject::invokeMethod(o, "funcA", Qt::QueuedConnection);
    3. QMetaObject::invokeMethod(o, "funcB", Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 
    Then you should get:
    • Object::funcA()
    • Object::funcB()

    But if you do:
    Qt Code:
    1. Object *o = new Object;
    2. QTimer::singleShot(0, o, SLOT(funcA()));
    3. QMetaObject::invokeMethod(o, "funcB", Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 

    you will probably get:
    • Object::funcB()
    • Object::funcA()


    I haven't verified that but that's what I would be expecting to see.

  5. #5
    Join Date
    Apr 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTimer::singleShot(0, ... confusion

    Thanks a lot for the answers! Basically, that was my assumption as well, but I ran into a problem in my code using the mac cocoa port of qt that - at least as I see it - can only be caused if under some conditions QTimer::singleShot(0, ...) is called directly. Then I browsed a little bit through the qt sources and found out that there are some calls to QTimer::singleShot with a msecs value of 1, which to me doesn't make much sense because of the timer resolution on most platforms. And that there a more calls to QMetaObject::invokeMethod(..., Qt::QueuedConnection) than to QTimer::singleShot(0, ...)
    After that I checked the QTimer documentation and found it a little bit hooey on that matter, but maybe I just missed the keyword "queued" in it to make things clearer.

  6. #6
    Join Date
    Aug 2009
    Posts
    28
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: QTimer::singleShot(0, ... confusion

    I think it jusk like:
    CallInUiThreadLater(yourSlotFunction);

Similar Threads

  1. Replies: 3
    Last Post: 31st January 2010, 16:56
  2. Replies: 8
    Last Post: 10th December 2009, 10:06
  3. multiple QTimer::singleShot() calls?
    By mattc in forum Qt Programming
    Replies: 1
    Last Post: 27th July 2009, 19:22
  4. Can we connect QTimer::SingleShot with a slot taking arguments?
    By maverick_pol in forum Qt Programming
    Replies: 4
    Last Post: 17th September 2008, 18:02
  5. Replies: 1
    Last Post: 6th April 2006, 12:24

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.