Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to save documents when X-Button is pressed
Qt 6.12 LTS is out! See what's new in the release blog

How to save documents when X-Button is pressed

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 3.0k Views 3 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
    Michelle02283
    wrote on last edited by
    #1

    Hello,
    my program should show a MessageBox with the question to save the progress. I tried to override the function closeEvent in my subclass from QWidget, but my programm closes without showing the QMessageBox.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Can you show your widget implementation ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 2 Replies Last reply
      0
      • SGaistS SGaist

        Hi,

        Can you show your widget implementation ?

        M Offline
        M Offline
        Michelle02283
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Can you show your widget implementation ?

          M Offline
          M Offline
          Michelle02283
          wrote on last edited by
          #4

          @SGaist 0_1520281643238_7b0293ab-4e8c-454c-b648-4bf433d9dd62-grafik.png

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            What is AddAvailableFoodView ?

            Note that pasting the code here between coding tags allows for easier reading and copy pasting to test which an image does not.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • mrdebugM Offline
              mrdebugM Offline
              mrdebug
              wrote on last edited by
              #6

              It is very easy!

              #ifndef MAINWINDOW_H
              #define MAINWINDOW_H
              
              #include <QMainWindow>
              #include "QCloseEvent"
              #include "QMessageBox"
              
              namespace Ui {
              class MainWindow;
              }
              
              class MainWindow : public QMainWindow
              {
                  Q_OBJECT
              
              public:
                  explicit MainWindow(QWidget *parent = 0);
                  ~MainWindow();
              
              protected:
                  void closeEvent(QCloseEvent *event) override;
              
              private:
                  Ui::MainWindow *ui;
              };
              
              #endif // MAINWINDOW_H
              
              #include "mainwindow.h"
              #include "ui_mainwindow.h"
              
              MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
                  ui->setupUi(this);
              }
              
              MainWindow::~MainWindow(){
                  delete ui;
              }
              
              void MainWindow::closeEvent(QCloseEvent *event) {
                  switch (QMessageBox(tr("Warning!"), tr("Are you sure you want to exit without save?"), QMessageBox::Warning, QMessageBox::Yes | QMessageBox::Default, QMessageBox::No, QMessageBox::Escape).exec()) {
                      case 3: {
                          event->accept();
                          break;
                      }
                      default: {
                          event->ignore();
                          break;
                      }
                  }
              }
              

              Need programmers to hire?
              www.labcsp.com
              www.denisgottardello.it
              GMT+1
              Skype: mrdebug

              M 2 Replies Last reply
              0
              • mrdebugM mrdebug

                It is very easy!

                #ifndef MAINWINDOW_H
                #define MAINWINDOW_H
                
                #include <QMainWindow>
                #include "QCloseEvent"
                #include "QMessageBox"
                
                namespace Ui {
                class MainWindow;
                }
                
                class MainWindow : public QMainWindow
                {
                    Q_OBJECT
                
                public:
                    explicit MainWindow(QWidget *parent = 0);
                    ~MainWindow();
                
                protected:
                    void closeEvent(QCloseEvent *event) override;
                
                private:
                    Ui::MainWindow *ui;
                };
                
                #endif // MAINWINDOW_H
                
                #include "mainwindow.h"
                #include "ui_mainwindow.h"
                
                MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
                    ui->setupUi(this);
                }
                
                MainWindow::~MainWindow(){
                    delete ui;
                }
                
                void MainWindow::closeEvent(QCloseEvent *event) {
                    switch (QMessageBox(tr("Warning!"), tr("Are you sure you want to exit without save?"), QMessageBox::Warning, QMessageBox::Yes | QMessageBox::Default, QMessageBox::No, QMessageBox::Escape).exec()) {
                        case 3: {
                            event->accept();
                            break;
                        }
                        default: {
                            event->ignore();
                            break;
                        }
                    }
                }
                
                M Offline
                M Offline
                Michelle02283
                wrote on last edited by
                #7

                @mrdebug But I have a subclass from QWidget. This function deosn“t show anything.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Michelle02283
                  wrote on last edited by
                  #8

                  void AddAvailableFoodView::closeEvent(QCloseEvent event)
                  {
                  QMessageBox
                  saveOrNot = new QMessageBox();
                  QPushButton* save = new QPushButton();
                  saveOrNot->addButton(save);

                  }

                  I want that my programm saves my progress, if I push the "save" button

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Michelle02283
                    wrote on last edited by
                    #9

                    class AddAvailableFoodView : public QWidget
                    {
                    Q_OBJECT
                    public:
                    explicit AddAvailableFoodView(ErrorModel* errorModel, QStackedWidget* stackedWidget, QStringList* widgets,
                    SearchModel* searchmodel, ManageModel* manageModel,
                    StackHistoryView* stackHistoryView, QWidget* parent = nullptr);
                    void createStartLayout(void);

                    protected:
                    virtual void closeEvent(QCloseEvent *event) override;

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      closeEvent comes from Widget.

                      Take a look at the Main Window Example it shows what you want to achieve.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • mrdebugM mrdebug

                        It is very easy!

                        #ifndef MAINWINDOW_H
                        #define MAINWINDOW_H
                        
                        #include <QMainWindow>
                        #include "QCloseEvent"
                        #include "QMessageBox"
                        
                        namespace Ui {
                        class MainWindow;
                        }
                        
                        class MainWindow : public QMainWindow
                        {
                            Q_OBJECT
                        
                        public:
                            explicit MainWindow(QWidget *parent = 0);
                            ~MainWindow();
                        
                        protected:
                            void closeEvent(QCloseEvent *event) override;
                        
                        private:
                            Ui::MainWindow *ui;
                        };
                        
                        #endif // MAINWINDOW_H
                        
                        #include "mainwindow.h"
                        #include "ui_mainwindow.h"
                        
                        MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
                            ui->setupUi(this);
                        }
                        
                        MainWindow::~MainWindow(){
                            delete ui;
                        }
                        
                        void MainWindow::closeEvent(QCloseEvent *event) {
                            switch (QMessageBox(tr("Warning!"), tr("Are you sure you want to exit without save?"), QMessageBox::Warning, QMessageBox::Yes | QMessageBox::Default, QMessageBox::No, QMessageBox::Escape).exec()) {
                                case 3: {
                                    event->accept();
                                    break;
                                }
                                default: {
                                    event->ignore();
                                    break;
                                }
                            }
                        }
                        
                        M Offline
                        M Offline
                        Michelle02283
                        wrote on last edited by
                        #11

                        @mrdebug why do you use the digit 3 in the switch?

                        mr ZXM 1 Reply Last reply
                        0
                        • M Michelle02283

                          @mrdebug why do you use the digit 3 in the switch?

                          mr ZXM Offline
                          mr ZXM Offline
                          mr ZX
                          wrote on last edited by
                          #12

                          @Michelle02283
                          use saving method in close event ,before accept event...
                          like this:

                          void MainWindow::closeEvent(QCloseEvent *event)
                          {
                              QMessageBox::StandardButton res=QMessageBox::question(this,
                                                                                    "title","exit?",
                                                                                    QMessageBox::Yes|QMessageBox::No);
                              if(res==QMessageBox::Yes)
                              {
                                  qDebug()<<"exit: Yes";
                                  SaveFunc();//saveig method
                                  event->accept();//exit
                              }
                              else
                              {
                                  event->ignore();
                              }
                          }
                          
                          void MainWindow::SaveFunc()
                          {
                              qDebug()<<"saving";
                              Sleep(3000);
                              qDebug()<<"ok saved";
                          }
                          

                          program after 3 second closing...

                          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
                          • Unsolved