add a new button in a QWidget in Qt5

Viewed 154

I am trying to add a new QPushButton to a QWidget, but it doesn't seem to have an addWiget method.

I am using the following code:

  QWidget *wdg = new QWidget;
  QPushButton *btn=new QPushButton(this);

  btn->setText("text");
  
  wdg->addWidget(btn)

  wdg->show();
  hide();

the 6th line is giving an error

1 Answers

You can pass QWidget as parent to QPushButton.

QWidget *wdg = new QWidget;
QPushButton *btn=new QPushButton(wdg);
Related