PDA

View Full Version : beginInsertRows seems not to be working



ugurkoltuk
6th January 2010, 11:27
Hi all,

I have my own model SettingsTreeModel (which is editable) and here is the part of my code:



bool SettingsTreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
{

StackTrace st("stm setdata");
if (role != Qt::EditRole)
return false;
SettingsNode *node = static_cast<SettingsNode *>(index.internalPointer());
QStringList attributeList;
switch(index.column()) {
case 0:
node->setName(value.toString());
break;
case 1:
node->setAttributeMap(AttributeMap(value.toString()));
break;
case 2:
beginInsertRows(index,0,0); //a new row may be prepended.
st.addExtraInfo(QString("beginInsertRows(%1, %2, %3)").arg(reinterpret_cast<unsigned>(index.internalPointer())).arg(0).arg(0));
node->setValue(value.toString());
endInsertRows(); //maybe?
break;
default:
return false;
}
emit dataChanged(index, index);
return true;
}


Here the data is changed (new row is inserted) but view doesn't show it. why do u think?

btw, i tested my model againist qt labs' modeltest and there isn't any error.

faldzip
6th January 2010, 12:05
It is hard for me to understand what you are doing :P QAbstractItemModel::setData() is for setting data to existing index. You set your value to your internal structure "node" so I don't see any reason for model to insert any additional row - so there is no reason to call beginInsertRows() or endInsertRows(), which dont insert any rows - just notify view etc about inserting new rows. And I suggest implementing rows inserting in QAbstractItemModel::insertRow() or QAbstractItemModel::insertRows().

ugurkoltuk
6th January 2010, 13:38
Here is the story about inserting rows in setData:

this is a kind of tree xml editor, something similar to Simple DOM Model Example (http://doc.trolltech.com/4.2/itemviews-simpledommodel.html). I already have QAbstractItemModel::insertRow() and QAbstractItemModel::insertRows() implemented, and I called beginInsertRows and it works fine.

What I did here is this: In DOM specification(http://www.w3.org/TR/REC-DOM-Level-1/), it says that any node which has a text value, will be interpreted like having it's first child element(a text node) with the value of the text.

So when editing an existing xml file, if there is a node not having any child or value, when user wants to set its value, i should add a child to this node (before any child) and it should be a text node. anyway, i somehow implemented it, and when i edit the value of a node which didn't have any value before, view shows nothing, but when i save file and reload it, i see that actually internally what i wanted has happened and the node has a new child element (#text) and value is set.

my question is, why i don't see updated tree? any ideas? I can paste here any code on request.

faldzip
6th January 2010, 14:21
the thing is that if you use setData() it is meant that you set data to the existing index. How are editing value node if there is no such node? I don't understand it, because you want to edit not existing index in your model which will make this model to insert this index...
Then my advice is to insert a new node (index) with insertRow() and then edit its value.