Hi I am trying to create thread in console application on Qt.
My main method is :
#include<featurematcher.h>
#include<QCoreApplication>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
FeatureMatcher * fm = new FeatureMatcher();
fm->start();
return a.exec();
}
My FeatureMatches class is as follows :
#ifndef FEATUREMATCHER_H
#define FEATUREMATCHER_H
#include<QThread>
class FeatureMatcher:public QThread
{
Q_OBJECT
public:
FeatureMatcher();
void run();
};
#endif // FEATUREMATCHER_H
and cpp file :
#include "featurematcher.h"
#include <iostream>
FeatureMatcher::FeatureMatcher()
{
}
void FeatureMatcher::run()
{
std::cout<<"Process"<<std::endl;
}
My problem is that when I start running program it only calls run method once.I was expecting output to be infinite number of "process" printed out but it is only printed once. Where I am missing ?
First of all, it's generally not a good idea to inherit QThread
. But, if you absolutely have to do it, you'll have to implement loop yourself. You can do it in two ways.
You can create a QTimer
and then run QThread::exec
:
void FeatureMatcher::run()
{
this->moveToThread(this);
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), SLOT(onTimer()));
timer->setInterval(1000);
timer->start();
exec();
}
or you can create an infinite loop:
void FeatureMatcher::run()
{
while (1) {
std::cout<<"Process"<<std::endl;
}
}
Updated first example #2.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments