purga
purga

Reputation: 453

how to add an widget into the Form In QtDesigner

in qdesigner_workbench.cpp, how can I add a widget (say QLabel) into a FormWindow by code? Since methods like createWidget()...etc are all abstract, how do I properly use the internal mechanics to add QLabel into the active FormWindow?

EDIT:

In qdesigner_workbench.cpp, this is currently what I have:

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

The mw (obtained from fw->mainContainer()) is actually a MainWindow, however the real data I need is in:

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

There are 9 widgets in the designer, and you can see there's 9 arrays in children mentioned above; see this link (an image) for illustration.

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

So... how can I correctly add the QLabel widget? Tried both

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

and apprarently either of the works.

Upvotes: 3

Views: 3350

Answers (3)

milot
milot

Reputation: 1060

If you want just to display a widget on a form, you can set your QMainWindow or QDialog to be the widget parent:

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

Where this is a pointer pointing to your current QDialog or QMainWindow.

Otherwise as ufukgun pointed out, you can use setCentralWidget if you need your widget to occupy the center of the QMainWindow.

Upvotes: 5

Patrice Bernassola
Patrice Bernassola

Reputation: 14416

You should add any QWidget to the QLayout of the form.This will put it into the display strategy of the form when resizing it.

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

Depending of the QLayout you are using, parameters of the add function will not be the same.

Upvotes: 1

ufukgun
ufukgun

Reputation: 7209

create a widget and add it to your main window as it is your central widget

mainWindow->setCentralWidget(centralWidget);

if you want to add a label, you can add it to this central widget

Upvotes: 0

Related Questions