Ran*_*952 2 c++ qt multithreading class qthread
我想在主应用程序的单独线程中运行代码,因为我hava创建了一些文件:
thread2.h
#ifndef THREAD2_H
#define THREAD2_H
#include <QThread>
class thread2 : public QThread
{
Q_OBJECT
public:
thread2();
protected:
void run();
};
#endif // THREAD2_H
Run Code Online (Sandbox Code Playgroud)
thread2.cpp
#include "thread2.h"
thread2::thread2()
{
//qDebug("dfd");
}
void thread2::run()
{
int test = 0;
}
Run Code Online (Sandbox Code Playgroud)
并且主文件名为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();
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用......
Qt Creator告诉我:"无法调用成员函数'虚拟void thread2 :: run()'没有对象"
谢谢 !
像这样调用它:thread2::run()你是如何调用static函数的,而run()不是.
另外,要启动一个线程,你没有run()显式调用该方法,你需要创建一个线程对象并调用start()它,它应该run()在适当的线程中调用你的方法:
thread2 thread;
thread.start()
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8748 次 |
| 最近记录: |