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