Results 1 to 18 of 18

Thread: QThread not threading??

  1. #1
    Join Date
    Apr 2010
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QThread not threading??

    I need a program that runs a thread with an event loop independently. Therefore I subclassed a QThread.
    Thus my code looks like this;
    #include "tthread.h"

    TThread::TThread(QObject *parent) :
    QThread(parent)
    {
    }


    void TThread::run(){
    QTimer *time = new QTimer();
    time->singleShot(1000,this,SLOT(timeOUT()));
    exec();
    }

    void TThread::timeOUT(){
    while(1);
    }

    And my mainwindow:
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    TThread *thread =new TThread(0);
    thread->start();
    }

    So here is what I meant to happen: My GUI mainwindow will not be affectet by the fact, that the TThread will stuck in the while(1)-loop.
    But it does so my questions:
    1. Why is that... what do I miss?
    2. What do I have to do to really have an independent thread with an event loop?

    Greetings
    Roman

  2. #2
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread not threading??

    First of all why did you use a timer inside a thread???? Your code seems messy

  3. #3
    Join Date
    Apr 2010
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread not threading??

    Well its obviously not my final programm. I need to check the time between a sent and received UDP-Message. Further if no Connection could have been established, a reconnect in a certain time should be made without action from the main application. Therefore Timers would be very usefull. Further as I posted a peeled code it doesnt look really nice I guess thats what you ment with
    Your code seems messy
    or is there something else you dont like about my code?

  4. #4
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread not threading??

    Instead of using extra timer and singleShot just use a sleep in thread

  5. #5
    Join Date
    Apr 2010
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread not threading??

    Well your solutions would really work... I tried it allready.... unfortunately the udpsocket cant be clodes / deleted in a thread without an eventloop further i want to implement the qtserialport (using qt5) in the same manner where there is no chance of getting it working in a thread without an eventloop...

    But even apart from my application and what I want to do.... shouldn't the QThread behave like two absolutely independent programms... means freezing of one thread should not affect the other thread by any circumstances?

  6. #6
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread not threading??

    Quote Originally Posted by Roman View Post
    I need a program that runs a thread with an event loop independently. Therefore I subclassed a QThread.
    Thus my code looks like this;
    Qt Code:
    1. #include "tthread.h"
    2.  
    3. TThread::TThread(QObject *parent) :
    4. QThread(parent)
    5. {
    6. }
    7.  
    8.  
    9. void TThread::run(){
    10. QTimer *time = new QTimer();
    11. time->singleShot(1000,this,SLOT(timeOUT()));
    12. exec();
    13. }
    14.  
    15. void TThread::timeOUT(){
    16. while(1);
    17. }
    To copy to clipboard, switch view to plain text mode 

    And my mainwindow:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. TThread *thread =new TThread(0);
    8. thread->start();
    9. }
    To copy to clipboard, switch view to plain text mode 

    So here is what I meant to happen: My GUI mainwindow will not be affectet by the fact, that the TThread will stuck in the while(1)-loop.
    But it does so my questions:
    1. Why is that... what do I miss?
    2. What do I have to do to really have an independent thread with an event loop?

    Greetings
    Roman
    Qt Code:
    1. void TThread::run(){
    2. exec();
    3. }
    To copy to clipboard, switch view to plain text mode 

    fixed.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  7. #7
    Join Date
    Apr 2010
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread not threading??

    Quote Originally Posted by amleto View Post
    Qt Code:
    1. void TThread::run(){
    2. exec();
    3. }
    To copy to clipboard, switch view to plain text mode 

    fixed.
    What should I do whit that?
    I really do the exec();
    Qt Code:
    1. void TThread::run(){
    2. QTimer *time = new QTimer();
    3. time->singleShot(1000,this,SLOT(timeOUT()));
    4. exec(); /*<-----------------------------------------
    5. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QThread not threading??

    The reason for such behaviour is that Your QTimer lives not in new thread but in main thread.
    QTimer excepts that thread even loop is running but ... You can't create QTimer before exec() because thread event loop is not running and You can't call it after exec() because code won't be executed past that point until thread is running (after QThread hit's exit() or quit() then code in run() past exec() will be executed!).

    QThread is just a object that manage new thread and is not thread itself. it lives in the thread that it was created in, hance Your QTimer lives in main thread!
    The run method by default (not reimplemented!) call exec().

    Try this code (in Your code replace QThread subclass with QObject subclass and use QThread as shown bellow):

    Qt Code:
    1. wThread::wThread(QObject *parent) :
    2. QObject(parent)
    3. {
    4. }
    5.  
    6. void wThread::doSomeWork()
    7. {
    8. qDebug() << Q_FUNC_INFO;
    9.  
    10. QTimer *t = new QTimer();
    11. t->singleShot(1000, this, SLOT(timeOut()));
    12.  
    13. }
    14.  
    15. void wThread::timeOut()
    16. {
    17. qDebug() << Q_FUNC_INFO;
    18.  
    19. bool doLoop = true;
    20. int c = 0;
    21.  
    22. while (1) {
    23. c++;
    24. if (c > 10)
    25. doLoop = false;
    26.  
    27. qDebug() << "in thread!";
    28. }
    29. }
    30.  
    31. MainWindow::MainWindow(QWidget *parent) :
    32. QMainWindow(parent),
    33. ui(new Ui::MainWindow)
    34. {
    35. QThread *th = new QThread(this);
    36.  
    37. wThread *workerThread = new wThread(0);
    38. workerThread->moveToThread(th);
    39. th->start();
    40. workerThread->doSomeWork(); // <-- work done in new thread!
    41. }
    To copy to clipboard, switch view to plain text mode 

    Good luck.
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  9. #9
    Join Date
    Apr 2010
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread not threading??

    Quote Originally Posted by Talei View Post
    QThread is just a object that manage new thread and is not thread itself.
    Now I got it!! Thanks a lot!!

    Got it all to work now. But got one more question:
    Is the timer t which is initiated in the doSomeWork()-Function which is called by the mainWindow (workerThread->doSomeWork() dependent from the MainWindow-eventloop? If so how would a structure look alike that initiate a timer right from the beginning thats in the eventloop of the new thread?

  10. #10
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread not threading??

    Quote Originally Posted by Talei View Post
    The reason for such behaviour is that Your QTimer lives not in new thread but in main thread.
    QTimer excepts that thread even loop is running but ... You can't create QTimer before exec() because thread event loop is not running and You can't call it after exec() because code won't be executed past that point until thread is running (after QThread hit's exit() or quit() then code in run() past exec() will be executed!).

    QThread is just a object that manage new thread and is not thread itself. it lives in the thread that it was created in, hance Your QTimer lives in main thread!

    This is not correct. run() is executed in the new thread, therefore the timer is also instantiated in the new thread.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  11. #11
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QThread not threading??

    It's been a while since I touched QThread, so sorry for my mistake. My other reply is wrong at least partially.
    run() and exec() are indeed already executed in new thread!. Sad thing is that I forget about this . I really need to stop posting after 12h+ code "marathon"...

    IMHO the reason why QTimer is created within main thread is that in QTimer, actually QObject, parent thread is a thread that parent object lives in.
    So QTimer's parent is TThread and parent thread for this object is main thread and not TThread itself.

    That's why when You create QObject inside a QThread that object lives in TThread parent thread and not TThread itself.
    And You can't do QTimer(this) inside QThread that way due to i.e.:

    Example:

    Qt Code:
    1. class wnThread : public QThread
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. wnThread(QObject *parent = 0) : QThread(parent) {
    7. qDebug() << "wnThread ctor:" << this->currentThread();
    8. }
    9.  
    10. void run() {
    11. qDebug() << "in thread th b4 exec:" << this->currentThread();
    12.  
    13. exec(); // <-- enter event loop and wait until quit() or exit()
    14.  
    15. // ... code placed here will be executed -after- thread s terminated!
    16. qDebug() << "after exec:" << this->currentThread();
    17. }
    18.  
    19. void exec() {
    20. qDebug() << Q_FUNC_INFO << this->currentThread();
    21.  
    22. QTimer *t = new QTimer(this);
    23. t->singleShot(100, this, SLOT(doSomeWork()));
    24.  
    25. }
    26.  
    27. public slots:
    28. void doSomeWork() {
    29. int c = 0;
    30.  
    31. while (c < 1000) {
    32. c++;
    33. }
    34.  
    35. qDebug() << Q_FUNC_INFO << this->currentThread();
    36. this->quit();
    37. }
    38.  
    39. };
    40.  
    41. MainWindow::MainWindow(QWidget *parent) :
    42. QMainWindow(parent),
    43. ui(new Ui::MainWindow)
    44. {
    45. wnThread *th = new wnThread(this);
    46. qDebug() << "main thread:" << this->thread() << "th thread:" << th->thread() << th->currentThread();
    47. th->start();
    48. qDebug() << "th after start:" << th->currentThread();
    49. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. wnThread ctor: QThread(0x3e57a0)
    2. main thread: QThread(0x3e57a0) th thread: QThread(0x3e57a0) QThread(0x3e57a0)
    3. th after start: QThread(0x3e57a0)
    4. in thread th b4 exec: wnThread(0x106271c8)
    5. void wnThread::exec() wnThread(0x106271c8)
    6. QObject: Cannot create children for a parent that is in a different thread.
    7. (Parent is wnThread(0x106271c8), parent's thread is QThread(0x3e57a0), current thread is wnThread(0x106271c8)
    8. after exec: wnThread(0x106271c8)
    To copy to clipboard, switch view to plain text mode 

    If I'm wrong please provide example of instantiated QTmer from within a QThread subclass.
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  12. #12
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread not threading??

    In all code previous to your post qtimer has been instantiated with no parent. So it has no knowledge at all of the 'parent' thread. It simply exists in the new thread.


    Added after 33 minutes:

    qtimer in a thread...
    Qt Code:
    1. class wnThread : public QThread
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. wnThread(QObject *parent = 0) : QThread(parent) {
    7. qDebug() << "wnThread ctor:" << this->currentThread();
    8. }
    9.  
    10. void run() {
    11. qDebug() << "in thread th b4 exec:" << this->currentThread();
    12.  
    13. exec(); // <-- enter event loop and wait until quit() or exit()
    14.  
    15. // ... code placed here will be executed -after- thread s terminated!
    16. qDebug() << "after exec:" << this->currentThread();
    17.  
    18. //**************************//
    19. // INCORRECT! The thread is still alive here!! How can it not be? run() is executed inside the thread...
    20. }
    21.  
    22. void exec() {
    23. qDebug() << Q_FUNC_INFO << this->currentThread();
    24.  
    25. QTimer *t = new QTimer();
    26.  
    27. qDebug() << "timer is in " << t->thread();
    28.  
    29. t->singleShot(100, this, SLOT(doSomeWork()));
    30.  
    31. }
    32.  
    33. public slots:
    34. void doSomeWork() {
    35. int c = 0;
    36.  
    37. while (c < 1000) {
    38. c++;
    39. }
    40.  
    41. qDebug() << Q_FUNC_INFO << this->currentThread();
    42. this->quit();
    43. }
    44.  
    45. };
    46.  
    47. MainWindow::MainWindow(QWidget *parent) :
    48. QMainWindow(parent),
    49. ui(new Ui::MainWindow)
    50. {
    51. wnThread *th = new wnThread();
    52. qDebug() << "main thread:" << this->thread() << "th thread:" << th->thread() << th->currentThread();
    53. th->start();
    54. qDebug() << "th after start:" << th->currentThread();
    55. }
    To copy to clipboard, switch view to plain text mode 

    don't do

    QThread mynewthread = QThread(this);
    It is a well known 'boo boo'.
    Last edited by amleto; 10th November 2012 at 10:52.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  13. #13
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QThread not threading??

    Did You test that code?
    I'm using Win with qt 4.8.1 and that QTimer don't work for me.

    Slot doSomeWork() is never executed.

    And why I shouldn't give parent to the QThread or to the QTimer?
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  14. #14
    Join Date
    Nov 2012
    Posts
    12
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread not threading??

    Note: Subclassing QThread is no more recommended by Qt, see http://qt-project.org/doc/note_revisions/5/8/view

  15. #15
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread not threading??

    Quote Originally Posted by Talei View Post
    Did You test that code?
    I'm using Win with qt 4.8.1 and that QTimer don't work for me.
    of course it doesn't - you didn't put an event loop in there anywhere! Additionally, the slot will only be invoked from the main thread, not the thread that run() is in!


    Added after 14 minutes:


    Quote Originally Posted by Talei View Post

    And why I shouldn't give parent to the QThread
    sorry, that is fine. I was thinking of doing moveToThread(this) in a thread ctor which causes more hassle.

    or to the QTimer?
    because that makes the qtimer associated to the main thread - but you want it in the new thread.


    Added after 12 minutes:


    maybe this will make things clearer.

    // winthread.h
    Qt Code:
    1. #ifndef WNTHREAD_H
    2. #define WNTHREAD_H
    3.  
    4. #include <QThread>
    5. #include <QTimer>
    6. #include <QDebug>
    7.  
    8. class Shout : public QObject
    9. {
    10. Q_OBJECT
    11. public slots:
    12. void shout()
    13. {
    14. static int i = 0;
    15. qDebug() << "shout " << i++ << " " << thread();
    16. }
    17. };
    18.  
    19. class wnThread : public QThread
    20. {
    21. Q_OBJECT
    22. public:
    23. explicit wnThread(QObject *parent = 0) : QThread(parent) {
    24. qDebug() << "wnThread ctor:" << this->currentThread();
    25. }
    26.  
    27. ~wnThread();
    28.  
    29. void run() {
    30. qDebug() << "in thread th b4 exec:" << this->currentThread();
    31.  
    32. QTimer t;
    33.  
    34. Shout s;
    35.  
    36. connect(&t, SIGNAL(timeout()), this, SLOT(doSomeWork())); // 'this' belongs to main thread!
    37. connect(&t, SIGNAL(timeout()), &s, SLOT(shout())); // s belongs to this new thread
    38.  
    39. qDebug() << "timer is in " << t.thread();
    40. t.start(3000);
    41. exec(); // <-- enter event loop and wait until quit() or exit()
    42.  
    43. qDebug() << "after exec:" << this->currentThread();
    44. }
    45.  
    46. public slots:
    47. void doSomeWork() {
    48. static int x = 0;
    49. ++x;
    50.  
    51. int c = 0;
    52. qDebug() << "doSomeWork " << thread() << currentThread();
    53. while (c < 1000) {
    54. c++;
    55. }
    56.  
    57. if (x > 5)
    58. this->quit();
    59. }
    60.  
    61. };
    62.  
    63. #endif // WNTHREAD_H
    To copy to clipboard, switch view to plain text mode 

    main
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "wnthread.h"
    3.  
    4. #include <QWidget>
    5. #include <QDebug>
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication a(argc, argv);
    10. w.show();
    11.  
    12. wnThread *th = new wnThread();
    13.  
    14. th->start();
    15.  
    16. return a.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by amleto; 10th November 2012 at 14:20.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  16. #16
    Join Date
    Apr 2010
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread not threading??

    @amleto: also Thanks for your help.
    I tested your code. It was a really eyeopener to me. Thanks.
    Now at first to see if I got the whole thing right i'd like to sum up:
    1. QThread is not a thread itself, but handles one.
    2. The QThread-Object (an thus also its functions) belongs to its creator/parent.
    3. Objects in the QThread-Object belonging to the thread.

    Please correct me if im wrong.

    Secound: This would imply that if, for example a qudpsocket would be handled in the thread a second object is needed that really handles the socket like the code underneath? Is that right? isnt there an easier way of doing so?
    Qt Code:
    1. #ifndef WINTHREAD_H
    2. #define WINTHREAD_H
    3.  
    4. #include <qthread.h>
    5. #include <qdebug.h>
    6. #include <qtimer.h>
    7. #include <qudpsocket.h>
    8.  
    9. class socketHdl : public QObject
    10. {
    11. Q_OBJECT
    12. public:
    13. socketHdl(QObject *parent = 0):QObject(parent){
    14.  
    15. }
    16.  
    17. public slots:
    18. void readData(){
    19.  
    20. }
    21. };
    22.  
    23. class wnThread : public QThread
    24. {
    25. Q_OBJECT
    26. public:
    27. explicit wnThread(QObject *parent = 0) : QThread(parent) {
    28. qDebug() << "wnThread ctor:" << this->currentThread();
    29. }
    30.  
    31. void run() {
    32. qDebug() << "in thread th b4 exec:" << this->currentThread();
    33.  
    34. QTimer t;
    35.  
    36. socketHdl s;
    37.  
    38. connect(&t, SIGNAL(timeout()), this, SLOT(doSomeWork()));
    39. connect(&us, SIGNAL(readyRead()), &s, SLOT(readData()));
    40.  
    41. qDebug() << "timer is in " << t.thread();
    42. t.start(3000);
    43. exec(); // <-- enter event loop and wait until quit() or exit()
    44.  
    45. qDebug() << "after exec:" << this->currentThread();
    46. }
    47.  
    48. public slots:
    49.  
    50. void doSomeWork() {
    51. static int x = 0;
    52. ++x;
    53.  
    54. int c = 0;
    55. qDebug() << "doSomeWork " << thread() << currentThread();
    56. while (c < 1000) {
    57. c++;
    58. }
    59.  
    60. if (x > 5)
    61. this->quit();
    62. }
    63.  
    64. };
    65.  
    66. #endif // WINTHREAD_H
    To copy to clipboard, switch view to plain text mode 

    thanks

  17. #17
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread not threading??

    1 - yes
    2 - erm...
    3 - If QThread object is in the main thread, then all QObject members of that instance will have to be in that thread.


    your example shows you still don't quite have something right. Why have QUdpSocket us and socketHdl s ? They are both in the 'new' thread.

    Perhaps this will clear things up:

    Qt Code:
    1. class wnThread : public QThread
    2. {
    3.  
    4. void some_func()
    5. {
    6. QObject obj2_;
    7. }
    8.  
    9. QObject obj1_;
    10. QObject* ptr_;
    11. };
    To copy to clipboard, switch view to plain text mode 

    QObjects MUST be instantiated in the thread that they 'belong' to. In other words, you CANNOT pass a parent ptr into ctor that lives in another thread.
    Therefore obj1_ will HAVE to live in the same thread as wnThread instance. ptr_ is not a QObject so can point to any QObject. obj2_ is only created in some_func(), so obj2_ will be in whatever thread is running some_func. This is why in your code above, t, us, & s are all in the same thread. Therefore there is no point having both s & us.

    As ljuhrich pointed out, there is a method that removes all this 'nonsense' and confusion: Don't sub class QThread.

    Qt Code:
    1. class SomeJob : public QObject
    2. {
    3. public:
    4. SomeJob(QObject* parent = 0) :
    5. obj_(parent) // This is important!! Make sure my member variables switch thread when I do
    6. // e.g. when SomeJob.moveToThread(thread), make sure obj_ goes to the thread with me.
    7. {
    8. }
    9.  
    10. // some long work
    11. void work();
    12.  
    13. // signals and slots...
    14. // ...
    15.  
    16. // members
    17. QObject obj_;
    18. };
    19.  
    20. SomeWidget::SomeQuickMethod()
    21. {
    22. // oh no, I need to do something heavy - thread it
    23. QThread* th = new QThread;
    24.  
    25. SomeJob* job = new SomeJob;
    26. job->moveToThread(th);
    27.  
    28. // Now there is no confusion - all slots of job will be handled in the sub thread
    29. // Even all slots of obj_ will be in the sub thread!
    30.  
    31. th->start();
    32. }
    To copy to clipboard, switch view to plain text mode 
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  18. #18
    Join Date
    Apr 2010
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread not threading??

    Quote Originally Posted by amleto View Post
    Why have QUdpSocket us and socketHdl s ? They are both in the 'new' thread.
    Well thats true. What I wanted to show is, that I need a secound Object (in my case the socketHdl) wich actualy reads and processes the data from the udpsocket object. As since in your code you stated, that a connect like:
    Qt Code:
    1. connect(&t, SIGNAL(timeout()), this, SLOT(doSomeWork())); // 'this' belongs to main thread!
    To copy to clipboard, switch view to plain text mode 
    would result in an execution of the doSomeWork()-function in the main thread.
    Quote Originally Posted by amleto View Post
    As ljuhrich pointed out, there is a method that removes all this 'nonsense' and confusion: Don't sub class QThread.
    Thats true... so far I didnt subclass Qthread.... but in the current solution provided by ljuhrich I need to deal with two object (the th and my SomeJob). This isnt very cute since I'll have several udp-connections in the future.

Similar Threads

  1. Replies: 1
    Last Post: 4th October 2012, 14:49
  2. Threading with rsh and rcp
    By jaxrpc in forum Newbie
    Replies: 2
    Last Post: 4th June 2010, 11:50
  3. Threading...?
    By sekatsim in forum Qt Programming
    Replies: 12
    Last Post: 10th June 2008, 01:14
  4. Qt Threading
    By avh in forum Newbie
    Replies: 7
    Last Post: 30th May 2008, 20:20
  5. Sub-Threading
    By TheGrimace in forum Qt Programming
    Replies: 4
    Last Post: 7th June 2007, 16:38

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.