Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a factory to create QT promoted widgets

Tags:

c++

qt

Promoting widgets is one of the best features in the QT Designer. But when using the designer, it 'new's the object there it self ( inside setupUi ). This limits the ability to extend the interface file.

Is it possible to use a factory of some sort to get the promoted widgets, so that when a sub class of a promoted widget is required, this could be achieved via changing the factory and returning the sub class of the widget.

requirement:

NamePanel* myPanel;
void setupUi(QWidget* pWidget)
{
     myPanel = Factory::CreateNamePanel();
}

Is there a work around to achieve this?

like image 867
Kanishka Weerasekara Avatar asked Jan 24 '26 17:01

Kanishka Weerasekara


1 Answers

Unfortunately it seems not to be possible to add a factory into the Ui files. A workaround would be to have a QWidget with an attached layout in the ui and add the factory generated widget to this layout.

enter image description here

The code would look like this:

ui->setupUi(this);

QWidget *panel = Factory::CreateNamePanel();
ui->namePanelContainer->layout()->addWidget(panel);
like image 149
tomvodi Avatar answered Jan 27 '26 08:01

tomvodi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!