5
votes

A QMainwindow object is constructed and show()n in the program's main() function. This object's constructur is used to create all GUI widgets. It contains additional code (or a method call) that is currently run before the QMainWindow widget is visible.

This code / method should be run once after the QMainWindow constructor, i.e. when the application window is visible.

According the the documentation of showEvent, it may be run more than once.

Do I need to use some sort of toggle flag inside this event, or is there a "better" solution (I thought I read that a QTimer could be used to enqueue methods to the event loop)?

1
@vahancho He wants to run the code after the mainwindow is shown, ie queued function call. - thuga
"...after the QMainWindow constructor, i.e. when the application window is visible". Those are two different things. Which is it? - D Drmmr
@DDrmmr The application window is only visible after the constructor has finished and the show() method is called, so it may be redundant, but it's only one "thing" :-) - handle

1 Answers

5
votes

You can try using a Qt singleshot timer with timeout of 0 seconds. Call this at the end of your mainwindow constructor connecting your callback function as the slot:

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