-1

I created QT custom widget MyWidget with a .ui file. But when I place QWidget to MainWindow in QT designer and promote it to MyWidget, it just renders an empty widget.

I figured out why it doesn't work. In generated file ui_mainwindow.h in function setupUi is code for displaying my widget following:

widget = new MyWidget(centralWidget);
widget->setObjectName(QString::fromUtf8("widget"));
verticalLayout->addWidget(widget);

It works, when I call Ui::MyWidget.setupUi (replace the code above with following):

widget = new MyWidget(centralWidget);
Ui::MyWidget uiOfWidget;
uiOfWidget.setupUi(widget);
widget->setObjectName(QString::fromUtf8("widget"));
verticalLayout->addWidget(widget);

So my question is, how it's possible to reach one of following:

  • Generate code like this automatically when compiling MainWindow.ui file
  • Remove the need for calling Ui::MyWidget.setupUi
  • Or other fix...?

I use QT5. Thank you!

FireFragment
  • 596
  • 1
  • 4
  • 15
  • 2
    Normally I call setupUi in the constructor for MyWidget just like I would do for any other widget that uses a `.ui` – drescherjm Dec 07 '21 at 20:52

1 Answers1

0

As @drescherjm pointed out, the solution is to call Ui::MyWidget.setupUi in constructor of MyWidget:

MyWidget::MyWidget(QWidget* parent, Qt::WindowFlags f): QWidget(parent, f) {
    Ui::MyWidget ui;
    ui.setupUi(this);
}
FireFragment
  • 596
  • 1
  • 4
  • 15