QT/C++ - 覆盖方法的衍生自

问题描述:

呼叫我有以下代码: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

有一种更容易的方式来实现你想要做什么,而不必使用信号和槽,并从插槽中调用基类的功能。

可以直接从您的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,这使得创建简单的消息框变得非常容易。