Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. thread does not work
Forum Updated to NodeBB v4.3 + New Features

thread does not work

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • MrErfanM Offline
    MrErfanM Offline
    MrErfan
    wrote on last edited by
    #1

    I do not know what the problem is?

    mythread.h

    #ifndef MYTHREAD_H
    #define MYTHREAD_H
    
    #include <QThread>
    
    class MyThread : public QThread
    {
        Q_OBJECT
    public:
        explicit MyThread(QObject *parent = 0);
        void run();
        bool Stop;
    
    signals:
        void NumberChanged(int);
    
    public slots:
    
    };
    
    #endif // MYTHREAD_H
    
    

    mythread.cpp

    #include "mythread.h"
    #include <QtCore>
    
    MyThread::MyThread(QObject *parent) : QThread(parent)
    {
    
    }
    
    void MyThread::run()
    {
        for(int i = 0; i < 10000; i++)
        {
            QMutex mutex;
            mutex.lock();
            if(this->Stop) break;
            mutex.unlock();
            emit NumberChanged(i);
        }
    }
    
    

    mainwindow.cpp

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        setWindowFlags(Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::Window |  Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint);
        ui->setupUi(this);
        mThread = new MyThread(this);
        connect(mThread,SIGNAL(NumberChanged(int)),this,SLOT(onNumberChanged(int)));
    }
    void MainWindow::onNumberChanged(int Number)
    {
        ui->txt1->insertPlainText(QString::number(Number));
    
    }
    void MainWindow::on_pushButton_clicked()
    {
        mThread->start();
    
    }
    

    output: QMutex: destroying locked mutex

    jsulmJ JonBJ 2 Replies Last reply
    0
    • MrErfanM MrErfan

      I do not know what the problem is?

      mythread.h

      #ifndef MYTHREAD_H
      #define MYTHREAD_H
      
      #include <QThread>
      
      class MyThread : public QThread
      {
          Q_OBJECT
      public:
          explicit MyThread(QObject *parent = 0);
          void run();
          bool Stop;
      
      signals:
          void NumberChanged(int);
      
      public slots:
      
      };
      
      #endif // MYTHREAD_H
      
      

      mythread.cpp

      #include "mythread.h"
      #include <QtCore>
      
      MyThread::MyThread(QObject *parent) : QThread(parent)
      {
      
      }
      
      void MyThread::run()
      {
          for(int i = 0; i < 10000; i++)
          {
              QMutex mutex;
              mutex.lock();
              if(this->Stop) break;
              mutex.unlock();
              emit NumberChanged(i);
          }
      }
      
      

      mainwindow.cpp

      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          setWindowFlags(Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::Window |  Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint);
          ui->setupUi(this);
          mThread = new MyThread(this);
          connect(mThread,SIGNAL(NumberChanged(int)),this,SLOT(onNumberChanged(int)));
      }
      void MainWindow::onNumberChanged(int Number)
      {
          ui->txt1->insertPlainText(QString::number(Number));
      
      }
      void MainWindow::on_pushButton_clicked()
      {
          mThread->start();
      
      }
      

      output: QMutex: destroying locked mutex

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @MrErfan You're using a local mutex! Put it in your class as member variable and unlock it before break. All threads executing this code must use same mutex else there is no synchronisation! It is better to use QMutexLocker.
      Actually there is no need for this mutex at all in this case...

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • MrErfanM Offline
        MrErfanM Offline
        MrErfan
        wrote on last edited by
        #3

        Re: thread does not work

        Thank you for your reply, but unfortunately Thread does not work.
        By clicking on the Start button, the program will hang ،
        If I use the QLabel, the program works properly, but I want to use QPlaintextedit

        void MainWindow::onNumberChanged(int Number)
        {
            ui->label->setText(QString::number(Number));
        }
        
        J.HilkJ 1 Reply Last reply
        0
        • MrErfanM MrErfan

          Re: thread does not work

          Thank you for your reply, but unfortunately Thread does not work.
          By clicking on the Start button, the program will hang ،
          If I use the QLabel, the program works properly, but I want to use QPlaintextedit

          void MainWindow::onNumberChanged(int Number)
          {
              ui->label->setText(QString::number(Number));
          }
          
          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @MrErfan try it with void QPlainTextEdit::appendPlainText(const QString &text) your TextEdit might not have a valid curse to use insert upon


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          2
          • MrErfanM MrErfan

            I do not know what the problem is?

            mythread.h

            #ifndef MYTHREAD_H
            #define MYTHREAD_H
            
            #include <QThread>
            
            class MyThread : public QThread
            {
                Q_OBJECT
            public:
                explicit MyThread(QObject *parent = 0);
                void run();
                bool Stop;
            
            signals:
                void NumberChanged(int);
            
            public slots:
            
            };
            
            #endif // MYTHREAD_H
            
            

            mythread.cpp

            #include "mythread.h"
            #include <QtCore>
            
            MyThread::MyThread(QObject *parent) : QThread(parent)
            {
            
            }
            
            void MyThread::run()
            {
                for(int i = 0; i < 10000; i++)
                {
                    QMutex mutex;
                    mutex.lock();
                    if(this->Stop) break;
                    mutex.unlock();
                    emit NumberChanged(i);
                }
            }
            
            

            mainwindow.cpp

            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
            {
                setWindowFlags(Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::Window |  Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint);
                ui->setupUi(this);
                mThread = new MyThread(this);
                connect(mThread,SIGNAL(NumberChanged(int)),this,SLOT(onNumberChanged(int)));
            }
            void MainWindow::onNumberChanged(int Number)
            {
                ui->txt1->insertPlainText(QString::number(Number));
            
            }
            void MainWindow::on_pushButton_clicked()
            {
                mThread->start();
            
            }
            

            output: QMutex: destroying locked mutex

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @MrErfan
            This does not address your problem, but would your code be a lot nicer if you used QWaitCondition?

            J.HilkJ 1 Reply Last reply
            1
            • JonBJ JonB

              @MrErfan
              This does not address your problem, but would your code be a lot nicer if you used QWaitCondition?

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @JNBarchan For that what the code does, so far as shown to us, it would be more usefull to use QtConcurrent::run


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              JonBJ 1 Reply Last reply
              1
              • J.HilkJ J.Hilk

                @JNBarchan For that what the code does, so far as shown to us, it would be more usefull to use QtConcurrent::run

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @J.Hilk His run seems to be waiting for something to set this->Stop over a period of time, that seems to be just what QWaitCondition is for, but I am a beginner so bow to superior knowledge. I just discovered QWaitCondition an hour ago :)

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved