Qt:如何初始化对话框小部件?
我想知道在 Qt 自定义对话框中初始化控件的既定程序是什么。在我编写的代码中,对话框将呈现一个 QListView,其中包含在构造期间传递(通过引用)到对话框类的对象的目录。当显示对话框时,我显然希望列表显示对象中当前配置的目录。
但这应该在哪里完成?也许在重写的 showEvent()
方法中?
背景:我以前做过很多 MFC 编程,并且会在 OnCreate
方法中完成此类操作,或者一些类似的操作,一旦窗口对象已创建。
I would like to know what the established procedure is for initializing the controls within a Qt custom dialog box. In the code I am writing, the dialog would present a QListView
containing directories from an object passed (by reference) to the dialog class during construction. When the dialog is displayed, I obviously want the list to display the directories currently configured in the object.
Where should this be done though? Perhaps in the overridden showEvent()
method?
Background: I used to do a lot of MFC programming back in the day, and would have done this sort of stuff in the OnCreate
method, or some such, once the window object had been created.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
值得庆幸的是,Qt 不需要您进行任何挂钩来找到创建事物的时机(除非您愿意)。如果您查看 Qt 对话框示例,大多数都是在构造函数中完成所有构造:
http://doc.qt.io/archives/qt-4.7/examples-dialogs.html
选项卡对话框示例(例如)不执行“按需”初始化选项卡。尽管您可以通过
currentChanged
信号连接某些内容:http://doc.qt.io/archives/qt-4.7/qtabwidget.html#currentChanged
向导式对话框具有
initializePage
和cleanupPage
方法:http://doc.qt .io/archives/qt-4.7/qwizardpage.html#initializePage
http://doc.qt.io/archives/qt-4.7/qwizardpage.html #cleanupPage
但总的来说,您可以只使用构造函数。我想主要的例外是,如果发现自己在实际显示对话框(通过 exec )之前分配对话框,并且您不想承担某些部分的性能负担直到它实际显示为止。这种情况应该很少见,最简单的方法可能就是添加您自己调用的函数(例如
finalizeCreationBeforeExec
)。Thankfully Qt doesn't require you to do any hooking to find the moment to create things (unless you want to). If you look over the Qt examples for dialogs, most do all the constructing in the constructor:
http://doc.qt.io/archives/qt-4.7/examples-dialogs.html
The tab dialog example--for instance--doesn't do "on-demand" initializing of tabs. Although you could wire something up via the
currentChanged
signal:http://doc.qt.io/archives/qt-4.7/qtabwidget.html#currentChanged
Wizard-style dialogs have
initializePage
andcleanupPage
methods:http://doc.qt.io/archives/qt-4.7/qwizardpage.html#initializePage
http://doc.qt.io/archives/qt-4.7/qwizardpage.html#cleanupPage
But by and large, you can just use the constructor. I guess the main exception would be if find yourself allocating the dialog at a much earlier time from when you actually display it (via
exec
), and you don't want to bear the performance burden for some part of that until it's actually shown. Such cases should be rare and probably the easiest thing to do is just add your own function that you call (likefinalizeCreationBeforeExec
).