C++ Создание объектов класса в другом классе с помощью цикла for
Допустим мне необходимо создать массив объектов класса Button... (Ну или любого другого класса.) Как грамотно это реализовать? Перепробовал массу вариантов - безрезультатно... Ругается на "attempting to reference a deleted function". Вопрос рациональности конкретной реализации оставим за скобками. Представим что это просто абстрактный пример.
class Button : public QObject{
Q_OBJECT
public:
QFont font;
QPushButton button;
Button(QString text, Window &window, QGridLayout &grid, int row, int column, void(Window::* slot )()):
button(&window.window)
{
font.setFamily("Arial");
font.setPixelSize(15);
button.setText(text);
button.setMinimumHeight(40);
button.setStyleSheet("padding-left: 10px; padding-right: 10px;");
button.setFont(font);
grid.addWidget(&button, row, column);
connect(&button, &QPushButton::clicked, &window, slot);
}
};
class Main_window :public Window{
Q_OBJECT
std::vector<Button> button_array;
public:
Main_window():
Window(2000, 600, "Дневник")
{
for (int c = 0; c <= 12; c++)
{
for (int r = 2; r <= 10; r++)
{
Button* button = new Button("Кнопка", *this, grid, r, c, &Window::confirm_window_open);
button_array.push_back(*button);
// пытаемся добавить в вектор... ругается
}
}
for (int c = 0; c <= 12; c++)
{
for (int r = 2; r <= 10; r++)
{
Button* button = new Button("Кнопка", *this, grid, r, c, &Window::confirm_window_open);
// просто циклом создаем кнопки... все работает. Рисует в окне 12 колонок по 10 кнопок.
}
}
}
};