问题描述
- Qt使用多线程刷新界面,界面一段时间后不响应
-
我设计了一个串口工作线程和界面刷新主线程。
串口线程如下:
#include "thread.h"
#include "mainwindow.h"
#include
#includeThread::Thread()
{
//初始化}
Thread::~Thread()
{
mutex.lock();
abort = true;
mutex.unlock();wait();
}//析构
void Thread::run() //这就是线程的具体工作了
{
// qint64 f=0;
int i;
int fd;
int nread=1;
//int nwrite
int count=0;//size of writed bufffd=open("/dev/ttySAC1",O_RDONLY | O_NOCTTY); set_speed(fd,115200); if(set_Parity(fd,8,1,'N')==FALSEE) { printf("Set Parity Errorn"); exit (0); } while(!abort) { qDebug()<<"r:"<<QThread::currentThreadId(); nread=read(fd,buff1,1); qDebug()<<"rea"; if(nread>0) { buff[count]=buff1[0]; count++; // qDebug()<<count; } if(count==16) { dosomething; count=0; emit receiver16(nameflag,time,datas); msleep(85); nameflag=0; } } close(fd);
}
界面主线程:
#include "mainwindow.h"
#include
#include
#includevoid MyMainWindow::paintEvent(QPaintEvent *)
//paintEvent函数由系统自动调用,用不着我们人为的去调用。
{
QPainter paint;//1024*768
paint.begin(this);/*画一大堆东西*/ qDebug()<<"paint"; paint.end();
}
MyMainWindow::MyMainWindow(QWidget *parent)
:QWidget(parent)
{
setGeometry(0,0,1024,768);
ioread= new Thread;
timer = new QTimer;
timerPon= new QTimer;//总共4个定时器,但结构都是这样的
ioread->start(QThread::InheritPriority);
connect(ioread,SIGNAL(receiver16(int,char *,char *)),this,SLOT(display(int,char *,char *)));
connect(timer, SIGNAL(timeout()), this, SLOT(timerFunction()));
connect(timerPon, SIGNAL(timeout()), this, SLOT(timerFunctionPon()));//初始化;
}
void MyMainWindow::display(int nameflag,char *time,char *datas)
{
qDebug()<<"dis:"<<QThread::currentThreadId();DoSth……; update();
}
void MyMainWindow::timerFunction()
{
cttimerDoSth;
}void MyMainWindow::timerFunctionPon()
{
PonttimerDoSth;
}运行了一段时间(一般是几个小时后)界面卡住不动了,定时器计时都不显示了,这是怎么回事啊?