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
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.
AutoConnection
, DirectConnection
, QueuedConnection
, BlockingQueuedConnection
(+ optional UniqueConnection
). Read the manual for details. :)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++;
}
}
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
}