簡體   English   中英

超時后如何關閉並退出與exec()一起顯示的QDialog?

[英]How to close and exit a QDialog shown with exec() after a timeout?

我正在嘗試使用QTimer中的超時來關閉QDialog。

到目前為止,我已經嘗試這樣做:

QDialog dlg;
.. 
..
myTimer.start(60000); // 60 s
connect(&myTimer, SIGNAL(timeout()),
        &dlg, SLOT(close())));

dlg.exec();
qWarning() << "---timer expired or key pressed--";

但是當觸發超時並執行close插槽時,不會退出事件循環。 reject槽的行為相同。 我知道done插槽應該具有預期的行為,但是由於它需要一個額外的參數( int r ),因此它不能直接連接到timeout()信號。

當然,我可以“中繼” timeout信號以提供缺少的參數,但是還有另一種更直接的方法嗎?

謝謝。

dlg.exec(); 是共時的,他返回接受或拒絕的答案。

void MainWindow::btnClicked() {
    Dialog *dialog = new Dialog();
    dialog.exec();
    qDebug() << "test"; 
    // while dialog not destroyed (rejected, accepted) Print will not happen never. 
}

在Dialog類中使用QTimer的一種方法:

Dialog::dialog(...) {
    //constructor
    QTimer::singleShot(60000, this, SLOT(close()));
}

或者不使用dialog.exec(); 使用dialog.show(); 如果您想讓對話框成為模態,可以使用:

void MainWindow::btnClicked() {
    Dialog *dialog = new Dialog();
    dialog->setModal(true);
    dialog->show();
    qDebug() << "test"; //The "test" will be printed, and you can use QTimer :))
 }

我建議給對話框自己的計時器(即在執行對話框之前在本地實例化QTimer):

QTimer dlg_timer;
dlg_timer.start(60000); // 60 s
connect(&dlg_timer, SIGNAL(timeout()), &dlg, SLOT(close()));
dlg.exec();
dlg_timer.stop();

正如OP在其評論中所擔心的那樣,如果在與對話框關閉連接之前將計時器超時信號連接到其他某個插槽,並在該插槽中調用QTimer::disconnect() ,則將永遠不會調用對話框關閉插槽。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM