Results 1 to 7 of 7

Thread: QThread in Qt4.4

  1. #1
    Join Date
    Apr 2008
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QThread in Qt4.4

    Hi!

    Due to the well known bug with TableView in combination with sorting I had to switch from
    Qt 4.3 to Qt 4.4. Everything went pretty well until it tried my threading-classes. Here I have a strange issue which I haven't had with Qt 4.3. I wrote a small example:

    mythread.h:
    Qt Code:
    1. #ifndef MYTHREAD_H
    2. #define MYTHREAD_H
    3.  
    4. #include <QThread>
    5. #include <QTimer>
    6. #include <QDebug>
    7.  
    8. class MyThread : public QThread
    9. {
    10. Q_OBJECT
    11. public:
    12. MyThread(QObject *parent = 0) : QThread(parent){}
    13. ~MyThread(){ delete timer;}
    14.  
    15. void run(void)
    16. {
    17. qDebug() << "head of run";
    18. timer = new QTimer;
    19. timer->setSingleShot(true);
    20. timer->setInterval(1000);
    21.  
    22. connect( timer, SIGNAL(timeout()), this, SLOT(restartTimer()));
    23.  
    24. timer->start();
    25.  
    26. qDebug() << "about to start event loop";
    27. exec();
    28.  
    29. }
    30.  
    31. private slots:
    32. void restartTimer(void)
    33. {
    34. timer->start();
    35. qDebug() << "Timer restarted";
    36. }
    37.  
    38. private:
    39. QTimer *timer;
    40. };
    41.  
    42. #endif //MYTHREAD_H
    To copy to clipboard, switch view to plain text mode 

    main.cpp:
    Qt Code:
    1. #include <QtGui>
    2. #include "mythread.h"
    3.  
    4.  
    5. int main(int argc, char* argv[])
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. MyThread thread;
    10. thread.start();
    11.  
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    When I compile it with Qt 4.4.0 I get the following output:
    head of run
    about to start event loop
    QObject::startTimer: timers cannot be started from another thread
    Timer restarted

    And then nothing. Which is logical because it can't access the timer. But why?
    Maybe someone could try this code on his machine. It would be really great!
    Or maybe someone could tell me why this doesn't work...

    Have a nice evening

    Col

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

    Default Re: QThread in Qt4.4

    The restartTimer() slot gets executed in "wrong" thread context. You may force the connection as direct to get the slot executed in "correct" thread context:
    Qt Code:
    1. connect( timer, SIGNAL(timeout()), this, SLOT(restartTimer()), Qt::DirectConnection);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. The following 2 users say thank you to jpn for this useful post:

    ColonelMoW (2nd June 2008), MaximA (31st May 2008)

  4. #3
    Join Date
    May 2008
    Posts
    15
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread in Qt4.4

    I'm new to threads and I try to wrap my head around it for quite some time (with only little success ):

    Am I right with saying: the wrong context results, because the "thread.start()" is called before "return app.exec()" ?

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

    Default Re: QThread in Qt4.4

    Quote Originally Posted by MaximA View Post
    I'm new to threads and I try to wrap my head around it for quite some time (with only little success ):
    The multi-threading presentation by Brad Hughes, available at http://labs.trolltech.com/page/Proje...ys/DevDays2007 and the introduction to QThreads available at http://ics.com/icsnetwork/ are worth reading and watching.

    Am I right with saying: the wrong context results, because the "thread.start()" is called before "return app.exec()" ?
    Well not quite. QCoreApplication::exec() starts and event loop (in the main thread). QThread::start() just begins the execution of another thread. A QThread may optionally enter to its own event loop by calling QThread::exec().
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    MaximA (31st May 2008)

  7. #5
    Join Date
    May 2008
    Posts
    15
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread in Qt4.4

    Thank you very much for the links, I'm looking forward to watch/read into it

    ... A QThread may optionally enter to its own event loop by calling QThread::exec().
    Which the OP does in his thread (line 27).
    Is the connect called to early, doesn't it therefore not belong to the eventloop of the thread....(I thought, with my -in the meantime- totally toasted brain, that the threat is "autonomous" because everything is inside- even its own event-loop.... aarrrggg :lol:
    Please don't be bothered by my stupid understanding, and it's okay if you don't want to answer (you have probably answered this kind of question a million times)

    In any case thanks for listening!
    And have a nice & stupid-free weekend :lol:

    EDIT: I've just read http://www.qtcentre.org/forum/f-newb...ing-13939.html and get a feeling of an understanding (thanks for the effort!)
    Last edited by MaximA; 31st May 2008 at 00:30.

  8. #6
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QThread in Qt4.4

    Why do you connect timeout() to restartTimer()? What if you write use setSingleShot(false)? This will restart timer automatically after it fires.
    I'm a rebel in the S.D.G.

  9. #7
    Join Date
    Apr 2008
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Talking Re: QThread in Qt4.4

    @lyuts
    You're right. That was just for demonstration purposes.

    I did a lot of research and I found my error:
    All Slots, attributes, routines, ... of the QThread-class belong
    to the Thread that instantiated the QThread-object.

    A wrote a QObject-class that is now instantiated on the stack
    of the run()-routine. Now I can access all slots of my QObject via QueuedConnections and they run in the right thread.

    Thank you everyone for your Help.

    btw. Of course in this particular problem I can use a DirectConnection. That way
    the slot is executed in the calling threat. Thank you jpn - that actually brought me on the right track.

    Col

Similar Threads

  1. QThread and QTcpSocket
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 12th May 2008, 14:06
  2. Spawn a QThread in another QThread
    By david.corinex in forum Qt Programming
    Replies: 2
    Last Post: 19th December 2007, 13:54
  3. QThread and QSlot
    By Xaar in forum Qt Programming
    Replies: 5
    Last Post: 6th December 2007, 22:37
  4. QThread exec proplem to stop...
    By patrik08 in forum Qt Programming
    Replies: 29
    Last Post: 21st May 2007, 08:51
  5. Is it possible to create a QThread without inheriting ?
    By probine in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2006, 23:51

Tags for this Thread

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.