2017-06-16 24 views
2

私は次のコードを持っている:私は、基本クラスのメソッドcloseEventを呼び出すしたいメインウィンドウのQt/C++ - 派生クラスからオーバーライドメソッドの呼び出し

のAppMPhase派生クラスで

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

を"restoreInitialConfig"メソッドのMainWindowを使用して、設定を復元した後でウィンドウを閉じます。 可能ですか? closeEventメソッドがAppMPhaseクラスでオーバーライドされているため、私が見た他の質問とは異なります。

AppMPhaseクラス:

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; 

メイン・ウィンドウクラス:事前に

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

感謝。

Floのあなたはシグナルとスロットを使用せずに、やりたいと思っているものを達成し、あなたのスロットから基底クラスの関数を呼び出すためにはるかに簡単な方法があります

+0

C++で「スーパー」と同等の機能を使用する方法を見てみたいかもしれません。詳細は、このディスカッションを参照してください。 – Xatyrian

+1

'closeEvent()'関数を全く呼び出す必要はありません。あなたが 'QMainWindow'クラスを継承しているときに、' close() 'スロットを呼び出して閉じることができます。したがって、あなたのコードで 'MainWindow :: closeEvent(closeEvent);'の代わりに 'close();'だけを呼び出してください。 – vahancho

+0

問題を再現するコードの最小バージョンである[MCVE]が好きです。これにより、あなたの質問は簡単に答えることができ、同じ質問をした他の人にも役立ちます。 – m7913d

答えて

0

closeEventハンドラ内から直接復元することは可能です。

QMessageBox::execは、どのボタンが押されたかに応じて、StandardButton enumの値の1つに一致する整数コードを返します。

これにより、どのボタンが押されたかを知っているので、closeEventハンドラから直接restoreInitialConfigに電話することができます。

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

ボタンのテキストをチェックする必要がないとして、これはまた、あなたのrestoreInitialConfig機能を簡素化することに注意してください、あなたは答えはイエスだった知っています。

また、私はthis QMessageBox constructorを使用しているため、簡単なメッセージボックスの作成が非常に簡単です。

関連する問題