How to call function after window is shown?

前端 未结 9 1215
余生分开走
余生分开走 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条回答
  •  旧时难觅i
    2020-12-05 07:09

    I found a nice answer in this question which works well, even if you use a Sleep() function.

    So tried this:

    //- cpp-file ----------------------------------------
    
    #include "myapp.h"
    #include 
    #include 
    
    MyApp::MyApp(QWidget *parent)
        : QMainWindow(parent, Qt::FramelessWindowHint)
    {
        ui.setupUi(this);
    }
    
    MyApp::~MyApp()
    {
    
    }
    
    void MyApp::showEvent(QShowEvent *event) {
        QMainWindow::showEvent(event);
        QTimer::singleShot(50, this, SLOT(window_shown()));
        return;
    }
    
    void MyApp::window_shown() {
        std::cout << "Running" << std::endl;
        Sleep(10000);
        std::cout << "Delayed" << std::endl;
        return;
    }
    
    //- h-file ----------------------------------------
    
    #ifndef MYAPP_H
    #define MYAPP_H
    
    #include 
    #include 
    #include 
    #include "ui_myapp.h"
    
    
    class MyApp : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MyApp(QWidget *parent = 0);
        ~MyApp();
    
    protected:
        void showEvent(QShowEvent *event);
    
    
    private slots:
        void window_shown();
    
    private:
        Ui::MyAppClass ui;
    };
    
    #endif // MYAPP_H
    

提交回复
热议问题