Vector of custom classes

I'm trying to create a vector of custom items that I have created but I encounter a very strange problem.

When I create an instance of my class I don't have any problem but when I try to add it the vector I have a very long error and I don't know what to make of it.

I don't know if it helps but the m_mesureList is a private attribute of the class PanneauMesure.

Declaration in the .h:

std::vector<Mesure> m_mesureList;

Use:

void PanneauMesure::add(QPointF point1, QPointF point2, QPointF point3, QGraphicsItem *line1, QGraphicsItem *line2)
{
    Mesure mesure(point1, point2, point3, line1, line2, this);
    //m_mesureList.push_back(mesure);
}

error:

In file included from ..\testQt\/PanneauMesure.h:21,
                 from ..\testQt\PanneauMesure.cpp:1:
d:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtGui/qwidget.h: In copy constructor 'Mesure::Mesure(const Mesure&)':
..\testQt\/Mesure.h:14:   instantiated from 'void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = Mesure]'
d:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/bits/stl_vector.h:737:   instantiated from 'void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = Mesure, _Alloc = std::allocator<Mesure>]'
..\testQt\PanneauMesure.cpp:104:   instantiated from here
d:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtGui/qwidget.h:806: error: 'QWidget::QWidget(const QWidget&)' is private
..\testQt\/Mesure.h:14: error: within this context
In file included from d:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/mingw32/bits/c++allocator.h:34,
                 from d:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/bits/allocator.h:48,
                 from d:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/string:43,
                 from d:\QtSDK\Desktop\Qt\4.8.1\mingw\include/QtCore/qstring.h:54,
                 from d:\QtSDK\Desktop\Qt\4.8.1\mingw\include/QtCore/qobject.h:48,
                 from d:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtGui/qwidget.h:46,
                 from d:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtGui/QWidget:1,
                 from ..\testQt\/PanneauMesure.h:4,
                 from ..\testQt\PanneauMesure.cpp:1:
d:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/ext/new_allocator.h: In member function 'void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = Mesure]':
d:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/ext/new_allocator.h:105: note: synthesized method 'Mesure::Mesure(const Mesure&)' first required here 
In file included from ..\testQt\/PanneauMesure.h:21,
                 from ..\testQt\PanneauMesure.cpp:1:
d:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtGui/qwidget.h: In member function 'Mesure& Mesure::operator=(const Mesure&)':
..\testQt\/Mesure.h:14:   instantiated from 'void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = Mesure, _Alloc = std::allocator<Mesure>]'
d:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/bits/stl_vector.h:741:   instantiated from 'void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = Mesure, _Alloc = std::allocator<Mesure>]'
..\testQt\PanneauMesure.cpp:104:   instantiated from here
d:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtGui/qwidget.h:806: error: 'QWidget& QWidget::operator=(const QWidget&)' is private
..\testQt\/Mesure.h:14: error: within this context
In file included from d:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/vector:69,
                 from d:\QtSDK\Desktop\Qt\4.8.1\mingw\include/QtCore/qvector.h:52,
                 from d:\QtSDK\Desktop\Qt\4.8.1\mingw\include/QtGui/qbrush.h:47,
                 from d:\QtSDK\Desktop\Qt\4.8.1\mingw\include/QtGui/qpalette.h:47,
                 from d:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtGui/qwidget.h:49,
                 from d:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtGui/QWidget:1,
                 from ..\testQt\/PanneauMesure.h:4,
                 from ..\testQt\PanneauMesure.cpp:1:
d:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/bits/vector.tcc: In member function 'void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = Mesure, _Alloc = std::allocator<Mesure>]':
d:\qtsdk\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/bits/vector.tcc:312: note: synthesized method 'Mesure& Mesure::operator=(const Mesure&)' first required here 

Edit, if it helps:

Mesure.cpp:

Mesure::Mesure(QPointF point1, QPointF point2, QPointF point3, QGraphicsItem *line1, QGraphicsItem *line2, QWidget *parent):
    QWidget(parent),
    m_hlayoutMesure(new QHBoxLayout(this)),
    m_buttonClose(new QToolButton(this)),
    m_labelAngle(new QLabel(this)),
    m_checkboxMesure(new QCheckBox(this)),
    m_point1(point1),
    m_point2(point2),
    m_point3(point3),
    m_line1(line1),
    m_line2(line2),
    m_angle(0)
{
    m_buttonClose->setText("X");
    m_labelAngle->setAlignment(Qt::AlignCenter);
    m_checkboxMesure->setChecked(true);
    m_hlayoutMesure->addWidget(m_checkboxMesure);
    m_hlayoutMesure->addWidget(m_labelAngle);
    m_hlayoutMesure->addWidget(m_buttonClose);
    m_hlayoutMesure->setSpacing(10);
    m_hlayoutMesure->setMargin(0);
    QObject::connect(m_buttonClose, SIGNAL(clicked()), this, SLOT(close()));
}

