Search code examples
c++multithreadingqthookassert

How to call QDialog::exec in a separate tread avoiding any signals (including repaint) to main thread?


I want to create my oun ASSERT/VERIFY message box. So I use:

int _CrtSetReportHook(int reportType, char * message, int * returnValue)

The hook itself is:

int MyReportHook(int reportType, char * message, int * returnValue)
{
    ...
    QDialog assertDlg;
    ...
    assertDlg.exec();
}

It works, but if I have an ASSERT on paint event (in other tread, generally), I will crash. So... How can I fix this problem?

EDIT Generally, I want to call QDialog::exec() while executing paintEvent(QPaintEvent * event). So, I need to prevent calling the whole QApplication's repainting (and processing any other signals for sure).


Solution

  • The simple answer is: you can't. There are two solutions that will work from an arbitrary thread:

    1. Use the MessageBox winapi.

    2. Send a signal to a QObject that lives in the GUI thread. This signal will be received in the GUI thread and can interact with the user. If you care about the return value, you can use a synchronization object to wait for the GUI thread to indicate that the message box was processed and that the return value is available.