[SOLVED] QMainWindow - closeEvent (saving data on close)



  • Hi,

    I have re-implemented the function closeEvent on my MainWindow.
    I want to save information over the network before closing my mainProgram.
    However, I never get a response from my QNetworkReply when using it inside the closeEvent() function.

    The function work if I bind it to a pushButton for example (the data is saved on the server)
    Is there a way to achieve what I want? I just want to do a put before closing my program so all the data is persisted, one put at the close would be the best way to minimize database access on my side, thanks!

    This is what i'm doing :

    @void MainWindow::closeEvent(QCloseEvent *event) {

    Q_UNUSED(event);
    saveSettings();
    
    ///To test if Put worked
    account->FTP = 666;
    
    replySaveAccount = UserDAO::putAccount(account);
    connect(replySaveAccount, SIGNAL(finished()), this, SLOT(slotPutAccountFinished()) );
    connect(replySaveAccount, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotPutAccountError(QNetworkReply::NetworkError)));
    
    
    while (!replySaveAccountReceived) {
        qDebug() << "waiting on reply confirmation to close program...";
        QThread::msleep(1000);
    }
    

    }
    //////////////////////////////////////////////////////////////////////////////////////////////
    void MainWindow::slotPutAccountFinished() {

    qDebug() << "slotPutAccountFinished";
    replySaveAccountReceived = true;
    

    }

    void MainWindow::slotPutAccountError(QNetworkReply::NetworkError) {

    qDebug() << "slotPutAccountError";
    replySaveAccountReceived = true;
    

    }
    @

    This one works

    @void MainWindow::on_pushButton_clicked()
    {

    account->FTP = 666;
    
    replySaveAccount = UserDAO::putAccount(account);
    connect(replySaveAccount, SIGNAL(finished()), this, SLOT(slotPutAccountFinished()) );
    connect(replySaveAccount, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotPutAccountError(QNetworkReply::NetworkError)));
    

    }@



  • You put an event loop to sleep, so Qt is not able to send any signals.
    @
    QThread::msleep(1000);
    @

    After the timer is expired MainWindow is closed and I guess is destroyed so there are no active slots to receive the signals.

    [EDIT] And your app may stuck in the loop
    @
    while (!replySaveAccountReceived)
    @



  • QEventLoop was the solution!

    @//////////////////////////////////////////////////////////////////////////////////////////////
    void MainWindow::closeEvent(QCloseEvent *event) {

    Q_UNUSED(event);
    saveSettings();
    
    ///To test if Put worked
    account->FTP = 666;
    
    
    QEventLoop loop;
    replySaveAccount = UserDAO::putAccount(account);
    QObject::connect(replySaveAccount, SIGNAL(finished()), &loop, SLOT(quit()) );
    QObject::connect(replySaveAccount, SIGNAL(error(QNetworkReply::NetworkError)), &loop, SLOT(quit()));
    
    
    loop.exec&#40;&#41;;
    
    qDebug() << "loop done";
    

    }@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.