Results 1 to 19 of 19

Thread: QThread misunderstanding...I suppose!

  1. #1
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QThread misunderstanding...I suppose!

    I am not new in Posix threads but new in QThread. The expected behaviour of my code is both threads increasing a shared variable named val. Unfortunately it appears that each one is working on a different instance of val, ie if I clic twice on button 1 I get val=2 and then if I clic on button 2 I get val=0.
    What is wrong in my code?
    Thanks in advance for your help.

    Qt Code:
    1. class boutonThread : public QThread
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. boutonThread(QWidget *parent = 0, int number = 0);
    7.  
    8. public slots:
    9. void threadClicked();
    10.  
    11. private:
    12. int val;
    13. QVector<long> thread_ids;
    14. int GetId();
    15. };
    16.  
    17. class boutons : public QWidget
    18. {
    19. Q_OBJECT
    20.  
    21. public:
    22. boutons(QWidget *parent = 0);
    23.  
    24. private:
    25. QPushButton *buttons[2];
    26. buttonThread *threads[2];
    27. };
    28.  
    29. boutons::boutons(QWidget *parent)
    30. {
    31. QHBoxLayout *layout = new QHBoxLayout(this);
    32. buttons[0] = new QPushButton("Thread 1", this);
    33. layout->addWidget(buttons[0]);
    34. threads[0] = new boutonThread(this, 1);
    35. QObject::connect(this->buttons[0], SIGNAL(clicked()), threads[0], SLOT(threadClicked()));
    36. buttons[1] = new QPushButton("Thread 2", this);
    37. layout->addWidget(buttons[1]);
    38. threads[1] = new boutonThread(this, 2);
    39. QObject::connect(this->buttons[1], SIGNAL(clicked()), threads[1], SLOT(threadClicked()));
    40. threads[0]->start();
    41. threads[1]->start();
    42. }
    43.  
    44. boutonThread::boutonThread(QWidget *parent, int number)
    45. {
    46. this->thread_ids.resize(2);
    47. this->thread_ids[number-1] = (long)QThread::currentThread();
    48. this->val = 0;
    49. }
    50.  
    51. int
    52. boutonThread::GetId()
    53. {
    54. return this->thread_ids.indexOf((long)QThread::currentThread()) + 1;
    55. }
    56.  
    57. void
    58. boutonThread::threadClicked()
    59. {
    60. QMessageBox *msg = new QMessageBox(QMessageBox::NoIcon, QString(), QString("Thread %1 : val = %2").arg(this->GetId()).arg(this->val), QMessageBox::Ok);
    61. this->val++;
    62. msg->exec();
    63. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread misunderstanding...I suppose!

    Define val as static. And of course :
    1. Don't init value in class constructor.
    2. Use some method of synchronizing access to val

  3. #3
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QThread misunderstanding...I suppose!

    Apparently you are not familiar with C++ and Object-Oriented Programing.
    You have made so many mistakes that I don't know how to start to fix it (Lesiok didn't even scratch the surface of the problem).

    Maybe it will be better if you explain what are you trying to do.
    Last edited by MarekR22; 4th April 2013 at 13:19.

  4. #4
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QThread misunderstanding...I suppose!

    Quote Originally Posted by MarekR22 View Post
    Apparently you are not familiar with C++ and Object-Oriented Programing.
    You have made so many mistakes that I don't know how to start.
    Ok man so describes these "so many mistakes", I am really curious about it.


    Added after 7 minutes:


    Quote Originally Posted by Lesiok View Post
    Define val as static. And of course :
    1. Don't init value in class constructor.
    2. Use some method of synchronizing access to val
    You mean declaring val outside of the QThread subclass declaration?
    In the examples given in the doc - mandelbrot and fortune client - there is no such declaration and I suppose that it works despite it, doesn't it?
    2. Right but there is no concurrent access at the same time, due to button based triggering, and anyway this program is just for testing purpose.
    Last edited by Caius Aérobus; 4th April 2013 at 13:23.

  5. #5
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QThread misunderstanding...I suppose!

    1. sublclassing QThread. In general it is not needed but thing that you did is useless, you didn't override run(), this slot will not be invoked in this thread; simply you don't have mutithreading.
    2. variable 'val' in strange place it should be in some other object
    3. QVector<long> thread_ids; - why here? what are you doing?
    4. (long)QThread::currentThread() - very nasty thing why you need that?


    so in general you have such mess that I'm unable to fix, mainly because I don't known what are you trying to achieve.
    Last edited by MarekR22; 4th April 2013 at 13:48.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 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 misunderstanding...I suppose!

    Quote Originally Posted by Caius Aérobus View Post
    Ok man so describes these "so many mistakes", I am really curious about it.
    I'd begin with declaring a class member variable and expecting it to be shared by multiple instances of that class.

    Since you say you are familiar with POSIX threads, I'm also surprised that you didn't do anything to synchronize access to the shared variable. Not that your threads are doing anything, as Marek has already explained...

    Your calls to currentThread() also don't make any sense -- you can easily verify they all return the same value regardless where and how you call them.
    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.


  7. #7
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QThread misunderstanding...I suppose!

    Ok so you just did not understand what I want to do and conclude that I am not familiar with C++, funny conclusion!
    Quote Originally Posted by MarekR22 View Post
    1. sublclassing QThread. In general it is not needed but thing that you did is useless, you didn't override run(), this slot will not be invoked in this thread; simply you don't have mutithreading.
    It is just that the default QThread::run() method is enough for my needs, is it wrong? I just want to have 2 threads runnning and reacting to signals so I define a method to be attached to a signal and I do not have anything special to code in run(), the code is in the slot. May be not the best way to do it but it seems that this works actually.
    Quote Originally Posted by MarekR22 View Post
    1. variable 'val' in strange place it should be in some other object
    This is a variable expected to be shared by all the threads, I believed that all instances of a class inherited by QThread share common variables but actually I did not find any explanation on the way Qt has made things with Threads so I have to imagine how it works actually.
    Quote Originally Posted by MarekR22 View Post
    1. QVector<long> thread_ids; - why here? what are you doing?
    Probably but I did not find a better way of identifying threads so as to determine which one is number 1 and which one is number 2.
    Quote Originally Posted by MarekR22 View Post
    1. (long)QThread::currentThread() - very nasty thing why you need that?

    so in general you have such mess that I'm unable to fix that, mainly because I don't known what are you trying to achieve.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 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 misunderstanding...I suppose!

    Quote Originally Posted by Caius Aérobus View Post
    It is just that the default QThread::run() method is enough for my needs, is it wrong? I just want to have 2 threads runnning and reacting to signals so I define a method to be attached to a signal and I do not have anything special to code in run(), the code is in the slot. May be not the best way to do it but it seems that this works actually.
    No, it doesn't work. Your QThread object lives in the main thread so all its slots are executed in context of the main thread.



    This is a variable expected to be shared by all the threads, I believed that all instances of a class inherited by QThread share common variables but actually I did not find any explanation on the way Qt has made things with Threads so I have to imagine how it works actually.

    Probably but I did not find a better way of identifying threads so as to determine which one is number 1 and which one is number 2.[/QUOTE]
    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.


  9. #9
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QThread misunderstanding...I suppose!

    [QUOTE=wysota;242054]No, it doesn't work. Your QThread object lives in the main thread so all its slots are executed in context of the main thread.

    But this way the "val" variable should be modified in the context of the main thread hence it should increase by 1 each time the slot is executed, isn't it?
    BTW I have defined run() in my buttonThread class, just putting exec() in it, and got the same result.

  10. #10
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QThread misunderstanding...I suppose!

    The problem you are facing is not due to QThread, it basically ia a C++ object concept problem. I tried to modify your code at a minimum to get to work the way you wanted.

    BTW, I should say that your OP has nothing to with QThread (As already explained by others). You just defined them and off-course started them, but they just don't do anything. May be you were just doing some experiment.

    Qt Code:
    1. #include <QtGui>
    2. #include <QApplication>
    3.  
    4.  
    5. class buttonThread : public QThread
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10. buttonThread(QWidget *parent = 0, int number = 0);
    11.  
    12. public slots:
    13. void threadClicked();
    14.  
    15. private:
    16. static int val;
    17. QVector<long> thread_ids;
    18. int GetId();
    19. };
    20.  
    21. int buttonThread::val = 0;
    22.  
    23. class boutons : public QWidget
    24. {
    25. Q_OBJECT
    26.  
    27. public:
    28. boutons(QWidget *parent = 0);
    29.  
    30. private:
    31. QPushButton *buttons[2];
    32. buttonThread *threads[2];
    33. };
    34.  
    35. boutons::boutons(QWidget *parent)
    36. {
    37. QHBoxLayout *layout = new QHBoxLayout(this);
    38. buttons[0] = new QPushButton("Thread 1", this);
    39. layout->addWidget(buttons[0]);
    40. threads[0] = new buttonThread(this, 1);
    41. QObject::connect(this->buttons[0], SIGNAL(clicked()), threads[0], SLOT(threadClicked()));
    42. buttons[1] = new QPushButton("Thread 2", this);
    43. layout->addWidget(buttons[1]);
    44. threads[1] = new buttonThread(this, 2);
    45. QObject::connect(this->buttons[1], SIGNAL(clicked()), threads[1], SLOT(threadClicked()));
    46. threads[0]->start();
    47. threads[1]->start();
    48. }
    49.  
    50. buttonThread::buttonThread(QWidget *parent, int number)
    51. {
    52. this->thread_ids.resize(2);
    53. this->thread_ids[number-1] = (long)QThread::currentThread();
    54. }
    55.  
    56. int
    57. buttonThread::GetId()
    58. {
    59. return this->thread_ids.indexOf((long)QThread::currentThread()) + 1;
    60. }
    61.  
    62. void
    63. buttonThread::threadClicked()
    64. {
    65. QMessageBox *msg = new QMessageBox(QMessageBox::NoIcon, QString(), QString("Thread %1 : val = %2").arg(this->GetId()).arg(val), QMessageBox::Ok);
    66. val++;
    67. msg->exec();
    68. }
    69.  
    70. int main(int argc, char *argv[])
    71. {
    72. QApplication app(argc, argv);
    73. boutons w;
    74.  
    75. w.show();
    76. return app.exec();
    77. }
    78.  
    79. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  11. #11
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QThread misunderstanding...I suppose!

    Quote Originally Posted by Santosh Reddy View Post
    The problem you are facing is not due to QThread, it basically ia a C++ object concept problem. I tried to modify your code at a minimum to get to work the way you wanted.

    BTW, I should say that your OP has nothing to with QThread (As already explained by others). You just defined them and off-course started them, but they just don't do anything. May be you were just doing some experiment.
    What you did is declare val as static and yes I do know what a static variable is but do you mean threads of a same class only share global variables???
    And why do you say my threads are doing nothing? I see each thread displaying a message with its own id so I suspect that 2 different threads runs at the same time, wrong?

  12. #12
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QThread misunderstanding...I suppose!

    Hi,

    do you mean threads of a same class only share global variables???
    All objects of a class share their static variables but not the other ones. Every object of a class have a copy of the variable if it is not defined as static.

    And why do you say my threads are doing nothing?
    You have to redefine the "run()" method and do whaterver you want to do there.
    If you want to each thread have it's event loop to make that a slot is executed from the thread you have to call "exec()" into the "run()" method.
    Another way to use a thread is to use a loop into the "run()" method to do a heavy process, ...

    I recommend you to take a look at Qt thread examples.
    Òscar Llarch i Galán

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 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 misunderstanding...I suppose!

    Quote Originally Posted by Caius Aérobus View Post
    What you did is declare val as static and yes I do know what a static variable is but do you mean threads of a same class only share global variables???
    There is no such concept as "threads of the same class".

    I see each thread displaying a message with its own id so I suspect that 2 different threads runs at the same time, wrong?
    What happens if you do not call start() on the threads at all? If it were the threads displaying those messages, you should not be getting them at all, right?
    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.


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

    Default Re: QThread misunderstanding...I suppose!

    Quote Originally Posted by ^NyAw^ View Post
    Hi,


    All objects of a class share their static variables but not the other ones. Every object of a class have a copy of the variable if it is not defined as static.


    You have to redefine the "run()" method and do whaterver you want to do there.
    If you want to each thread have it's event loop to make that a slot is executed from the thread you have to call "exec()" into the "run()" method.
    Another way to use a thread is to use a loop into the "run()" method to do a heavy process, ...

    I recommend you to take a look at Qt thread examples.
    a QThread will call exec() in run() by default unless it is overridden.
    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.

  15. #15
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QThread misunderstanding...I suppose!

    Ok I think I start to understand how things go. I have modified the code (see below), all threads are running (see prints) but only the main thread executes the slot so what is wrong now?

    Qt Code:
    1. class buttonThread : public QThread
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. buttonThread(QWidget *parent = 0, int number = 0);
    7.  
    8. public:
    9. void run();
    10.  
    11. public slots:
    12. void threadClicked();
    13.  
    14. private:
    15. QVector<long> thread_ids;
    16. static int val;
    17. int GetId();
    18. };
    19.  
    20. class buttons : public QWidget
    21. {
    22. Q_OBJECT
    23.  
    24. public:
    25. buttons(QWidget *parent = 0);
    26.  
    27. private:
    28. QPushButton *pushb[2];
    29. buttonThread *threads[2];
    30. };
    31.  
    32. int buttonThread::val = 0;
    33.  
    34. buttons::buttons(QWidget *parent)
    35. {
    36. printf("buttons(): QThread::currentThread() = %p\n", QThread::currentThread());
    37. QHBoxLayout *layout = new QHBoxLayout(this);
    38. this->pushb[0] = new QPushButton("Thread 1", this);
    39. layout->addWidget(this->pushb[0]);
    40. threads[0] = new buttonThread(this, 1);
    41. QObject::connect(this->pushb[0], SIGNAL(clicked()), threads[0], SLOT(threadClicked()));
    42. this->pushb[1] = new QPushButton("Thread 2", this);
    43. layout->addWidget(this->pushb[1]);
    44. threads[1] = new buttonThread(this, 2);
    45. QObject::connect(this->pushb[1], SIGNAL(clicked()), threads[1], SLOT(threadClicked()));
    46. threads[0]->start();
    47. threads[1]->start();
    48. }
    49.  
    50. buttonThread::buttonThread(QWidget *parent, int number)
    51. {
    52. this->thread_ids.resize(2);
    53. this->thread_ids[number-1] = (long)QThread::currentThread();
    54. this->val = 0;
    55. }
    56.  
    57. int
    58. buttonThread::GetId()
    59. {
    60. printf("GetId(): QThread::currentThread() = %p\n", QThread::currentThread());
    61. return this->thread_ids.indexOf((long)QThread::currentThread()) + 1;
    62. }
    63.  
    64. void
    65. buttonThread::threadClicked()
    66. {
    67. QMessageBox *msg = new QMessageBox(QMessageBox::NoIcon, QString(), QString("Thread %1 : val = %2").arg(this->GetId()).arg(this->val), QMessageBox::Ok);
    68. this->val++;
    69. msg->exec();
    70. }
    71.  
    72. void
    73. buttonThread::run()
    74. {
    75. printf("run(): QThread::currentThread() = %p\n", QThread::currentThread());
    76. this->exec();
    77. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. buttons(): QThread::currentThread() = 0x101300e90
    2. run(): QThread::currentThread() = 0x1011070b0
    3. run(): QThread::currentThread() = 0x10114fc70
    4. GetId(): QThread::currentThread() = 0x101300e90
    5. GetId(): QThread::currentThread() = 0x101300e90
    6. GetId(): QThread::currentThread() = 0x101300e90
    To copy to clipboard, switch view to plain text mode 

  16. #16
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QThread misunderstanding...I suppose!

    The OP
    The expected behaviour of my code is both threads increasing a shared variable named val.
    So your problem is solved.

    I have modified the code (see below), all threads are running (see prints) but only the main thread executes the slot so what is wrong now?
    The word "wrong" is very subjective. IMO as this exercise is to understand QThread, I will say if the code does what you wanted it do then it not "worng".
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  17. #17
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QThread misunderstanding...I suppose!

    Hi,

    Don't create GUI elements on another thread that is not the main thread, so forget to create a QMessageBox on the thread slot. This will get you app crash.
    Use "Qt::QueuedConnection" as the last parameter of the connection between the signal and the slot.
    Òscar Llarch i Galán

  18. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 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 misunderstanding...I suppose!

    Quote Originally Posted by Caius Aérobus View Post
    Ok I think I start to understand how things go. I have modified the code (see below), all threads are running (see prints) but only the main thread executes the slot so what is wrong now?
    Your QThread object still lives in the main thread. You have to treat an instance of QThread not as a thread itself but rather as an object controlling a thread. This object has no real relation to the thread it controls apart the fact that when the thread is started, it executes the run() method of the QThread instance.

    Read this article for more details: http://blog.qt.digia.com/blog/2010/0...oing-it-wrong/
    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.


  19. #19
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QThread misunderstanding...I suppose!

    Quote Originally Posted by wysota View Post
    Read this article for more details: http://blog.qt.digia.com/blog/2010/0...oing-it-wrong/
    Great article! Thanks a lot for this hint, it has helped me to understand how things have been made for.
    Below my final code, probably not as you would have written it but quite closer to what it should.

    Qt Code:
    1. class buttonThread : public QThread
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. buttonThread(QWidget *parent = 0) {};
    7. void run();
    8. };
    9.  
    10. class message : public QObject
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. message(QWidget *parent = 0) {};
    16.  
    17. signals:
    18. void buttonHasBeenClicked(buttonThread *, int);
    19.  
    20. public slots:
    21. void threadClicked();
    22. };
    23.  
    24. class buttons : public QWidget
    25. {
    26. Q_OBJECT
    27.  
    28. public:
    29. buttons(QWidget *parent = 0);
    30.  
    31. public slots:
    32. void buttonClicked(buttonThread *current, int);
    33.  
    34. private:
    35. QVector<QPushButton *> pushb;
    36. QVector<buttonThread *> threads;
    37. QVector<message *> msg;
    38. };
    39.  
    40. #define NBTHREADS 2
    41.  
    42. int val = 0;
    43.  
    44. buttons::buttons(QWidget *parent)
    45. {
    46. printf("buttons(): QThread::currentThread() = %p\n", QThread::currentThread());
    47. QHBoxLayout *layout = new QHBoxLayout(this);
    48. for (int i=0 ; i<NBTHREADS ; i++) {
    49. // Button
    50. this->pushb << new QPushButton(QString("Thread %1").arg(i), this);
    51. layout->addWidget(this->pushb[i]);
    52. this->msg << new message(this);
    53. QObject::connect(this->pushb[i], SIGNAL(clicked()), this->msg[i], SLOT(threadClicked()));
    54. QObject::connect(this->msg[i], SIGNAL(buttonHasBeenClicked(buttonThread *, int)), this, SLOT(buttonClicked(buttonThread *, int)));
    55. // Thread
    56. this->threads << new buttonThread(this);
    57. this->msg[i]->moveToThread(this->threads[i]);
    58. // Start thread
    59. this->threads[i]->start();
    60. }
    61. }
    62.  
    63. void
    64. buttons::buttonClicked(buttonThread *current, int value)
    65. {
    66. int id = this->threads.indexOf(current);
    67. if (id == -1) {
    68. QMessageBox *msg = new QMessageBox(QMessageBox::NoIcon, QString("Error"), QString("buttons::buttonClicked: current=%1 not found in thread list").arg((long)current, 0, 16), QMessageBox::Ok);
    69. msg->exec();
    70. return;
    71. }
    72. QMessageBox *msg = new QMessageBox(QMessageBox::NoIcon, QString("Button clicked"), QString("button %1 clicked - val = %2").arg(id).arg(value), QMessageBox::Ok);
    73. msg->exec();
    74. }
    75.  
    76. void
    77. message::threadClicked()
    78. {
    79. printf("threadClicked(): QThread::currentThread() = %p\n", QThread::currentThread());
    80. emit buttonHasBeenClicked((buttonThread *)QThread::currentThread(), val++);
    81. }
    82.  
    83. void
    84. buttonThread::run()
    85. {
    86. printf("run(): QThread::currentThread() = %p\n", QThread::currentThread());
    87. this->exec();
    88. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 4th October 2012, 14:49
  2. QSharedDate - possible misunderstanding
    By kornicameister in forum Qt Programming
    Replies: 11
    Last Post: 10th February 2011, 19:10
  3. Misunderstanding QTestLib's TestCase term
    By lyuts in forum Qt Programming
    Replies: 4
    Last Post: 13th July 2010, 11:34
  4. QSortFilterProxyModel.invalidateFilter() misunderstanding
    By aspidites in forum Qt Programming
    Replies: 2
    Last Post: 23rd April 2009, 13:17
  5. parent() heirarchy misunderstanding?
    By KShots in forum Qt Programming
    Replies: 2
    Last Post: 16th October 2007, 18:18

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.