像这种让主界面主线程进入等待的情况一般很少用到,但是在嵌入式平台中还是会用到的,在windows平台中这种等待事件,直接就开一个线程去处理了,
但是在嵌入式系统中就不一定会那么方便了。
比如我们在嵌入式系统中打印测量数据,要等待打印完成才能进行下一步操作,这时我们就要将界面锁定。等待打印完成在解锁。
方式如下:
我们需要建立一个全局变量来表示锁定和解锁状态
bool mUserInputLock;//设定为全局
我们初始为
mUserInputLock = false;
我们还需要在锁定状态下将事件过滤掉:
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if((event->type()==QEvent::TouchBegin) || (event->type()==QEvent::KeyPress) || (event->type()==QEvent::MouseButtonPress)|| (event->type()==QEvent::MouseButtonRelease)|| (event->type()==QEvent::MouseButtonDblClick)) {
if (sysInfo.mUserInputLock) {
qDebug() << "sysInfo.mUserInputLock";
return true;
}
}
return QObject::eventFilter(obj, event);
}
这里有点击事件,鼠标事件等。根据自己做处理。否则程序因为是单线程阻塞而崩溃。
接着,我们在进入打印时锁定屏幕
MainWindow::sysInfo.mUserInputLock = true;
打印完成后解锁:
if (MainWindow::sysInfo.mUserInputLock)
{
jobProgressBar->hide();
delete jobProgressBar;
qDebug() << "sysInfo.mUserInputLock";
MainWindow::sysInfo.mUserInputLock = false;
}
这里要注意,为了避免程序永远打印退出不了,我们设定一个定时器,在多久后强制解锁,否则程序就永远死在这里了。
void MainMeasure::acceptPrintWorkTimer()
{
if(recordPrintWorkTimerNum == 100)
{
if (MainWindow::sysInfo.mUserInputLock)
{
jobProgressBar->hide();
delete jobProgressBar;
qDebug() << "sysInfo.mUserInputLock";
MainWindow::sysInfo.mUserInputLock = false;
}
}else{
recordPrintWorkTimerNum++;
}
}