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;
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));
}
}
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();