Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QMainWindow - closeEvent (saving data on close)

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

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 4.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maximus
    wrote on last edited by
    #1

    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)));
    

    }@


    Free Indoor Cycling Software - https://maximumtrainer.com

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on last edited by
      #2

      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)
      @

      1 Reply Last reply
      0
      • M Offline
        M Offline
        maximus
        wrote on last edited by
        #3

        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";
        

        }@


        Free Indoor Cycling Software - https://maximumtrainer.com

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved