Results 1 to 6 of 6

Thread: QThread Proper Use ?? connect Problems....

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2016
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default QThread Proper Use ?? connect Problems....

    Hi everyone,

    I'm new on this forum. I'm using Qt4.8.6 for its GUI performances but now I want to use QThreads properly. I saw on the web different ways to declare and use them.

    - redefinition of methods run() of Object inheriting of QThread class => Wrong way

    - definition of a Worker and worker->moveToThread (QThread*) => Right way ??? (https://forum.qt.io/topic/14378/my-t...y-use-qthreads)

    I developed a small project with these two methods to compare them. threadA which displays "A" and threadB which displays "B". To start/stop each threads, there are buttons. Both of these methods work (displaying AAAAA when AButton pushed, BBBBBBB when BButton pushed and ABABABABABA when both are pushed...

    BUT if I follow steps of the second method (Correct use), I must connect :
    Qt Code:
    1. connect(thread, SIGNAL(started()), worker, SLOT(process())); OK
    2. connect(worker, SIGNAL(finished()), thread, SLOT(quit())); OK
    To copy to clipboard, switch view to plain text mode 
    but when I add these two connections, I have : Violation of Ox0FFFFFFFF emplacement Mutex....
    Qt Code:
    1. connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
    2. connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    To copy to clipboard, switch view to plain text mode 
    How can I do to make this error disappear ?
    Other question, in my code when i'm displaying threadID, it always displays 00000000159 (for example), for both threads ! I think I'm missing something.... Thread must NOT have the same ID ???? principles ??? (same problem in the two methods)

    this is my code, without hpp files :
    -------------------------------------------
    Worker.cpp
    Qt Code:
    1. Worker::Worker() { stopped = false;} // attribute of Worker instance
    2.  
    3. void Worker::process()
    4. {
    5. while (!stopped)
    6. std::cout << messageStr.toStdString(); // attribute of Worker instance
    7. emit finished();
    8. }
    9.  
    10. void Worker::stop() { stopped = true;}
    11. void Worker::start() {stopped = false;}
    To copy to clipboard, switch view to plain text mode 
    ---------------------------------------------
    ThreadDialog.cpp
    Qt Code:
    1. ThreadDialog::ThreadDialog(QWidget *parent) : QDialog(parent)
    2. {
    3. workerA = new Worker();
    4. workerB = new Worker();
    5.  
    6. threadAButton = new QPushButton(tr("Start A"));
    7. threadBButton = new QPushButton(tr("Start B"));
    8. quitButton = new QPushButton(tr("Quit"));
    9. quitButton->setDefault(true);
    10.  
    11. threadA = new QThread;
    12. workerA->setMessage("A ");
    13. workerA->moveToThread(threadA);
    14.  
    15. threadB = new QThread;
    16. workerB->setMessage("B ");
    17. workerB->moveToThread(threadB);
    18.  
    19. connect(threadA, SIGNAL(started()), workerA, SLOT(process()));
    20. connect(workerA, SIGNAL(finished()), threadA, SLOT(quit()));
    21. //connect(workerA, SIGNAL(finished()), workerA, SLOT(deleteLater()));
    22. //connect(threadA, SIGNAL(finished()), threadA, SLOT(deleteLater()));
    23.  
    24. connect(threadB, SIGNAL(started()), workerB, SLOT(process()));
    25. connect(workerB, SIGNAL(finished()), threadB, SLOT(quit()));
    26. //connect(workerB, SIGNAL(finished()), workerB, SLOT(deleteLater()));
    27. //connect(threadB, SIGNAL(finished()), threadB, SLOT(deleteLater()));
    28.  
    29. connect(threadAButton, SIGNAL(clicked()), this, SLOT(startOrStopThreadA()));
    30. connect(threadBButton, SIGNAL(clicked()), this, SLOT(startOrStopThreadB()));
    31. connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
    32. .... // displayLayout
    33. }
    34.  
    35. void ThreadDialog::startOrStopThreadA()
    36. {
    37. if (threadA->isRunning())
    38. {
    39. workerA->stop();
    40. threadAButton->setText(tr("Start A"));
    41. }
    42. else
    43. {
    44. std::cout << threadA->currentThreadId() << std::endl;
    45. workerA->start();
    46. threadA->start();
    47. threadAButton->setText(tr("Stop A"));
    48. }
    49. }
    50.  
    51. void ThreadDialog::startOrStopThreadB()
    52. {
    53. if (threadB->isRunning())
    54. {
    55. workerB->stop();
    56. threadBButton->setText(tr("Start B"));
    57. }
    58. else
    59. {
    60. std::cout << threadB->currentThreadId() << std::endl;
    61. workerB->start();
    62. threadB->start();
    63. threadBButton->setText(tr("Stop B"));
    64. }
    65. }
    66.  
    67. void ThreadDialog::closeEvent(QCloseEvent *event)
    68. {
    69. workerA->stop();
    70. workerB->stop();
    71. threadA->wait();
    72. threadB->wait();
    73. threadA->exit();
    74. threadB->exit();
    75.  
    76. event->accept();
    77. }
    To copy to clipboard, switch view to plain text mode 
    Please Help me to understand QThread features...
    Thank you

    GuiHanoi
    Last edited by anda_skoa; 23rd February 2016 at 10:29. Reason: missing [code] tags

Similar Threads

  1. connect and qthread
    By tom989 in forum Newbie
    Replies: 1
    Last Post: 5th May 2013, 23:56
  2. Trying to get problems with QThread
    By valdemar593 in forum Qt Programming
    Replies: 1
    Last Post: 21st May 2011, 22:26
  3. Problems with a QThread
    By franco.amato in forum Qt Programming
    Replies: 10
    Last Post: 5th April 2010, 10:46
  4. Problems with Threads connect
    By Max123 in forum Qt Programming
    Replies: 3
    Last Post: 29th March 2010, 12:20
  5. Problems with Qsemaphore and QThread
    By perseo in forum Qt Programming
    Replies: 2
    Last Post: 27th August 2008, 01:21

Tags for this Thread

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.