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

久未见 提交于 2019-12-04 04:31:23

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/44788513/how-to-get-back-to-main-gui-thread-when-handling-an-interrupt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!