1

I have a class Variable, extension of QWidget. I want to add it to the vector QVector <Variable> *list_variables, but when I try, I get the error

Attempt to refer to a deleted function

Here is the code :

QVector <Variable> *list_variables;
QVBoxLayout *layout = new QVBoxLayout (scroll_area);

for (int i = 0; i < 5; i ++)
{
    Variable *variable = new Variable;
    list_variables->append(*variable); // Error happens here
    layout->addWidget(variable);
}

Variable.h :

#ifndef VARIABLE_H
#define VARIABLE_H

#include "constant.h"

class Variable : public QWidget
{
    public :
        Variable();

    private :
        QLineEdit *line_edit_name, *line_edit_value, *line_edit_imprecision;
};

#endif // VARIABLE_H

Variable.cpp :


#include "constant.h"

Variable::Variable()
{
    this->setFixedSize(240, 30);

    line_edit_name = new QLineEdit(this);
    line_edit_name->setGeometry(0, 0, 50, 20);
    line_edit_name->setPlaceholderText("Name");

    line_edit_value = new QLineEdit (this);
    line_edit_value->setGeometry(60, 0, 80, 20);
    line_edit_value->setPlaceholderText("Value");

    line_edit_imprecision = new QLineEdit (this);
    line_edit_imprecision->setGeometry(150, 0, 80, 20);
    line_edit_imprecision->setPlaceholderText("Imprecision");
}

All the includes are done in "constant.h"

William Miller
  • 9,839
  • 3
  • 25
  • 46
Thechi2000
  • 35
  • 6
  • 2
    Does this answer your question? [Could I have copy constructor for subclass of QObject?](https://stackoverflow.com/questions/25890802/could-i-have-copy-constructor-for-subclass-of-qobject) – G.M. Jan 18 '20 at 09:24

1 Answers1

0

I have found a solution: declaring the QVector as a vector of Variable pointers.

So I have:

QVector <Variable*> list_variables; // Instead of QVector <Variable> *list_variables;
QVBoxLayout *layout = new QVBoxLayout (scroll_area);

for (int i = 0; i < 5; i ++)
{
    Variable *variable = new Variable;
    list_variables.append(variable); // Instead of list_variables->append(*variable);
    layout->addWidget(variable);
}
William Miller
  • 9,839
  • 3
  • 25
  • 46
Thechi2000
  • 35
  • 6