2013-01-21 34 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()可能的情况下自行删除。