April 28, 2012

wimschuiteman wimschuiteman
Lab Rat
23 posts

QThread and QObject

 

Hello

I have a problem with Qthread. I would like to run a Functie of my Serial class in a thread. And start the thread in my mainwindow class.
But when I run my application I get the following error:

  1. Start Thread
  2. QThread: Destroyed while thread is still running
  3. The program has unexpectedly finished.
  4. Allflowlift.exe exited with code -1073741819

the Mainwindow

  1. void MainWindow::Serial_Connect()
  2. {
  3.     qDebug() << "Start Thread";
  4.     QThread cThread;
  5.     Serial sSerial;
  6.     sSerial.Serial_Thread_Start(cThread);
  7.     sSerial.moveToThread(&cThread);
  8.     cThread.start();
  9.     bSerialOn = true;
  10. }

The Serial.cpp:

  1. Serial::Serial(QObject *parent)
  2.     :QObject(parent)
  3. {
  4. }
  5.  
  6. void Serial::Serial_Thread_Start(QThread &cThread)
  7. {
  8.     connect(&cThread, SIGNAL(started()), this, SLOT(Test()));
  9. }
  10.  
  11. void Serial::Test()
  12. {
  13.     qDebug() << "Tread";
  14. }

The Serial.h

  1. class Serial : public QObject
  2. {
  3.     Q_OBJECT
  4. public:
  5.     Serial(QObject *parent = 0);
  6.     void Serial_Thread_Start(QThread &cThread);  
  7.    
  8. private slots:
  9.      void Test();
  10.  
  11. signals:
  12.  
  13.  
  14. };

Know someone maybe what I’m doing wrong?

 Signature 

Regards,
Wim

6 replies

April 28, 2012

sierdzio sierdzio
Area 51 Engineer
2333 posts

You are declaring the thread on a stack. It goes out of scope on line 10. of the first snippet you’ve provided, and is destroyed. You should refactor that bit – either declare the thread in your MainWindow class, or use a pointer.

 Signature 

(Z(:^

April 29, 2012

wimschuiteman wimschuiteman
Lab Rat
23 posts

I have now set “QThread cThread;” in my mainwindow.h

The applicatie run now but. The Thread run not the code of the Serial::Test() functie.

The Serial.cpp

  1. void Serial::Serial_Thread_Start(QThread &cThread)
  2. {
  3.     connect(&cThread, SIGNAL(started()), this, SLOT(Test()));
  4.     qDebug() << "Connect Thread";
  5. }
  6.  
  7. void Serial::Test()
  8. {
  9.     for(int i = 0; i < 100; i ++)
  10.     {
  11.         qDebug() << "Tread" << i;
  12.     }
  13. }

 Signature 

Regards,
Wim

April 29, 2012

1+1=2 1+1=2
Hobby Entomologist
309 posts

But you still did not follow sierdzio’s advice to modify “Serial sSerial;”, right?

April 29, 2012

wimschuiteman wimschuiteman
Lab Rat
23 posts

I have declare the thread in my MainWindow.

  1. namespace Ui {
  2.     class MainWindow;
  3. }
  4.  
  5. class MainWindow : public QMainWindow
  6. {
  7.     Q_OBJECT
  8.  
  9. public:
  10.     explicit MainWindow(QWidget *parent = 0);
  11.     ~MainWindow();
  12.  
  13. private:
  14.     QThread cThread;
  15.     Ui::MainWindow *ui;
  16. };

 Signature 

Regards,
Wim

April 29, 2012

1+1=2 1+1=2
Hobby Entomologist
309 posts

Yes, I see, but your

  1. Serial sSerial;

still “goes out of scope on line 10. of the first snippet you’ve provided, and is destroyed. “

April 29, 2012

VRonin VRonin
Lab Rat
32 posts

modify

  1. Serial sSerial;
  2.     sSerial.Serial_Thread_Start(cThread);
  3.     sSerial.moveToThread(&cThread);

to:
  1. Serial* sSerial=new Serial(this);
  2.     sSerial->Serial_Thread_Start(cThread);
  3.     sSerial->moveToThread(&cThread);

 Signature 

“La mort n’est rien, mais vivre vaincu et sans gloire, c’est mourir tous les jours”
~Napoleon Bonaparte

 
  ‹‹ Simulating a KeyBoardPress system wide in Qt?      [SOLVED] How to change the background color of selected text in QGraphicsTextItem? ››

You must log in to post a reply. Not a member yet? Register here!