简体   繁体   中英

How to get back to main GUI thread when handling an interrupt?

I have a Raspberry Pi 3 running a GUI program written in Qt. I'm using the wiringPi library to set an interrupt that fires when a certain GPIO pin goes low. When this happens, I want a dialog window to appear telling the user that the Pi will shutdown in 10 seconds, during which they have the option to cancel the shutdown.

The problem is that the function that receives the interrupt is launched in a new thread, and Qt does not allow timers, etc. to be used outside of the main thread. I would like to know how I can communicate back to the main thread from the interrupt function. The function accepts no arguments, btw.

Example code:

MainWindow::MainWindow() {
    wiringPiSetup();
    //Set up an interrupt to detect when WiringPI pin 0 (header #11) goes low
    //Call the ShutdownISR function when this happens.
    wiringPiISR(0, INT_EDGE_FALLING, &ShutdownISR);
}

//Non-member, free function. Handles interrupt.
void ShutdownISR() {
    //Crashes the program with errors about doing GUI stuff outside the main thread
    ShutdownDialog* sdDlg = new ShutdownDialog();
    sdDlg->exec();
} 

AFAIU interrupts are only handled by the Linux kernel and are not directly visible to application code. However, be aware of unix signals and read signal(7) & signal-safety(7) & Advanced Linux Programming & Operating Systems : Three Easy Pieces

Regarding Qt and signals, it is documented; see Calling Qt Functions From Unix Signal Handlers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM