来自qtconcurrent :: run的Qmessagebox [英] Qmessagebox from qtconcurrent::run

查看:261
本文介绍了来自qtconcurrent :: run的Qmessagebox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从多线程循环中调用一个消息框。我的循环在一个从QtConcurrent :: run调用的函数(ReadThread)中运行。在该功能中,应用程序继续狡猾地读取硬件设备。我想根据硬件响应弹出一个消息框。



如何从ReadThread()函数调用消息框?我对使用QFutureWatcher不感兴趣。在我的另一个代码中,我想继续相同的循环。关闭消息框后。



I am trying to call a message box from a multi threaded loop. My loop is running in a function (ReadThread) that called from the QtConcurrent::run. In that function the application is continue sly reading a hardware device. I want to pop up a message box based on the hardware response.

How I can call a message box from ReadThread() function? I am not interested to use QFutureWatcher. In my another code I want to continue the same loop. after closing the messagebox.

m_Future->waitForFinished();
*m_Future = QtConcurrent::run(this, &MainWindow::ReadThread);





我尝试过:





What I have tried:

m_Future->waitForFinished();
*m_Future = QtConcurrent::run(this, &MainWindow::ReadThread);




void MainWindow::ReadThread()
{
  while(true)
  {
    if(ui->fanviewer->ReadAndUpdateFanData() == false)
    {
       QMessageBox::information(this,tr("An error occured"), tr("Reading     failed."));
     return;
    }
 }
}

推荐答案

应始终在主GUI线程中执行用户交互。



一个可能的解决方案是使用一个事件来指示主线程显示消息。



在MainWindow头文件中将事件ID定义为公共成员。请注意,该值必须是应用程序范围唯一的(下面的示例假定第一个可用值 QEvent :: User 已经在某处使用过):

User interactions should be always performed in the main GUI thread.

A possible solution would be using an event that signals your main thread to display the message.

Define the event ID as public member in the MainWindow header file. Note that the value must be application wide unique (the below example assumes that the first available value QEvent::User is already used somewhere):
static const int ReadThreadErrEv = QEvent::User + 1;





处理customEvent [ ^ ](不要忘记将声明添加到头文件中):



Handle the customEvent[^] (don't forget to add the declaration to the header file):

void MainWindow::customEvent(QEvent *event)
{
    if (static_cast<int>(event->type()) == ReadThreadErrEv)
    {
        QMessageBox::information(this,
            tr("An error occured"), 
            tr("Reading failed. Check error log for more details"));
    }
}





在工作线程中触发事件(请注意,指向主窗口的指针是作为附加参数传递):



Fire the event in the worker thread (note that a pointer to your main window is passed as additional argument):

void MainWindow::ReadThread(MainWindow *pMainWindow)
{
    if(ui->fanviewer->ReadAndUpdateFanData() == false)
    {
        QCoreApplication::postEvent(pMainWindow, 
            new QEvent((QEvent::Type)MainWindow::ReadThreadErrEv));
    }
}





启动传递MainWindow指针的线程。请注意,我已经在线程启动后调用了wait函数:



Start the thread passing the MainWindow pointer. Note that I have moved the wait function to be called after the thread has started:

*m_Future = QtConcurrent::run(this, &MainWindow::ReadThread, this);
m_Future->waitForFinished();


这篇关于来自qtconcurrent :: run的Qmessagebox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