Results 1 to 19 of 19

Thread: QObject sleep

  1. #1
    Join Date
    May 2007
    Posts
    90
    Thanks
    40
    Qt products
    Qt4
    Platforms
    Windows

    Default QObject sleep

    Is there a way to queue a msleep in the event loop of an object?

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QObject sleep

    What do you mean?
    Something like sending an event that tells the thread to sleep?

    BTW, QOBjects don't have event loops. Only threads do.

  3. #3
    Join Date
    May 2007
    Posts
    90
    Thanks
    40
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject sleep

    I have a QObject that is part of the main loop that is constantly doing a poll-like operation via the event-loop. I am wondering if there is a way to queue a 10 millisecond sleep for the main app so i am not using all my cpu.

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QObject sleep

    Still, I don't think I understand.

    So you are analyzing all the events in the event loop... If you could post a user event with some ID, then when you get this event you could put the thread to sleep.

    But you have to provide more details than that... For example, who is telling the thread to sleep? A worker thread?

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

    TheGrimace (19th October 2007)

  6. #5
    Join Date
    May 2007
    Posts
    90
    Thanks
    40
    Qt products
    Qt4
    Platforms
    Windows

    Wink Re: QObject sleep

    I have a main thread that receives signals from two worker threads. If a condition is not met when the main thread processes the signal, it resends the signal to itself via a queued connection. The problem is that I need to give the worker threads time to get things done before I process the event queue again. That way the condition might be met.

    Thank You

  7. #6
    Join Date
    May 2007
    Posts
    90
    Thanks
    40
    Qt products
    Qt4
    Platforms
    Windows

    Smile Re: QObject sleep

    After writing about it more it seems my idea is flawed. Why is my object an object and not a thread? I think I will change that now. Thank you for your help.

  8. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QObject sleep

    I think that when you get a signal and it is too soon to process it(condition is not met) then you should queue the action, in a stack, list, array, whatever. By action I mean a structure or a class that holds enough information about what needs to be done.

    Now, you could poll action list with a timer or even with another worker thread, and execute them as soon their conditions are met.

    Your solution with resending the events doesn't sound that good.

  9. The following user says thank you to marcel for this useful post:

    TheGrimace (19th October 2007)

  10. #8
    Join Date
    May 2007
    Posts
    90
    Thanks
    40
    Qt products
    Qt4
    Platforms
    Windows

    Smile Re: QObject sleep

    Thank You,

    I agree. I am now rethinking my entire approach.

  11. #9
    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: QObject sleep

    How about using a QTimer? There is probably no need to use threads...

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

    TheGrimace (19th October 2007)

  13. #10
    Join Date
    May 2007
    Posts
    90
    Thanks
    40
    Qt products
    Qt4
    Platforms
    Windows

    Smile Re: QObject sleep

    This is a side-question pertaining to the same code. If I have a widget which creates a thread, and if I then connect a signal from that thread to a slot in the same QThread object, to whom does the execution of that slot belong? The widget or the thread.

    Edit: And by widget I mean an object or widget in the Main Application and not a thread
    Last edited by TheGrimace; 19th October 2007 at 21:01.

  14. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QObject sleep

    Quote Originally Posted by TheGrimace View Post
    If I have a widget which creates a thread, and if I then connect a signal from that thread to a slot in the same QThread object, to whom does the execution of that slot belong? The widget or the thread.
    The answer depends on the connection type (I assume you have automatic connections) and Qt version.

    In Qt 4.2.3 automatic connections are handled this way:
    Qt Code:
    1. // determine if this connection should be sent immediately or
    2. // put into the event queue
    3. if ((c->type == Qt::AutoConnection
    4. && (currentThreadData != sender->d_func()->threadData
    5. || c->receiver->d_func()->threadData != sender->d_func()->threadData))
    6. || (c->type == Qt::QueuedConnection)) {
    7. ::queued_activate(sender, *c, argv, from_signal_index, to_signal_index);
    8. continue;
    9. }
    To copy to clipboard, switch view to plain text mode 
    Which means that in this case the signal will be queued (first != operator returns true).

    Edit: It appears that automatic connections always were handled this way.
    Last edited by jacek; 19th October 2007 at 21:39.

  15. #12
    Join Date
    May 2007
    Posts
    90
    Thanks
    40
    Qt products
    Qt4
    Platforms
    Windows

    Smile Re: QObject sleep

    In this case i have been explicitly making queued connections. What I am wondering is, do the signals go into the thread's event queue or the main applications? I am using 4.3.1.

    Thank You

  16. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QObject sleep

    Quote Originally Posted by TheGrimace View Post
    What I am wondering is, do the signals go into the thread's event queue or the main applications? I am using 4.3.1.
    Signals are queued in the event loop of the thread in which the receiver lives. Since you create the QThread instance in the main thread (a.k.a. the GUI thread), the signal will be stored in the main event loop, which means also that the slot will be executed by the main thread.

  17. The following user says thank you to jacek for this useful post:

    TheGrimace (19th October 2007)

  18. #14
    Join Date
    May 2007
    Posts
    90
    Thanks
    40
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject sleep

    So the only way to get a slot to execute in a thread is to make an object that is created in the thread? Darn!

    Thank You for the information. These forums and you folks are a great help.

  19. #15
    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: QObject sleep

    No, you can move an already existing object to another thread by using QObject::moveToThread. Just remember that you can only push the object from current to another thread, not pull it from another to current.

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

    TheGrimace (19th October 2007)

  21. #16
    Join Date
    May 2007
    Posts
    90
    Thanks
    40
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject sleep

    So it would be something like:

    thread->start();
    thread->moveToThread(thread);

    ?

  22. #17
    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: QObject sleep

    Yes, exactly like that. Just make sure the thread has no parent or it will not work. You can only move a parentless object.

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

    TheGrimace (19th October 2007)

  24. #18
    Join Date
    May 2007
    Posts
    90
    Thanks
    40
    Qt products
    Qt4
    Platforms
    Windows

    Smile Re: QObject sleep

    I just tried it and it worked like a charm No more gui freezing

    Thank You!

  25. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QObject sleep

    Another solution is to simply pass Qt::DirectConnection as connection type, but you have create all QObjects in run().

Similar Threads

  1. From QObject to QWidget
    By Maxilys in forum Qt Programming
    Replies: 3
    Last Post: 2nd February 2014, 17:07
  2. Reparenting a QObject
    By ghorwin in forum Qt Programming
    Replies: 1
    Last Post: 13th April 2007, 17:21
  3. Interface composition and QObject
    By brcain in forum Qt Programming
    Replies: 9
    Last Post: 20th November 2006, 17:56
  4. Problem with Destructor of QObject
    By mikro in forum Qt Programming
    Replies: 5
    Last Post: 9th July 2006, 19:05
  5. sleep - sleeps, but not when I want it to sleep
    By nongentesimus in forum Newbie
    Replies: 2
    Last Post: 2nd March 2006, 14:43

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.