Results 1 to 4 of 4

Thread: Executing slots in a separate QThread?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2009
    Posts
    33
    Thanks
    2

    Default Executing slots in a separate QThread?

    Hey all,

    I have a quick question regarding the start up of a new thread, separate to the main thread, and executing a slot in it, from a signal invoked from the main GUI thread. I understand this behaviour is not normally recommended.

    I created a simple subclass of QThread and connected a signal from my main thread to a slot in the subclassed thread. The slot prints out the current thread id. Initializing my class object in the main thread also prints out the current thread id. However, both these threads have the same id... aren't they supposed to be different? I'm using QThread::currentThread() to return the ID, and I read there might be issues using this with win32, but I'm on Linux. I've provided my source at the bottom of the post.

    ## output ##
    Qt Code:
    1. Main thread is 0x90fd250
    2. Executing Slot in 0x90fd250
    3. Executing Slot in 0x90fd250
    4. Executing Slot in 0x90fd250
    To copy to clipboard, switch view to plain text mode 

    If I move the thread object to the actual thread, (using moveToThread), the results are as expected:

    Qt Code:
    1. Main thread is 0x8ac5250
    2. Executing Slot in 0x8ace860
    3. Executing Slot in 0x8ace860
    4. Executing Slot in 0x8ace860
    To copy to clipboard, switch view to plain text mode 

    I just wanted to know if using moveToThread is the right way to do this (what if the thread quits or is terminated?)


    ## main.cpp ##
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include "test.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication a(argc, argv);
    7.  
    8. MyObject thisIsATest;
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 


    ## test.h ##
    Qt Code:
    1. #include <new>
    2. #include <iostream>
    3. #include <iomanip>
    4. #include <stdlib.h>
    5. #include <vector>
    6. #include <time.h>
    7. #include <math.h>
    8. #include <fstream>
    9. #include <sstream>
    10. #include <stdexcept>
    11.  
    12. // Qt Includes
    13. #include <QThread>
    14. #include <QMutex>
    15. #include <QString>
    16. #include <QWaitCondition>
    17.  
    18. class MyObject;
    19. class MyThread;
    20.  
    21. class MyThread : public QThread
    22. {
    23. Q_OBJECT
    24.  
    25. public:
    26. MyThread(MyObject* ptr, QObject *parent = 0) { MyObjectPtr = ptr; }
    27. ~MyThread() {}
    28.  
    29. public slots:
    30. void MyThreadsStartup();
    31. void MyThreadsSlot();
    32.  
    33. protected:
    34. void run();
    35.  
    36. private:
    37. MyObject* MyObjectPtr;
    38.  
    39. };
    40.  
    41. class MyObject : public QObject
    42. {
    43. Q_OBJECT
    44.  
    45. public:
    46. MyObject(QObject *parent = 0);
    47. ~MyObject() {}
    48.  
    49. signals:
    50. void TriggerThread();
    51.  
    52. private:
    53. MyThread* myThread;
    54.  
    55. };
    To copy to clipboard, switch view to plain text mode 

    ## test.cpp ##
    Qt Code:
    1. #include "test.h"
    2.  
    3.  
    4. void MyThread::MyThreadsStartup()
    5. { QObject::connect(MyObjectPtr, SIGNAL(TriggerThread()), this, SLOT(MyThreadsSlot()));
    6. //moveToThread(this)
    7. }
    8.  
    9. void MyThread::MyThreadsSlot()
    10. { std::cerr << " Executing Slot in " << QThread::currentThread() << std::endl; }
    11.  
    12. void MyThread::run()
    13. { exec(); }
    14.  
    15.  
    16. MyObject::MyObject(QObject *parent) : QObject(parent)
    17. {
    18. std::cerr << " Main thread is " << QThread::currentThread() << std::endl;
    19. myThread = new MyThread(this);
    20. myThread->MyThreadsStartup();
    21. myThread->start();
    22.  
    23.  
    24. emit TriggerThread();
    25. emit TriggerThread();
    26. emit TriggerThread();
    27. }
    To copy to clipboard, switch view to plain text mode 

    Regards,

    -KF
    Last edited by kachofool; 9th December 2009 at 01:21.

Similar Threads

  1. Replies: 8
    Last Post: 27th March 2013, 11:51
  2. Why slots in QThread subclasses are unsafe?
    By AlphaWolf in forum Qt Programming
    Replies: 8
    Last Post: 30th May 2010, 15:39
  3. QTableWidget, QThread, signals and slots
    By kazek3018 in forum Newbie
    Replies: 4
    Last Post: 30th December 2008, 21:21
  4. QThread - multi threaded signals and slots
    By rishid in forum Qt Programming
    Replies: 4
    Last Post: 30th March 2008, 01:47

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
  •  
Qt is a trademark of The Qt Company.