2010-08-10 70 views
1

問候所有,如何找到活動的QMainWindow實例?

在我的QT應用程序中,我有幾個QMainWindow實例。 我跟蹤應用程序上下文對象中打開的QWindow對象;

在一個點上,當點擊菜單項時,我想通過所有這些QWindows並檢查哪個窗口處於活動狀態,並執行一些操作。

請參考下面的代碼片段:

//Just a confirmation dialog 
QMessageBox msgBox(this->getMainWindow()); 
msgBox.setText("This will discard existing project"); 
msgBox.setInformativeText("All Contour data and Volume data will be discarded"); 
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel); 
msgBox.setDefaultButton(QMessageBox::Ok); 
if(msgBox.exec()==QMessageBox::Cancel) 
{ 
return 
} 


//This is where I check the active window. 

QList<MainWindow*> lst= applicationContext.getOpendedWindows(); 

for(i=0;i<lst.size();i++) 
{ 
    MainWindow *win=lst.value(i); 

    //Check if this is the active window 
    if(win.isActive()){ // tried (win==QApplication::activeWindow()) also 
     //DO SOMETHING with 'win' object 
    } 
    } 
} 

此檢查工作得很好,當我不顯示前檢查確認對話框。 也就是說,當我註釋掉'msgBox.exe()'這一行時,我可以從列表中找到一個活動窗口。

QMessageBox的顯示是否改變焦點窗口?

對此有任何提示?

由於提前,

umanga

+0

你知道你已經發布了兩次相同的問題嗎? http://stackoverflow.com/questions/3447179/how-to-find-active-qmainwindow-instance和http://stackoverflow.com/questions/3447165/how-to-find-active-qmainwindow-instance – 2010-08-10 08:43:40

+0

對不起那我刪除了重複 – 2010-08-10 08:56:10

回答

1

如果你有多個主窗口,我想你也有多個菜單?如果是這樣,我會將對菜單動作的插槽與主窗口相關聯,使其成爲主窗口的插槽或知道對應主窗口的對象的插槽。

您也可以在消息框之前識別活動窗口,就像Greg所建議的那樣,但我也不會考慮最佳。

1

您的問題,我能想到的之前競選活動窗口檢查最簡單的辦法告訴你的消息框。

是的,一旦您撥打msgBox.exec()QMessageBox將成爲活動窗口。