title: How can I automatically add new objects in Qt5?
body: <p>For convenience, I created a function that helps me add new objects, but the MainWindow function doesn't accept them. So how can the MainWindow function receive the newly created object?</p>
  <p>mainwindow.h</p>
  <pre><code>#ifndef MAINWINDOW_H
  #define MAINWINDOW_H
  
  #include &lt;QMainWindow&gt;
  #include &lt;QFrame&gt;
  #include &lt;QVBoxLayout&gt;
  
  QT_BEGIN_NAMESPACE
  namespace Ui { class MainWindow; }
  QT_END_NAMESPACE
  
  class MainWindow : public QMainWindow
  {
      Q_OBJECT
  
  public:
      MainWindow(QWidget *parent = nullptr);
      ~MainWindow();
  
  private:
      void CreateNewObj(int row, int column, int rowSpan, int columnSpan);
  
  private:
      Ui::MainWindow *ui;
  };
  #endif // MAINWINDOW_H
  </code></pre>
  <p>mainwindow.cpp</p>
  <pre><code>#include &quot;mainwindow.h&quot;
  #include &quot;ui_mainwindow.h&quot;
  
  MainWindow::MainWindow(QWidget *parent)
      : QMainWindow(parent)
      , ui(new Ui::MainWindow)
  {
      ui-&gt;setupUi(this);
  
      for (int i = 0; i &lt; 10; i++)
      {
          MainWindow::CreateNewObj(i,0,1,1);
      }
      // HERE
  
      // this-&gt;ui-&gt;frame_1...   IN HERE   MainWindow function does not accept new object created by CreateNewObj function
  
      // HERE
  }
  
  MainWindow::~MainWindow()
  {
      delete ui;
  }
  
  void MainWindow::CreateNewObj(int row, int column, int rowSpan, int columnSpan)
  {
      QFrame *frame = new QFrame(this-&gt;ui-&gt;scrollAreaWidgetContents);
      QVBoxLayout *verticalLayout;
      verticalLayout = new QVBoxLayout(frame);
      verticalLayout-&gt;setObjectName(QString::fromUtf8(&quot;verticalLayout_&quot;) + QString::number(row));
  
      frame-&gt;setObjectName(QString::fromUtf8(&quot;frame_&quot;) + QString::number(row));
      frame-&gt;setMinimumSize(QSize(100, 100));
      frame-&gt;setMaximumSize(QSize(16777215, 50));
      frame-&gt;setStyleSheet(QString::fromUtf8(&quot;background-color: rgb(255, 170, 127);&quot;));
      frame-&gt;setFrameShape(QFrame::StyledPanel);
      frame-&gt;setFrameShadow(QFrame::Raised);
  
      this-&gt;ui-&gt;gridLayout-&gt;addWidget(frame, row, column, rowSpan, columnSpan);
  }
  </code></pre>
  <p>mainwindow.ui:</p>
  <p><a href="https://i.stack.imgur.com/NJljP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NJljP.png" alt="mainwindow.ui" /></a></p>