Results 1 to 3 of 3

Thread: QThread not running in different thread

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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.