QThread error , was not defined in this scope

560 views Asked by At

I was hoping to get some help figuring out where I went wrong on my code for a QThread. This is the first time doing threading and have been reading and watching tutorials, but am still hvaing a hard time. Herr is my code

currentTimeThread.h (my thread)

#ifndef CURRENTTIMETHREAD_H
#define CURRENTTIMETHREAD_H
#include <QtCore>

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

#endif // CURRENTTIMETHREAD_H

currentTimeThread.cpp

#include "currenttimethread.h"
#include <QtCore>
#include <QDebug>
#include "noheatmode.h"

currentTimeThread::currentTimeThread()
{
}

void currentTimeThread::run()
{
 QTime time = QTime::currentTime();
 QString sTime = time.toString("hh:mm:ss:ms");
 noheatmode::ui->tempTimeNoHeatMode->append(sTime);
} 

and my noHeatMode.cpp when the thread is called/started

#include "noheatmode.h"
#include "ui_noheatmode.h"
#include "wiringPi.h"
#include "currenttimethread.h"
#include <QTime>
#include <QTextEdit>
#include <QTimer>
#include <QString>



noheatmode::noheatmode(QWidget *parent) :
QWidget(parent),
ui(new Ui::noheatmode)
{
    ui->setupUi(this);


}

noheatmode::~noheatmode()
{
   delete ui;
}


 while(flowTime > 0)

      currentTimeThread timeThread;
      timeThread.start();


      {// set second pin LED to flash according to dutyCycle
       digitalWrite(2,1);
       delay(onTime);
       digitalWrite(2,0);
       delay(offTime);

      //set zero pin to be high while flowtime is more than 0
      digitalWrite(0,1);

      flowTime--;
      }

The issues it, I am getting an error that the timeThread of

currentTimeThread timeThread

is not declared. What is the issue?

2

There are 2 answers

0
king_nak On

You are misplaced the braces in your while loop:

while(flowTime > 0)
{ // <---- HERE
  currentTimeThread timeThread;
  timeThread.start();


  // set second pin LED to flash according to dutyCycle
  digitalWrite(2,1);
  delay(onTime);
  digitalWrite(2,0);
  delay(offTime);

  //set zero pin to be high while flowtime is more than 0
  digitalWrite(0,1);

  flowTime--;
}

Otherwise, the code is equivalent to this

while (flowTime > 0)
{
    currentTimeThread timeThread;
}
// timeThread doesn't exist anymore
// Rest of code
2
Xeyos On

I think you're getting it the worng way. By the time (I think we're talking about QT3), QT has change their way of using threads and did confuse us, the users, a little bit, but it is easy when you get it. You should never extend QThread, just use the class they're offering you.

The scheme in your case would be as follows:

  • create a class with your loop
  • create on this class a slot, which is a method that can be invoked from another thread.
  • from the main class, create an instance of your loopClass and another one of QThread
  • Use movetothread over your loopObject using the QthreadObject as parameter
  • send a signal to your loopObject's slot so it can start working

You can see a full explanation in this article: https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/ it helped me a lot ;)

Sorry i haven't the time by now to code a full example, if you need it let me know and i can lend you a hand latter.