whites11
25th March 2010, 17:07
Hi all.
i have a non-really-qt related newbie problem that you surely can solve easily.
i have a class 'Model' which holds some members. one of thos is a QList.
this is the code for that class:
Header:
#ifndef MODEL_H
#define MODEL_H
#include <QList>
class Model
{
public:
Model();
QList<QString> list() const;
private:
QList<QString> m_list;
};
#endif // MODEL_H
cpp
#include "model.h"
#include <QString>
Model::Model()
{
m_list.append(QString("item1"));
m_list.append(QString("item2"));
}
QList<QString> Model::list() const
{
return m_list;
}
when i execute this snippet of code:
Model m;
qDebug() << "Before clear()";
for(int i=0;i<m.list().count();i++)
{
qDebug() << m.list().at(i);
}
m.list().clear();
qDebug() << "After clear()";
for(int i=0;i<m.list().count();i++)
{
qDebug() << m.list().at(i);
}
i get this output:
"Before clear()"
"item1"
"item2"
"After clear()"
"item1"
"item2"
i think that the problem is the fact that calling m.list() returns me a copy of the QList instead of a reference to it.
i think that one way to solve this is returning a pointer to the QList instead, but i'm wondering if there is another way to do this avoiding pointers.
sorry for the long post and thanks if you read since here :)
i have a non-really-qt related newbie problem that you surely can solve easily.
i have a class 'Model' which holds some members. one of thos is a QList.
this is the code for that class:
Header:
#ifndef MODEL_H
#define MODEL_H
#include <QList>
class Model
{
public:
Model();
QList<QString> list() const;
private:
QList<QString> m_list;
};
#endif // MODEL_H
cpp
#include "model.h"
#include <QString>
Model::Model()
{
m_list.append(QString("item1"));
m_list.append(QString("item2"));
}
QList<QString> Model::list() const
{
return m_list;
}
when i execute this snippet of code:
Model m;
qDebug() << "Before clear()";
for(int i=0;i<m.list().count();i++)
{
qDebug() << m.list().at(i);
}
m.list().clear();
qDebug() << "After clear()";
for(int i=0;i<m.list().count();i++)
{
qDebug() << m.list().at(i);
}
i get this output:
"Before clear()"
"item1"
"item2"
"After clear()"
"item1"
"item2"
i think that the problem is the fact that calling m.list() returns me a copy of the QList instead of a reference to it.
i think that one way to solve this is returning a pointer to the QList instead, but i'm wondering if there is another way to do this avoiding pointers.
sorry for the long post and thanks if you read since here :)