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

最后都变了- 提交于 2020-01-21 06:54:30

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/5465647/qt-how-to-capture-event-indicating-that-all-gui-elements-are-ready

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!