2

I have the following code:

void AppMPhase::closeEvent(QCloseEvent *closeEvent) {
    QMessageBox* dialog = new QMessageBox(this);
    dialog->setText("Warning: Initial configuration changed\nDo you want to restore it ?");
    dialog->setIcon(QMessageBox::Warning);
    dialog->addButton(QMessageBox::Yes);
    dialog->addButton(QMessageBox::No);
    connect(dialog, SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(restoreInitialConfig(QAbstractButton*)));
    dialog->exec();
}


void AppMPhase::restoreInitialConfig(QAbstractButton *button) {
    if(!button->text().compare(QString("Yes"))) {
        // restore Config
    }
    else {
        // don't restore
    }
    MainWindow::closeEvent(closeEvent);
}

with AppMPhase the derived class of MainWindow

I would like to call the method closeEvent of the base class MainWindow in the "restoreInitialConfig" method, to close the window after we restore the config. Is it possible ? it's different from the other questions I have seen because the method closeEvent is overriden in the AppMPhase class.

the AppMPhase class:

class AppMPhase : public QtUi::MainWindow
        {
            Q_OBJECT

        public:
            AppMPhase(QWidget *const parent = Q_NULLPTR, const Qt::WindowFlags flags = 0);
            ~AppMPhase();
            int readConfigFromExternFile(QString path);
            int readCalibrationDate(QString path);

            QSize sizeHint() const Q_DECL_OVERRIDE;
            QtWrapperTestManager * testManager;

        public Q_SLOTS:
            void show();

        public slots:
            void currentTestFinished();
            void createTest(unsigned int,QString);
            void restoreInitialConfig(QAbstractButton* button);

        signals:
            void notifyPageTestCurrentTestFinished();

        private:
            enum AppPage
            {
                PAGE_START,
                PAGE_ABOUT
            };

            bool isTestMangaerCreated;
            std::map<QString, std::map<std::string, std::string> > conversion_Table_Cable_Ref_sensorParamMap;


            Pages::GenericAppPage * appPage(const int index) const;
            QToolButton    * appPageButton(const int index) const;
            virtual void closeEvent(QCloseEvent *closeEvent) Q_DECL_OVERRIDE;

The MainWindow class:

class SHARED_EXPORT_LIB_QT_UI MainWindow : public QMainWindow
        {
            Q_OBJECT

        signals:
            void aboutToClose();

        public slots:
            virtual void show();

        private slots:
            void changeLanguage(const QString &language);

        public:
            MainWindow(QWidget *const parent = Q_NULLPTR, const Qt::WindowFlags flags = 0);
            virtual ~MainWindow();

        protected:
            void showCopyright(const QString &copyRightHeader);
            void showLanguageSelector(const QStringList &languages);
            void setupUi(const MainPageList &pageList);
            void setupUi(QWidget *centerWidget = Q_NULLPTR);
            virtual void closeEvent(QCloseEvent *closeEvent) Q_DECL_OVERRIDE;

            const Ui::MainWindow *const _ui;
            MainPageItemList            _mainPageItemList;
        };

Thanks in advance.

Flo

flog
  • 21
  • 1
  • You might want to look how you could use an equivalent of "super" in C++. See this disccussion for further informations. – Xatyrian Jun 16 '17 at 09:02
  • 1
    You don't have to call `closeEvent()` function at all. As you inherit `QMainWindow` class, you can just call its `close()` slot to close it. So, instead of `MainWindow::closeEvent(closeEvent);` in your code, just call `close();`. – vahancho Jun 16 '17 at 09:06
  • Note that we like a [MCVE], i.e. a minimal version of your code which reproduces your problem. This makes your question easier to answer and more useful for others with the same question. – m7913d Jun 16 '17 at 12:05

1 Answers1

1

There is a far easier way to achieve what you're wanting to do, without having to use signals and slots, and call base class functions from your slot.

It is possible to do the restoration directly from within your closeEvent handler.

This is made possible by the fact that QMessageBox::exec returns an integer code which matches one of the values in the StandardButton enum, depending on what button was pressed.

That then allows you to call restoreInitialConfig directly from within your closeEvent handler, as you know which button was pressed.

void AppMPhase::closeEvent(QCloseEvent* ev) 
{
    int res = QMessageBox(
        QMessageBox::Icon::Warning,
        "Restore configuration?",
        "Warning: Initial configuration changed\nDo you want to restore it?",
        QMessageBox::Yes | QMessageBox::No,
        this).exec();

    if (res == QMessageBox::Yes)
        restoreInitialConfig();

    MainWindow::closeEvent(ev);
}

Note that this also simplifies your restoreInitialConfig function, as there is no need to check button text, you know the answer was yes.

Note also I made use of this QMessageBox constructor which makes it very easy to create simple message boxes.

Steve Lorimer
  • 27,059
  • 17
  • 118
  • 213