How to call function after window is shown?

前端 未结 9 1224
余生分开走
余生分开走 2020-12-05 06:21

Using Qt I create a QMainWindow and want to call a function AFTER the windows is shown. When I call the function in the constructor the fun

9条回答
  •  余生分开走
    2020-12-05 07:01

    try this:

    in mainwindow.h:

    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();  
    
    protected:
          void showEvent(QShowEvent *ev);
    
    private:
          void showEventHelper();
          Ui::MainWindow *ui;
    }
    

    in mainwindow.cpp:

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    void MainWindow::showEvent(QShowEvent *ev)
    {
        QMainWindow::showEvent(ev);
        showEventHelper();
    }
    
    void MainWindow::showEventHelper()
    {
        // your code placed here
    }
    

提交回复
热议问题