2.5动态对话框(Dynamic Dialogs)
动态对话框是在程序运行时用Qt Designer的.ui文件创建。不用uic工具把.ui文件变成等价的c++代码,而是在程序时使用类QUiLoader加载.ui文件,例如下面的代码:
QUiLoader uiLoader;
QFile file("sortdialog.ui");
QWidget *sortDialog = uiLoader.load(&file);
if (sortDialog) {
...
}
子控件可以用QObject::findChild<T>()得到
QComboBox *primaryColumnCombo =
sortDialog->findChild<QComboBox *>("primaryColumnCombo");
if (primaryColumnCombo) {
...
}
findChild<T>()是模板成员函数,得到类型为T的给定名字的子控件的指针。由于编译器的原因,用MSVC6是得不到的。如果使用的是MSVC6,那么可以使用全局函数qFindChild<T>()。
QUiLoader类在一个单独的链接库中,如果在一个应用程序中使用了QUiLoader,必须在这个程序的.pro文件中添加下面这样的代码:
CONFIG += uitools
使用动态对话框不用重新编译程序就能够改变对话框的布局。它们可以用来创建“细客户”的程序,只有两个内建的对话框,其他的对话框都是按照不同需要创建的。(这段的翻译有点直,原文如下:Dynamic dialogs make it possible to change the layout of a form without recompiling the application. They can also be used to create thin-client applications, where the executable merely has a front-end form built-in and all other forms are created as required.)
本文介绍如何使用Qt的QUiLoader在程序运行时加载.ui文件创建动态对话框,避免重复编译并灵活调整布局。通过实例展示了如何获取子控件及配置项。
&spm=1001.2101.3001.5002&articleId=1514508&d=1&t=3&u=ef17df032fe448b4944c5cc1f44f8772)
1011

被折叠的 条评论
为什么被折叠?



