Results 1 to 4 of 4

Thread: thread->start() does not work ?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Default thread->start() does not work ?

    Hello,

    i'm doing the same thing for an hour, i followed this tutorial on Youtube (http://www.youtube.com/watch?v=PR6wV...f=mfu_in_order), about threads, and i don't understand why my code doesn't work :

    my label is supposed to take a number from 0 to 1000, but the label's content doesn't change, the text in the label stays "as is", if i put the text "number" in it, this string "number" won't be changed by an int from 0 to 1000. Here's my code :

    dialog.h :
    Qt Code:
    1. #ifndef DIALOG_H
    2. #define DIALOG_H
    3.  
    4. #include <QDialog>
    5. #include <QtGui>
    6. #include "mythread.h"
    7.  
    8. namespace Ui {
    9. class Dialog;
    10. }
    11.  
    12. class Dialog : public QDialog
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit Dialog(QWidget *parent = 0);
    18. ~Dialog();
    19. MyThread *mThread;
    20.  
    21. public slots:
    22. void onNumberEmit(int);
    23.  
    24. private slots:
    25. void on_pushButton_clicked();
    26. void on_pushButton_2_clicked();
    27.  
    28. private:
    29. Ui::Dialog *ui;
    30. };
    31.  
    32. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 


    dialog.cpp :
    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3.  
    4. #include "mythread.h"
    5.  
    6. Dialog::Dialog(QWidget *parent) :
    7. QDialog(parent),
    8. ui(new Ui::Dialog)
    9. {
    10. ui->setupUi(this);
    11.  
    12. mThread = new MyThread(this);
    13.  
    14. connect(mThread, SIGNAL(NumberEmit(int)), this, SLOT(onNumberEmit(int)));
    15. }
    16.  
    17. Dialog::~Dialog()
    18. {
    19. delete ui;
    20. }
    21.  
    22. void Dialog::on_pushButton_clicked()
    23. {
    24. //QMessageBox::warning(this, "alert", "all");
    25. mThread->start();
    26. }
    27.  
    28. void Dialog::on_pushButton_2_clicked()
    29. {
    30. mThread->Stop = true;
    31.  
    32. }
    33.  
    34. void Dialog::onNumberEmit(int num){
    35. ui->label->setText(QString::number(num));
    36. }
    To copy to clipboard, switch view to plain text mode 

    mythread.h :
    Qt Code:
    1. #ifndef MYTHREAD_H
    2. #define MYTHREAD_H
    3.  
    4. #include <QThread>
    5.  
    6. class MyThread : public QThread
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit MyThread(QObject *parent = 0);
    11. void run();
    12. bool Stop;
    13.  
    14. signals:
    15. void NumberEmit(int);
    16.  
    17. public slots:
    18.  
    19. };
    20.  
    21. #endif // MYTHREAD_H
    To copy to clipboard, switch view to plain text mode 

    mythread.cpp :
    Qt Code:
    1. #include "mythread.h"
    2. #include <QtCore>
    3.  
    4. MyThread::MyThread(QObject *parent) :
    5. QThread(parent)
    6. {
    7. }
    8.  
    9. void MyThread::run(){
    10. for (int i=0; i<1000; i++){
    11. QMutex mutex;
    12. mutex.lock();
    13. if (this->Stop) break;
    14. mutex.unlock();
    15.  
    16. //this->msleep(100);
    17.  
    18. emit NumberEmit(i);
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for your help !

  2. #2
    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: thread->start() does not work ?

    Quickly there are problems with the example link you mentioned. Some quick observations

    1. MyThread::stop is not initialized.
    Qt Code:
    1. MyThread::MyThread(QObject *parent) :
    2. QThread(parent)
    3. {
    4. Stop = false;
    5. }
    To copy to clipboard, switch view to plain text mode 

    2. QMutex is of no use there, it should not be used that way. Refer Documentation

    [
    Edit]
    Also refer the comments below the video it will solve your problems.
    Last edited by Santosh Reddy; 20th November 2011 at 06:36.
    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.

  3. #3
    Join Date
    Nov 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: thread->start() does not work ?

    ah I was close! i thought stop was declared to false as it is a boolean, but no Thanks a lot for your help !

Similar Threads

  1. [thread]how to suspend a work thread
    By viasant in forum Qt Programming
    Replies: 2
    Last Post: 8th April 2013, 18:48
  2. QTimer doesnt work or complains about thread
    By daemonna in forum Qt Programming
    Replies: 3
    Last Post: 1st September 2010, 17:00
  3. Replies: 9
    Last Post: 28th November 2009, 21:31
  4. How to start a thread (inline)
    By DiamonDogX in forum Qt Programming
    Replies: 4
    Last Post: 28th May 2009, 22:53
  5. Want to start work with QT GUI
    By atil in forum Newbie
    Replies: 5
    Last Post: 20th November 2007, 10:17

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.