Arm开发板+Qt学习之路-qt线程执行完毕发送signal主动释放线程内存

header:

#ifndef SENDCANMSGTHREAD_H
#define SENDCANMSGTHREAD_H

#include <QThread>
#include "can/canUtils.h"
#include <QTime>
#include <iostream>


class SendCanMsgThread : public QThread{

Q_OBJECT
public:
SendCanMsgThread();
~SendCanMsgThread();

signals:
void canResponseError(SendCanMsgThread *sendCanMsgThread);

};

#endif // SENDCANMSGTHREAD_H

 

cpp

 

#include "can/sendCanMsgThread.h"

SendCanMsgThread::SendCanMsgThread(){
isRunning = true;
responsed = false;
currentRequestTimes = 0;
}

SendCanMsgThread::~SendCanMsgThread(){
std::cerr<<" ~SendCanMsgThread() "<<std::endl;
}

void SendCanMsgThread::run(){
if(1){
emit canResponseError(this);
}

}

mainWindow.cpp

 

void MainWindow::showPickupStautus(){


SendCanMsgThread *sendCanMsgThread = new SendCanMsgThread();

sendCanMsgThread->start();

connect(sendCanMsgThread,SIGNAL(canResponseError(SendCanMsgThread*)),this,SLOT(canResponseError(SendCanMsgThread *)));
}

//红色地方很重要,一个是指针传参的方式  二是信号和槽这只能定义参数类型,不能出现具体参数,否则提示No such signal

void MainWindow::canResponseError(SendCanMsgThread *sendCanMsgThread ){
//can通讯异常提示

std::cerr<<" canResponseError "<<std::endl;
delete sendCanMsgThread;
sendCanMsgThread = NULL;

QMessageBox msgBox;
msgBox.setText("Could not get can response " );
msgBox.exec();

}

posted @ 2016-04-05 11:04  维克.贺仔  阅读(815)  评论(0编辑  收藏  举报