Hi, I am new to C++ and Qt and despite that i am thinking of using threads in my app. Here is an example code which outputs "." and then "QThread: Destroyed while thread is still running" error after pressing button.

class MyThread : public QThread
      {
          public:
              void run();
      };

      void MyThread::run()
      {
          while (true)
          {
              fprintf(stderr,".");
              sleep(5);
          }
      }

      void MainWindowImpl::on_button_clicked()
      {
          MyThread t;
          t.start();
      }

What can be wrong in this code? Am I not allowed to use while loop in thread?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.