直接上代码吧,注意这种方法创建的多线程,只有run函数内的属于新的线程,OnTempTimer()虽然是run函数调用的,但是是主线程在执行,一定要注意啊。这样弄相当于没有任何意义,还是主线程在处理函数。
下一个博客有讲怎么实现多线程的正确创建和使用
MainWindow.cpp
MysqlThread = new ReconnectMysql();
MysqlThread->start();
reconnectmysql.cpp
#include "reconnectmysql.h"
ReconnectMysql::ReconnectMysql()
{
}
void ReconnectMysql::run()
{
qDebug()<< "run()";
//Timer = new QTimer(this);这样写会报错:
//Parent is ReconnectMysql(0xd621978), parent's thread is QThread(0x4ac5dd0),
//current thread is ReconnectMysql(0xd621978)
Timer = new QTimer();//或者Timer = new QTimer(NULL);
//这句话也要在run()不能像这样给定时器指定父对象 Timer = new QTimer(this);
Timer->start(1000);
connect(Timer, SIGNAL(timeout()), this, SLOT(OnTempTimer()));
this->exec();//不加这句话定时器不执行
}
void ReconnectMysql::OnTempTimer()
{
qDebug()<< &