PanneauMesure.cpp:

PanneauMesure::PanneauMesure(QWidget *parent) :
    QWidget(parent),
    m_vlayoutTotal(new QVBoxLayout(this)),
    m_groupboxMesures(new QGroupBox("Mesures", this)),
    m_vlayoutGroupBoxMesures(new QVBoxLayout(m_groupboxMesures)),
    m_scrollareaMesures(new QScrollArea(m_groupboxMesures)),
    m_widgetMesures(new QWidget(m_groupboxMesures)),
    m_hlayoutMesures(new QHBoxLayout(m_widgetMesures)),
    m_vlayoutMesures(new QVBoxLayout(m_widgetMesures)),
    m_groupboxOptions(new QGroupBox("Options", this)),
    m_vlayoutOptions(new QVBoxLayout(m_groupboxOptions)),
    m_buttonRAZ(new QPushButton("Remise à zéro", m_groupboxOptions)),
    m_widgetAlpha(new QWidget(m_groupboxOptions)),
    m_hlayoutAlpha(new QHBoxLayout(m_widgetAlpha)),
    m_labelAlphaText(new QLabel("Alpha :", m_groupboxOptions)),
    m_labelAlphaValeur(new QLabel("50", m_groupboxOptions)),
    m_sliderAlpha(new QSlider(m_groupboxOptions)),
    m_hlayoutCouleur(new QHBoxLayout(m_groupboxOptions)),
    m_labelCouleur(new QLabel("Couleur :", m_groupboxOptions)),
    m_buttonCouleur(new QToolButton(m_groupboxOptions)),
    m_couleur(Qt::yellow),
    m_hlayoutLongueur(new QHBoxLayout(m_groupboxOptions)),
    m_labelLongueur(new QLabel("Longueur :  ", m_groupboxOptions)),
    m_spinboxLongueur(new QSpinBox(m_groupboxOptions)),
    m_longueur(20),
    m_hlayoutLargeur(new QHBoxLayout(m_groupboxOptions)),
    m_labelLargeur(new QLabel("Largeur :", m_groupboxOptions)),
    m_spinboxLargeur(new QSpinBox(m_groupboxOptions)),
    m_largeur(5),
    m_mesureList()
{
    // Alpha
    m_sliderAlpha->setMaximum(100);
    m_sliderAlpha->setValue(50);
    m_hlayoutAlpha->addWidget(m_labelAlphaText);
    m_hlayoutAlpha->addWidget(m_labelAlphaValeur);
    m_labelAlphaText->setAlignment(Qt::AlignRight);
    m_labelAlphaValeur->setAlignment(Qt::AlignLeft);
    m_sliderAlpha->setOrientation(Qt::Horizontal);
    QObject::connect(m_sliderAlpha, SIGNAL(valueChanged(int)), m_labelAlphaValeur, SLOT(setNum(int)));

    // Couleur
    QPixmap px(20, 20);
    px.fill(m_couleur);
    m_buttonCouleur->setIcon(px);
    m_hlayoutCouleur->addWidget(m_labelCouleur);
    m_hlayoutCouleur->addStretch(2);
    m_hlayoutCouleur->addWidget(m_buttonCouleur);
    m_hlayoutCouleur->addStretch(1);
    QObject::connect(m_buttonCouleur, SIGNAL(clicked()), this, SLOT(ouvrirCouleur()));

    // Longueur
    m_spinboxLongueur->setMinimum(1);
    m_spinboxLongueur->setValue(m_longueur);
    m_spinboxLongueur->setMaximum(999);
    m_hlayoutLongueur->addWidget(m_labelLongueur);
    m_hlayoutLongueur->addStretch(1);
    m_hlayoutLongueur->addWidget(m_spinboxLongueur);
    QObject::connect(m_spinboxLongueur, SIGNAL(valueChanged(int)), this, SLOT(sendParam()));

    // Largeur
    m_spinboxLargeur->setMinimum(1);
    m_spinboxLargeur->setMaximum(100);
    m_spinboxLargeur->setValue(m_largeur);
    m_hlayoutLargeur->addWidget(m_labelLargeur);
    m_hlayoutLargeur->addStretch(1);
    m_hlayoutLargeur->addWidget(m_spinboxLargeur);
    QObject::connect(m_spinboxLargeur, SIGNAL(valueChanged(int)), this, SLOT(sendParam()));

    // Options
    m_vlayoutOptions->addWidget(m_buttonRAZ);
    m_vlayoutOptions->addWidget(m_widgetAlpha);
    m_vlayoutOptions->addWidget(m_sliderAlpha);
    m_vlayoutOptions->addLayout((m_hlayoutCouleur));
    m_vlayoutOptions->addLayout((m_hlayoutLongueur));
    m_vlayoutOptions->addLayout((m_hlayoutLargeur));
    m_vlayoutOptions->setSpacing(0);
    QObject::connect(m_buttonRAZ, SIGNAL(clicked()), this, SLOT(remiseAZero()));

    // Mesures
    m_vlayoutMesures->addStretch(1);
    m_hlayoutMesures->addStretch(1);
    m_hlayoutMesures->addLayout(m_vlayoutMesures);
    m_hlayoutMesures->addStretch(1);
    m_widgetMesures->setLayout(m_hlayoutMesures);
    m_scrollareaMesures->setWidgetResizable(true);
    m_scrollareaMesures->setWidget(m_widgetMesures);
    m_scrollareaMesures->setFrameShape(QFrame::NoFrame);
    m_vlayoutGroupBoxMesures->addWidget(m_scrollareaMesures);
    m_vlayoutGroupBoxMesures->setMargin(0);

    // Total
    m_vlayoutTotal->addWidget(m_groupboxOptions);
    m_vlayoutTotal->addWidget(m_groupboxMesures);

}

void PanneauMesure::add(QPointF point1, QPointF point2, QPointF point3, QGraphicsItem *line1, QGraphicsItem *line2)
{
    Mesure mesure(point1, point2, point3, line1, line2, this);
    m_mesureList.push_back(mesure);
    //m_vlayoutMesures->insertWidget(0, &m_mesure);
    //QObject::connect(m_buttonRAZ, SIGNAL(clicked()), &m_mesure, SLOT(close()));
}

void PanneauMesure::remiseAZero()
{
    m_sliderAlpha->setValue(50);
    m_longueur = 20;
    m_spinboxLongueur->setValue(m_longueur);
    m_largeur = 5;
    m_spinboxLargeur->setValue(m_largeur);
    m_couleur = Qt::yellow;
    QPixmap px(20, 20);
    px.fill(m_couleur);
    m_buttonCouleur->setIcon(px);
    emit changementParam(m_couleur, m_longueur, m_largeur);
}

void PanneauMesure::ouvrirCouleur()
{
    QColor color = QColorDialog::getColor(m_couleur);
    if (color.isValid()) m_couleur=color;
    QPixmap px(20, 20);
    px.fill(m_couleur);
    m_buttonCouleur->setIcon(px);
    emit changementParam(m_couleur, m_longueur, m_largeur);
}

void PanneauMesure::sendParam()
{
    m_longueur = m_spinboxLongueur->value();
    m_largeur = m_spinboxLargeur->value();
    emit changementParam(m_couleur, m_longueur, m_largeur);
}

Answers


From the error log it seems Mesure inherits or contains a QWidget, which is not copyable. In C++03 the requirements for types to be placed in sequences is that they are copy-constructible, and your type is not.


As K-Ballo pointed out that you can't have a sequence of not-copy-constructible elements I just stored a sequence of pointers like this:

Declaration:

std::vector<Mesure*> m_mesureList;

Function:

void PanneauMesure::add(QPointF point1, QPointF point2, QPointF point3, QGraphicsItem *line1, QGraphicsItem *line2)
{
    Mesure* mesure = new Mesure(point1, point2, point3, line1, line2, this);
    m_mesureList.push_back(mesure);
}

Need Your Help

Destroy & re-create Service in my case

android android-intent broadcastreceiver android-service android-broadcast

In my activity, I start my service in onStart() of my activity &amp; bind to service in onResume() :

How to change the blue highlight color of a UITableViewCell?

ios uitableview

I'm wondering how to change the blue highlight/selection color of a UITableViewCell, any ideas?