如何在 QtDesigner 中将小部件添加到表单中

pur*_*rga 3 qt qt4

在 qdesigner_workbench.cpp 中,如何通过代码将小部件(比如 QLabel)添加到 FormWindow 中?由于像 createWidget()...etc 这样的方法都是抽象的,我如何正确使用内部机制将 QLabel 添加到活动的 FormWindow 中?

编辑:

在 qdesigner_workbench.cpp 中,这是我目前拥有的:

QDesignerFormWindowManagerInterface* fwm = core()->formWindowManager();
QDesignerFormWindowInterface* fw = fwm->activeFormWindow();

QWidget* mw = fw->mainContainer(); 

QLabel* label = new QLabel(mw);         //can be added correctly but not in the right hierarchy
label->setText("I am a good girl.");
Run Code Online (Sandbox Code Playgroud)

mw(从 fw->mainContainer() 获得)实际上是一个 MainWindow,但是我需要的真实数据在:

mw -> children[2] (which is a QDesignerWidget) -> children
Run Code Online (Sandbox Code Playgroud)

设计器中有9个widget,你可以看到上面提到的children中有9个数组;有关说明,请参阅此链接(图像)。

http://img24.imagevenue.com/img.php?image=98871_a_122_476lo.jpg

那么...如何正确添加 QLabel 小部件?两个都试过

QLabel* label = new QLabel(fw);   // will be a sibling of MainContainer, which is the QMainWindow (mw) in this case
QLabel* label = new QLabel(mw);   // will be a sibling of QDesignerWidget
Run Code Online (Sandbox Code Playgroud)

显然是其中任何一个作品。

mil*_*lot 5

如果您只想在表单上显示小部件,可以将 QMainWindow 或 QDialog 设置为小部件父级:

QLabel *l = new QLabel(this);
l->setText("My long string");
Run Code Online (Sandbox Code Playgroud)

是指向当前的QDialog的或QMainWindow的一个指针。

否则,正如 ufukgun 指出的那样,如果您需要小部件占据 QMainWindow 的中心,则可以使用 setCentralWidget。