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. Dialog closeEvent is never called

Dialog closeEvent is never called

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 348 Views
  • 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.
  • S Offline
    S Offline
    sandro4912
    wrote on last edited by sandro4912
    #1

    I have a Dialog which overrides closeEvent because on close i want to safe some settints. However it is never called the close Event.

    #ifndef WINDIALOG_H
    #define WINDIALOG_H
    
    #include "gamesettings.h"
    
    #include <QDialog>
    
    namespace Ui {
    class WinDialog;
    }
    
    class WinDialog : public QDialog
    {
        Q_OBJECT
    public:
        WinDialog(int recordTime, QWidget *parent = nullptr);
        ~WinDialog() override;
    
    protected:
        void closeEvent(QCloseEvent *event) override;
    
    private:
        GameSettings mGameSettings;
        int mRecordTime;
        Ui::WinDialog *ui;   
    };
    
    #endif // WINDIALOG_H
    
    void WinDialog::closeEvent(QCloseEvent *event)
    {
        Q_UNUSED(event)
    
        switch(mGameSettings.difficulty()){
        case GameSettings::Difficulty::beginner:
            mGameSettings.setBeginnerTime(mRecordTime);
            mGameSettings.setBeginnerName(ui->playerNameLineEdit->text());
            break;
        case GameSettings::Difficulty::intermediate:
            mGameSettings.setIntermediateTime(mRecordTime);
            mGameSettings.setIntermediateName(ui->playerNameLineEdit->text());
            break;
        case GameSettings::Difficulty::expert:
            mGameSettings.setExpertTime(mRecordTime);
            mGameSettings.setExpertName(ui->playerNameLineEdit->text());
            break;
        case GameSettings::Difficulty::custom:
            break;
        }
    }
    

    The Dialog is called inside a class derived from QWidget like this:

            auto dialog = new WinDialog{ recordTime, this };
            dialog->setAttribute(Qt::WA_DeleteOnClose);
    
            dialog->show();
            dialog->exec();
    

    I thought closeEvent allways gets called?

    1 Reply Last reply
    0
    • mranger90M Offline
      mranger90M Offline
      mranger90
      wrote on last edited by
      #2

      Why are you calling show() and exec() ? It should be one or the other depending on whether you are modal or not.

      1 Reply Last reply
      1
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by Chris Kawa
        #3

        Close event happens when you hit the close button. You can connect to the finished() signal instead.

        As @mranger90 said show() is called by exec() so that's not needed. Also don't forget to check the return value of exec() and, since exec() is blocking anyway, it's more idiomatic to create the dialog on the stack instead of heap + WA_DeleteOnClose.

        1 Reply Last reply
        2
        • S Offline
          S Offline
          sandro4912
          wrote on last edited by sandro4912
          #4
          This post is deleted!
          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