Adding a widget to a QFrame
-
I have a QMainWindow which has a QFrame(object name: navigationFrame) and I struggling to figure out a way to add a user widget(navigationWidget) onto this frame programmatically.
Is there a way to do it?
I tried the following:
layout->addWidget(navigationWidget);Also, what is the recommended practice when adding user defined widgets within a form?
-
Is there a way to do it?
If the main window is the standard main window created by the wizard and the
navigationForm
was added in the designer then you can access it through theui
member of the main window class, e.g.:auto layout = new QVBoxLayout(); layout->addWidget(navigationWidget); ui->navigationFrame->setLayout(layout);
Also, what is the recommended practice when adding user defined widgets within a form?
You place a "dummy" plain widget (or the base class of your custom widget) in the editor and then promote it to your custom class.
-
My code:
//panelCalculate is a QFrame QPushButton* btn = new QPushButton(ui->panelCalculate); btn->setText("ABC");
It works:
-
Hi
Yes if you use the panel as a parent, the buttons become a child widget (and shown inside). So that sort of works.
However, without a layout, you must manually position them as else they all be shown at pos 0,0 as the first one is.
And then if you resize your window, they won't follow / change positions so one should really try to use a layout as
Chris-Kawa shows. :)