2013-12-12 87 views
1

我想監視連接到Beaglebone的自定義設備中的緊急停止按鈕,我的代碼是使用Qt 4.6開發的。Qt退出事件循環

目前,當按下緊急停止按鈕時,我成功地顯示了一個消息框(沒有任何按鈕)。我想要做的是繼續執行程序ONLY當緊急停止按鈕被釋放時。按鈕按下/釋放在每個事件上發出不同的信號。但是,使用此代碼時,EmergencyStopIsInactive信號從未被檢測到。

QEventLoop loop; 
connect(this, SIGNAL(EmergencyStopIsInactive()), &loop, SLOT(quit())); 
loop.exec(QEventLoop::AllEvents); 

qDebug() << "Emergency Stop Deactivated"; 

事實上,使用斷點我可以看到它永遠不會生成。 Eventloop似乎沒有收到信號。

如果我註釋掉loop.exec行,使用斷點,我可以看到代碼發出信號。隨着執行命令回來,我們永遠不會到達斷點。

exec()似乎不允許應用程序處理事件。

我可以按照我想要的方式獲得此功能嗎?怎麼樣?

問候, 詹姆斯

====================================== = 編輯: 這是產生初始信號的代碼:

// Set up Emergency Stop Input 
EmStop = new mita_gpio; 
EmStop->initgpio_read(49); 
connect(EmStop,SIGNAL(StateOutput(unsigned int)), this, SLOT(update_EmStop(unsigned  int))); 
connect(EmStop,SIGNAL(StateOutput(unsigned int)), Test_Screen, SLOT(update_EmStop(unsigned int))); 
connect(this,SIGNAL(EmergencyStopIsInactive()), Probe_Screen, SLOT(quit())); 
connect(Probe_Screen,SIGNAL(ShowEmergencyStopScreen()),this,SLOT(EmergencyStopScreenShow())); 

此信號然後鏈接到以下內容:

void Manager::update_EmStop(unsigned int state_value) 
{ 
    if (state_value == 1) 
    { 
     MitaData.EmergencyStop = 1; 
     emit EmergencyStopIsActive(); 
     qDebug() << "Emergency Stop = 1"; 
    } 
    else 
    { 
     MitaData.EmergencyStop = 0; 
     emit EmergencyStopIsInactive(); 
     qDebug() << "Emergency Stop = 0"; 
    } 
} 
+0

從哪裏發出?同一個線程?事件循環可能會阻止排放? –

+0

發佈發出信號的代碼。 – sashoalm

+0

您沒有更改'state_value'變量!它會一直進入同一個分支。 –

回答

1

SIGNAL(EmergencyStopIsInactive())在p的主事件循環正在執行程序(我邏輯上假設)。

當你開始一個新的事件循環時,你使用阻塞函數exec運行它,因此儘管你的新循環正在運行,但你正在阻塞主事件循環。

loop.exec(QEventLoop::AllEvents); 

由於您的信號應該是從主循環發送,由您的閉鎖功能loop.exec受阻,它永遠不會被直到loop.exec將返回發送。


爲了解決這個問題,無論是從「環」事件循環內產生所述EmergencyStopIsInactive()信號,或把這個第二環路一個單獨的線程內。