How to call function after window is shown?

前端 未结 9 1212
余生分开走
余生分开走 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:12

    I solved it without a timer using Paint event. Works for me at least on Windows.

    // MainWindow.h
    class MainWindow : public QMainWindow
    {
        ...
        bool event(QEvent *event) override;
        void functionAfterShown();
        ...
        bool functionAfterShownCalled = false;
        ...
    }
    
    // MainWindow.cpp
    bool MainWindow::event(QEvent *event)
    {
        const bool ret_val = QMainWindow::event(event);
        if(!functionAfterShownCalled && event->type() == QEvent::Paint)
        {
            functionAfterShown();
            functionAfterShownCalled = true;
        }
        return ret_val;
    }
    

提交回复
热议问题