45

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 function (a dialog actually) get's called before the window is shown.

1
  • Call QMainWindow::show() and then QMetaObject::invokeMethod() and do whatever you also want to do. Commented Dec 18, 2015 at 9:13

9 Answers 9

42

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
} 
6
  • 3
    I already thought about implementing my own show function, but it never occured to me that I could call the parent's show function BEFORE my own code.*facepalm*
    – HWende
    Commented Jan 16, 2013 at 20:05
  • 1
    Actually, in this case, it doesn't make difference where you call QWidget::showEvent(), because QWidget::showEvent()'s implementation is empty and does nothing. (That's of course a implementation detail one shouldn't rely upon). Commented Jan 17, 2013 at 8:37
  • 7
    I went four your suggested implementation but the dialog still get's shown BEFORE the window is displayed.
    – HWende
    Commented Jan 17, 2013 at 10:32
  • 2
    When my QMainWindow inheriting class get's show it should display a dialog asking something. But I can't get the dialog to show AFTER the MainWindow is shown on screen.
    – HWende
    Commented Jan 17, 2013 at 10:54
  • 3
    If you use exec(), that's because exec() uses a local event loop, which is the root of all evil in Qt. You could open it asynchronously instead of synchronously, using QDialog::open instead of exec() Commented Jan 17, 2013 at 13:25
18

After analyzing the solutions above, it turns that all of them, including the heavily upvoted ones, are faulty.

Many recommend something like this:

class MyWidget : public QWidget {
    // ...
};

void MyWidget::showEvent(QShowEvent* event) {
    QWidget::showEvent(event);
    DoSomething();
}

void MyWidget::DoSomething() {
    // ...
}

This works as long as there is no QCoreApplication::processEvents(); in DoSomething. If there is one, it processes all events in the queue, including the QShowEvent which called MyWidget::showEvent in the first place. When it gets to the original QShowEvent, it calls MyWidget::showEvent again, causing an infinite loop.

If this happens, there are three solutions:

Solution 1. Avoid calling processEvents in MyWidget::DoSomething, instead call update or repaint when necessary. If DoSomething calls something else, these functions should avoid processEvents also.

Solution 2. Make DoSomething a slot, and replace direct call to DoSomething() by

QTimer::singleShot(0, this, SLOT(DoSomething()));

Since zero interval timer fires only when after all events in the queue are processed, it will process all events, including the original QShowEvent, remove them from the queue, and only then call DoSomething. I like it the most.

Since only zero interval timer fires only when after all events in the queue are processed, you should not try to "improve" it by lengthening the interval, for instance

QTimer::singleShot(50, this, SLOT(DoSomething())); // WRONG!

Since 50 ms is usually enough time for processing events in the queue, that would usually work, causing an error which is hard to reproduce.

Solution 3. Make a flag which prevents calling DoSomething the second time:

class MyWidget : public QWidget {
    // ...
};

void MyWidget::showEvent(QShowEvent* event) {
    if (is_opening)
        return;
    is_opening = true;
    QWidget::showEvent(event);
    DoSomething();
    is_opening = false;
}

void MyWidget::DoSomething() {
    // ...
}

Here, is_opening is a boolean flag which should be initialized as false in constructor.

15

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
}
2
  • Parentheses are missing, it should be connect(this, SIGNAL(window_loaded()), this, SLOT(your_function()));
    – vsz
    Commented Jan 12, 2017 at 13:40
  • 3
    Amazing but funny at the same time, why to use this extra signal and slot. Invoke the function (for instance doWork()) directly inside the void MainWindow::showEvent().
    – A.B.
    Commented Dec 20, 2017 at 17:58
13

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. :)
5

Assuming you want to run your code in the UI thread of the window after the window has been shown you could use the following relatively compact code.

class MainWindow : public QMainWindow
{
        // constructors etc omitted.

protected:
    void showEvent(QShowEvent *ev)
    {
        QMainWindow::showEvent(ev);
        // Call slot via queued connection so it's called from the UI thread after this method has returned and the window has been shown
        QMetaObject::invokeMethod(this, "afterWindowShown", Qt::ConnectionType::QueuedConnection);
    }

private slots:
    void afterWindowShown()
    {
        // your code here
        // note this code will also be called every time the window is restored from a minimized state
    }
};

It does invoke afterWindowShown by name but that sort of thing is fairly common practice in Qt. There are ways of avoiding this but they're a bit more verbose.

Note that this code should work for any QWidget derived class, not just QMainWindow derived classes.

In theory it might be possible for a very quick user to invoke some sort of action on the UI of the displayed window before afterWindowShown can be called but it seems unlikely. Something to bear in mind and code defensively against perhaps.

3
  • 2
    I love this solution, thank you. By the way, in current Qt version you can write QMetaObject::invokeMethod(this, &MainWindow::afterWindowShown, Qt::ConnectionType::QueuedConnection); and you get rid of the string function name.
    – Youda008
    Commented Apr 16, 2020 at 19:23
  • I wonder how this will differ from the QTimer::singleShot( 0, ... ); method
    – Youda008
    Commented Apr 16, 2020 at 19:26
  • I added a boolean guard so the action only happens first time. This gives a reasonably Delphi-like OnShow event.
    – Bill99
    Commented Jan 10, 2022 at 21:40
2

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 <time.h>
#include <iosteream>

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 <QtWidgets/QMainWindow>
#include <qtimer.h>
#include <time.h>
#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
1

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;
}
1
  • Thanks. This is the only solution that worked properly for me (first, display a "please wait" window then start a long task which regularly calls QApplication::processEvents() to avoid freezing the UI). showEvent, queued signals and immediate timers (0 ms) were no good: too early in the events stack I suspect, all I got at best was garbage on the screen. And obviously any solution based on arbitrary timers (eg. 50 ms) are highly dependent on the target system's speed, which means it's not reliable.
    – syam
    Commented Apr 6, 2018 at 13:55
1

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

Reimplement method void show() like this:

void MainWindow::show()
{
    QMainWindow::show();
    // Call your special function here.
}
1
  • 6
    Just a friendly reminder for anyone around: QWidget::show() is not virtual, and therefore attempting to call such "reimplemented" method on an upcast pointer will inevitably fail.
    – user35443
    Commented Jun 28, 2017 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.