简体   繁体   中英

Qt how to capture event indicating that all gui elements are ready

I'm wondering if it is possible to capture an event that is generated when all Qt objects are initialized and ready ?

It seems that some things can't be done in window's constructor. And they work fine in slot implementation.

For example, when I want to access root window of my application I do it like that

// in *.h
MainWindow* rootWindow

// in *.cpp
rootWindow = qobject_cast<MainWindow *>(this->window());

If it is done in the contructor I can't use rootWindow object - it couses runtime error.

There is no relevant slot to implement. And create event in QMainWindow class is not virtual.

Thanks for help :)

You can use a single-shot timer for this. In your main window class, define a slot function called say, appReady() . In the constructor, create and connect a single shot timer using QTimer::singleShot(0, this, SLOT(appReady())); This timer should fire as soon as the event loop is up and running and there are no more startup events pending.

How can you be sure that root window is in fact MainWindow? Later in the project lifetime you could feed your widget to a different parent (for example a few layers of QFrame decorations for layouting purposes) and this code will fail.

Pass it in as an explicit parameter in the constructor instead.

Unless it is MainWindows all the way down :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM