How to call function after window is shown?

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

    Follow Reza Ebrahimi's example, but keep this in mind:

    Do not omit the 5th parameter of connect() function which specifies the connection type; make sure it to be QueuedConnection.

    I.E.,

    connect(this, SIGNAL(window_loaded), this, SLOT(your_function()), Qt::ConnectionType(Qt::QueuedConnection | Qt::UniqueConnection));
    

    I believe that you'd achieve what you need if you do it this way.

    • There are several types in signal-slot connections: AutoConnection, DirectConnection, QueuedConnection, BlockingQueuedConnection (+ optional UniqueConnection). Read the manual for details. :)
    0 讨论(0)
  • 2020-12-05 07:23

    The best solution for me is count once paint event:

    .H

    public:
    void paintEvent(QPaintEvent *event);
    

    .CPP

    #include "qpainter.h"
    #include <QMessageBox> // example
    
    int contPaintEvent= 0;
    
    void Form2::paintEvent(QPaintEvent* event)
    {
      if (contPaintEvent ==0 )
      {
      QPainter painter(this); 
      QMessageBox::information(this, "title", "1 event paint"); // example
      // actions
      contPaintEvent++;
      }
    }
    
    0 讨论(0)
  • 2020-12-05 07:24

    If you want to do something while the widget is made visible, you can override QWidget::showEvent like this:

    class YourWidget : public QWidget { ...
    
    void YourWidget::showEvent( QShowEvent* event ) {
        QWidget::showEvent( event );
        //your code here
    } 
    
    0 讨论(0)
提交回复
热议问题