如何将窗口小部件添加到Qt设计器中的窗体中

时间:2009-11-18 11:32:12

标签: qt qt4

在qdesigner_workbench.cpp中,如何通过代码将窗口小部件(比如QLabel)添加到FormWindow中? 由于createWidget()等方法都是抽象的,我如何正确使用内部机制将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.");

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

mw -> children[2] (which is a QDesignerWidget) -> children

设计师中有9个小部件,你可以看到上面提到的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

并且显然是其中一项工作。

3 个答案:

答案 0 :(得分:4)

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

QLabel *l = new QLabel(this);
l->setText("My long string");

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

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

答案 1 :(得分:1)

您应该add任何QWidget到表单的QLayout。这会在调整表单时将其放入表单的显示策略中。

form->ui->layout->add(yourQWidget);

根据您使用的QLayout,添加功能的参数将不相同。

答案 2 :(得分:0)

创建一个小部件并将其添加到主窗口,因为它是您的中心小部件

mainWindow->setCentralWidget(centralWidget);

如果要添加标签,可以将其添加到此中央窗口小部件

相关问题