2017-06-16 83 views
2

呼叫我有以下代碼:QT/C++ - 覆蓋方法的衍生自

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

與AppMPhase派生類主窗口

我想調用基類的方法的closeEvent MainWindow在「restoreInitialConfig」方法中,在我們恢復配置後關閉窗口。 這可能嗎?它與我見過的其他問題不同,因爲方法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; 

MainWindow類:

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

在此先感謝。

弗洛

+0

你可能想看看如何在C++中使用「超級」的等價物。有關更多信息,請參閱此討論。 – Xatyrian

+1

根本不需要調用'closeEvent()'函數。當你繼承'QMainWindow'類時,你可以調用它的'close()'插槽來關閉它。因此,不要在代碼中使用'MainWindow :: closeEvent(closeEvent)',只需調用'close();'。 – vahancho

+0

請注意,我們喜歡[MCVE],即您的代碼的最小版本,它可以再現您的問題。這使得您的問題更容易回答,並且對於具有相同問題的其他人更有用。 – m7913d

回答

0

有一種更容易的方式來實現你想要做什麼,而不必使用信號和槽,並從插槽中調用基類的功能。

可以直接從您的closeEvent處理程序中進行修復。

由於QMessageBox::exec返回一個整數代碼,它與StandardButton enum中的一個值相匹配,這取決於按下了哪個按鈕。

然後,您可以直接從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,這使得創建簡單的消息框變得非常容易。