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
#ifndef MODEL_H
#define MODEL_H
#include <QList>
class Model
{
public:
Model();
QList<QString> list() const;
private:
QList<QString> m_list;
};
#endif // MODEL_H
To copy to clipboard, switch view to plain text mode
cpp
#include "model.h"
#include <QString>
Model::Model()
{
}
QList<QString> Model::list() const
{
return m_list;
}
#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;
}
To copy to clipboard, switch view to plain text mode
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);
}
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);
}
To copy to clipboard, switch view to plain text mode
i get this output:
"Before clear()"
"item1"
"item2"
"After clear()"
"item1"
"item2"
"Before clear()"
"item1"
"item2"
"After clear()"
"item1"
"item2"
To copy to clipboard, switch view to plain text mode
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
Bookmarks