Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a class method after its constructor (Qt GUI)?

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)?

like image 221
handle Avatar asked Nov 05 '14 13:11

handle


1 Answers

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() ) );                    
like image 164
mira Avatar answered Nov 07 '22 07:11

mira