Results 1 to 9 of 9

Thread: QThread

  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default QThread

    Hello everybody,
    I'm using threads ... and I've got a question. I've subclassed QThread and when I call start() the function below is called. Great. Now I thought there's be two ways of doing things: the first way with a forever loop - it works fine; and the second way, with a timer. My question: How do I connect the timer properly?

    update() is a func in 'this' i.e. MyClass where I do the work.

    Qt Code:
    1. void MyClass::run()
    2. {
    3. //can do this ....
    4.  
    5. forever{
    6. update();
    7. sleep(3);//seconds
    8. }
    9. return;
    10.  
    11. //or could do this...
    12.  
    13. QTimer* timer = new QTimer();
    14. timer->setInterval(3000);
    15. connect(timer, SIGNAL(timeout()), this, SLOT(update()), Qt::QueuedConnection);
    16.  
    17. exec();
    18.  
    19. delete timer;
    20.  
    21. return;
    22. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: QThread

    In this case you should use a direct connection, otherwise timeout events will be queued in... the main event loop, since MyClass instance belongs to the GUI thread.

  3. #3
    Join Date
    Jan 2006
    Posts
    75
    Thanks
    3
    Thanked 5 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QThread

    Jacek, do you mean when he want to use the QTimer and exec() method, he need to connect using Qt:irectConnection in order to let the timer event to be triggered and run in a the same thread as the thread using in run()?

  4. #4
    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: QThread

    Quote Originally Posted by ball
    do you mean when he want to use the QTimer and exec() method, he need to connect using Qt:irectConnection in order to let the timer event to be triggered and run in a the same thread as the thread using in run()?
    Yes, that's what I meant.

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QThread

    Sorry guys,
    I don't got it. (using DirectConnection)
    The stange thing is that the first variant works fine.
    I thought that getting a timer to trigger the work instead of a wait would be a little more elegant. But the timer way doesn't work. I don't get any messages that the connect() doesn't work.

    note also (although perhaps beside the point): I have emits in the update() func and they work fine into the gui thread.

    any ideas?

    thanks
    K

  6. #6
    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: QThread

    Quote Originally Posted by TheKedge
    But the timer way doesn't work.
    How did you start it?

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QThread

    I call
    start()
    from the constructor of my class. Or more precisely, I have an init() func called a single shot timer in the constructor. Thus:
    QTimer::singleShot(0, this, SLOT(init()));

    start() is called from the init() func.

    As said, the thread is running -> the loop/sleep method works. But I have the feeling somethings not right with the connect() (although I don't get any error messages)
    K

  8. #8
    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: QThread

    Does it work with queued connection?

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

    TheKedge (25th August 2006)

  10. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QThread

    Khryst!
    ok ... I'm sorry ... how about using timer->start() ?
    It works with both Direct and Queued connections.
    thank you, thank you ...
    K

Similar Threads

  1. How can I get the thread ID out of QThread
    By Artschi in forum Qt Programming
    Replies: 9
    Last Post: 8th November 2017, 04:27
  2. how to use QHttp inside QThread in Qt3
    By alusuel in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2006, 12:19
  3. Posting custom events to a subclass of QThread
    By jpn in forum Qt Programming
    Replies: 3
    Last Post: 4th July 2006, 16:49
  4. Is it possible to create a QThread without inheriting ?
    By probine in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2006, 23:51
  5. QProcess in a QThread
    By chombium in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2006, 16:52

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.