Q: How can I automatically add new objects in Qt5? 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?
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QFrame>
#include <QVBoxLayout>

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

mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    for (int i = 0; i < 10; i++)
    {
        MainWindow::CreateNewObj(i,0,1,1);
    }
    // HERE

    // this->ui->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->ui->scrollAreaWidgetContents);
    QVBoxLayout *verticalLayout;
    verticalLayout = new QVBoxLayout(frame);
    verticalLayout->setObjectName(QString::fromUtf8("verticalLayout_") + QString::number(row));

    frame->setObjectName(QString::fromUtf8("frame_") + QString::number(row));
    frame->setMinimumSize(QSize(100, 100));
    frame->setMaximumSize(QSize(16777215, 50));
    frame->setStyleSheet(QString::fromUtf8("background-color: rgb(255, 170, 127);"));
    frame->setFrameShape(QFrame::StyledPanel);
    frame->setFrameShadow(QFrame::Raised);

    this->ui->gridLayout->addWidget(frame, row, column, rowSpan, columnSpan);
}

mainwindow.ui:


A: It should be something like:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);

QVBoxLayout *verticalLayout = new QVBoxLayout(ui->scrollAreaWidgetContents());
verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));

for (int i = 0; i < 10; i++)
{
  QFrame *frame = new QFrame(ui->scrollAreaWidgetContents());
  frame->setObjectName(QString::fromUtf8("frame_") + QString::number(i));
  frame->setMinimumSize(QSize(100, 100));
  frame->setMaximumSize(QSize(16777215, 50));
  frame->setStyleSheet(QString::fromUtf8("background-color: rgb(255, 170, 127);"));
  frame->setFrameShape(QFrame::StyledPanel);
  frame->setFrameShadow(QFrame::Raised);

    verticalLayout->addWidget(frame, i, 0, 1, 1);
  }
}

