i want to run code in a seperate thread of the main application, for that i hava created some file :
thread2.h
#ifndef THREAD2_H
#define THREAD2_H
#include <QThread>
class thread2 : public QThread
{
Q_OBJECT
public:
thread2();
protected:
void run();
};
#endif // THREAD2_H
thread2.cpp
#include "thread2.h"
thread2::thread2()
{
//qDebug("dfd");
}
void thread2::run()
{
int test = 0;
}
And the main file called main.cpp
#include <QApplication>
#include <QThread>
#include "thread1.cpp"
#include "thread2.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
thread2::run();
return a.exec();
}
But it dosen’t work…
Qt Creator tell me : “cannot call member function ‘virtual void thread2::run()’ without object”
Thanks !
Invoking it like this:
thread2::run()is how you would call astaticfunction, whichrun()is not.Also, to start a thread you don’t call the
run()method explicitly, you need to create a thread object and callstart()on it which should invoke yourrun()method in the appropriate thread: