Reputation: 57
I would like to make a QList of QwtPlotCurve s. The reason for this is to be able to remove them later form my QwtPlot. I have the following code:
QList<QwtPlotCurve> myList = new QList<QwtPlotCurve>;
QwtPlotCurve* curve1 = new QwtPlotCurve();
QwtPlotCurve* curve2 = new QwtPlotCurve();
curves->append(curve1);
curves->append(curve2);
The code doesn't compile and the compiler outputs:
error: conversion from 'QList' to non-scalar type 'QList' requested
error: no matching function for call to 'QList::append(QwtPlotCurve
note: candidates are:
note: void QList::append(const T&) [with T = QwtPlotCurve]
note: no known conversion for argument 1 from 'QwtPlotCurve*' to 'const QwtPlotCurve&'
note: void QList::append(const QList&) [with T = QwtPlotCurve]
note: no known conversion for argument 1 from 'QwtPlotCurve*' to 'const QList&'
...
I says the QwtPlotCurve should be constant, but I don't know how to deal with it. I don't know neither whether storing curves in a QList and then removing it (on user demand) from the plot is the right approach.
After the answer of sjwarner I tried the following:
QList<QwtPlotCurve*> curves;
QwtPlotCurve* curve1 = new QwtPlotCurve();
QwtPlotCurve* curve2 = new QwtPlotCurve();
curves->append(curve1);
curves->append(curve2);
and I got the following error:
error: base operand of '->' has non-pointer type 'QList' error: base operand of '->' has non-pointer type 'QList'
I understood this error in the following way: curves is a QList and it should be a pointer to the QList.
If I try:
QList<QwtPlotCurve*>* curves = new QList<QwtPlotCurve*>;
QwtPlotCurve* curve1 = new QwtPlotCurve();
QwtPlotCurve* curve2 = new QwtPlotCurve();
curves->append(curve1);
curves->append(curve2);
it works fine. I am going to look at "Implicit sharing" pointed by sjwarner to get rid of "new" operators.
Upvotes: 0
Views: 997
Reputation: 7687
You have two problems:
As commented by Kamil Klimlek above, you are declaring your QList
object on the stack, and then trying to allocate it on the heap - since new
returns a pointer to the type of object you are new
ing, so you are effectively trying to do the following:
QList<T> = *QList<T>
As an aside: it is very rare that you'll need to new
off a QList
, since Qt implements implicit sharing for all of its container classes - in a nutshell, you can confidently declare all Qt containers (and plenty of other classes besides) as stack objects and pass-by-value if the contained data is needed elsewhere - Qt will handle all of the memory efficiencies and object cleanup.
Read this for more info.
You are declaring a QList
of objects and trying to fill it with pointers to objects. you need to decide whether you want your QList
to contain copies of the data:
QList<QwtPlotCurve> curves;
QwtPlotCurve curve1();
QwtPlotCurve curve2();
curves.append(curve1);
curves.append(curve2);
Or whether you want to allocate your QwtPlotCurve
s on the heap and store pointers to them in the QList
:
QList<QwtPlotCurve*> curves;
QwtPlotCurve* curve1 = new QwtPlotCurve();
QwtPlotCurve* curve2 = new QwtPlotCurve();
curves.append(curve1);
curves.append(curve2);
Upvotes: 1