Results 1 to 3 of 3

Thread: QThread not running in different thread

  1. #1
    Join Date
    Feb 2011
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default QThread not running in different thread

    I am trying to create a thread with an object inside it that does some work periodically (using a QTimer). My idea was to subclass QThread and have all communication as well as the timed event run through the event-loop. But with my current example, everything seems to still run in the same thread.

    This is the thread-class.
    Qt Code:
    1. class thread : public QThread
    2. {
    3. Q_OBJECT
    4.  
    5. private:
    6. QTimer *_timer;
    7.  
    8. public:
    9. void run(void);
    10.  
    11. public slots:
    12. void startTimer(int timeout);
    13. void _timeout(void);
    14.  
    15. };
    16.  
    17. void
    18. thread::run(void) {
    19. /* start the event-loop */
    20. exec();
    21. }
    22.  
    23. void
    24. thread::startTimer(int timeout) {
    25. if(!_timer)
    26. _timer = new QTimer();
    27.  
    28. QObject::connect(_timer, SIGNAL(timeout()), this, SLOT(_timeout()));
    29.  
    30. _timer->setInterval(timeout);
    31. _timer->start();
    32. }
    33.  
    34. void
    35. threadn::_timeout(void) {
    36. qDebug() << "timeout from" << QThread::currentThreadId();
    37. }
    To copy to clipboard, switch view to plain text mode 

    This is what I run from my 'main' thread:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. thread *t = new thread();
    8.  
    9. QObject::connect(this, SIGNAL(startTimer(int)), t, SLOT(startTimer(mode,int)), Qt::QueuedConnection);
    10. t->start(QThread::HighestPriority);
    11.  
    12. qDebug() << "Hello from " << QThread::currentThreadId();
    13.  
    14. emit startTimer(1000);
    15. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by lauwe; 27th February 2011 at 23:03.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread not running in different thread

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2011
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread not running in different thread

    Thanks for the link!

    After reading some more I found that threading in Qt does not work as I thought. The following does seem to work:
    Qt Code:
    1. /* notice that thread no longer inherits from QThread */
    2. class thread : public QObject
    3. {
    4. Q_OBJECT
    5.  
    6. private:
    7. QTimer *_timer;
    8.  
    9. public slots:
    10. void startTimer(int timeout);
    11. void _timeout(void);
    12.  
    13. };
    14.  
    15. void
    16. thread::startTimer(int timeout) {
    17. if(!_timer)
    18. _timer = new QTimer();
    19.  
    20. QObject::connect(_timer, SIGNAL(timeout()), this, SLOT(_timeout()));
    21.  
    22. _timer->setInterval(timeout);
    23. _timer->start();
    24. }
    25.  
    26. void
    27. threadn::_timeout(void) {
    28. qDebug() << "timeout from" << QThread::currentThreadId();
    29. }
    To copy to clipboard, switch view to plain text mode 
    Now create a QThread object and move the instance of 'thread' to it.
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. thread *t = new thread();
    8. QThread *qthread = new QThread();
    9.  
    10. QObject::connect(this, SIGNAL(startTimer(int)), t, SLOT(startTimer(mode,int)), Qt::QueuedConnection);
    11. t->moveToThread(qthread);
    12.  
    13. qthread->start(QThread::HighestPriority);
    14.  
    15. qDebug() << "Hello from " << QThread::currentThreadId();
    16.  
    17. emit startTimer(1000);
    18. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 5
    Last Post: 22nd February 2011, 21:21
  2. how to terminate a thread when it is running
    By guchangyuan in forum Qt Programming
    Replies: 1
    Last Post: 19th October 2009, 10:50
  3. Replies: 4
    Last Post: 26th June 2008, 18:41
  4. running() - Thread
    By sabeesh in forum Qt Programming
    Replies: 5
    Last Post: 9th October 2007, 18:45
  5. QThread: Destroyed while thread is still running
    By Shuchi Agrawal in forum Newbie
    Replies: 8
    Last Post: 3rd April 2007, 06:27

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.