2013-01-21 33 views
0

我現在將GUI添加到爲控制檯操作編寫的原始項目中。我選擇了Qt作爲框架,現在面臨處理QProgressDialog關閉事件的困難。關閉/中止QProgressDialog問題

問題1:我使用QtConcurrent :: run爲長/繁重任務分叉進程,並等待'QProgressDialog'(範圍爲0,0)以提示用戶長時間運行的進程。問題是我不能讓對話框自己關閉!

void MainWindow::doLongRunProcess() { 
    pDialog = new QProgressDialog("Loading 2 ...", "Abort", 0, 0, this); 
    pDialog->setWindowModality(Qt::WindowModal); 
    pDialog->show(); 
    QFuture<void> future = QtConcurrent::run(theApp, &SimApplication::runSimulation); 
    QFutureWatcher<void> watcher; 
    connect(&watcher, 
     SIGNAL(finished()), 
     this, 
     SLOT(endLongRunProcess())); 
    watcher.setFuture(future); 
    // at this point, the runSimulation is successfully invoked 
} 

void MainWindow::endLongRunProcess() 
{ 
    // no sign of being invoked! 
    if (pDialog) 
    { 
     pDialog->close(); 
     delete pDialog; 
    } 
    logMessage("Operation completed"); 
} 

要求1:如果可能,不要觸摸/更改原包裝的代碼。

問題2:如何鏈接「中止」按鈕來終止SimApplication :: runSimulation()?

+0

什麼是watcher.isFinished();回報?您可以添加公共插槽以打印watcher.isFinished()的狀態;以確認它實際上完成... –

+0

@IlyaKobelevskiy,在runSimulation()調用,我有調試打印輸出顯示調用結束。對於watcher.isFinished(),如何設置插槽? –

+0

在堆棧上創建觀察器會在構造函數結束時立即銷燬它。嘗試在堆上創建它/使其成爲類成員。 –

回答

0

嘗試在創建對話框後在對話框中調用setAttribute(Qt::WA_DeleteOnClose, true),並將finished()附加到對話框的close()插槽而不是插槽。對話框會在QObject::deleteLater()可能的情況下自行刪除。