PDA

View Full Version : Implementation of setData() with QList



Rocken
14th March 2012, 10:26
I want / have to write a setData() function in a class derived from QAbstractListModel because I want to modify the variable QList<ListItem*> m_list

in the QList I save Parameter-Objects with have a value property which can be written by calling setValue()

I tried the following lines in setData without success:


bool ListModel::setData(const QModelIndex &index, const QVariant &value, int role) {
Parameter item = *m_list[index.row()];
item.setValue(value.toString());
AND
m_list.at(index.row())->setValue(value.toString);
emit dataChanged(index,index);
return true;
}

Why is it not possible to get a Parameter object by reference and call the function setValue()?

wysota
14th March 2012, 10:55
Line #2 makes a copy of the object and you modify the copy and not the original object.

QList::at() is const and it returns a const object hence you can't call non-const methods on it.

Rocken
14th March 2012, 11:11
ok thanks.
I thought line 2 only copies the pointer to that object. ..either don't work

With the following I add new Parameter objects to my QList

ListModel *parameterList
Parameter *parm = new Parameter();
parm->setValue(3);
parameterList->appendRow(parm)

But with m_list[index.row()]->setValue(value.toString()) I get a ListItem which has not the setValue() function the Parameter object has.

I'm still not able to get the object reference.

wysota
14th March 2012, 17:35
I thought line 2 only copies the pointer to that object.
That's not a pointer declaration:

Parameter item

If you wrote the following:

Parameter *item = m_list[index.row()]
then it would have worked.

I'm still not able to get the object reference.
Your issue is strictly related to proper use of C++ and not Qt.

Rocken
15th March 2012, 07:15
Thank you for your help. You're right using pointers is general C++.