Pergunta

I have following strange problem.

I've implemented a QAbstractItemModel to the point that I can insert child nodes to the tree view but something strange occurs when I try to add the nodes via the insertRows() method.

First where all is called:

QApplication a(argc, argv);

QResource::registerResource("Qt5Tutorial.rcc");

QTreeView *treeView = new QTreeView();
treeView->show();

Node rootNode("rootNode");
CameraNode childNode0("childNode0", &rootNode);
CameraNode childNode1("childNode1", &rootNode);
LightNode childNode2("childNode2", &rootNode);
CameraNode childNode3("childNode3", &childNode0);
TransformNode childNode4("childNode4", &childNode2);
TransformNode tryNode("potato");

// setup model
ObjectTreeModel model(&rootNode);
treeView->setModel(&model);

// insert directly via the insert child method 
// this works!
childNode0.insertChild(1, &tryNode);

// get the QModelIndex of childNode1
// must be passed in the insertRows() method 
QModelIndex index(model.index(1, 0, QModelIndex()));

// the output is "childNode1" what is totally right
qDebug() << "index: "<<static_cast<Node*>(index.internalPointer())->getName();

// output see posted beneath
qDebug() << rootNode.log();

// should insert in "childNode1" -> at 0th position and just 1 Node object
// see the method beneath
model.insertRows(0, 1, index);

// if i try to call the method rootNode.log(); now again, it crashes 

return a.exec();

This is the output from the rootNode.log() call:

---rootNode
    ---childNode0
            ---childNode3
            ---potato
    ---childNode1
    ---childNode2
            ---childNode4

As you can see the "Potato" Node is correctly inserted.

View an image http://www10.pic-upload.de/04.01.13/m65huuqq4ruu.png

But once I try to expand the childNode1 node it crashes. But look at the last comment in the code above. As i mentioned -> if i try to output the tree view now (it iterates through all nodes) it crashes.

When the method is called everything seems to be ok - just when i try to expend the tree view it crashes -> the debug output let me think that all should be ok

The actual error message is a access violation when reading at position ... (translated from German - don't know if its called the same in English)

 bool ObjectTreeModel::insertRows(int position, int row, const QModelIndex &parent)
{
beginInsertRows(parent, position, position + row - 1);

Node *parentNode = getNode(parent);
qDebug() << "parentName: " << parentNode->getName();

bool success = false;
for(int i = position; i < row; i++)
{

    qDebug() << "inside loop"; 
    qDebug() << "position: " << position << "row: " << row;

    TransformNode childNode("insertedNode");
    success = parentNode->insertChild(i, &childNode);

    qDebug() << "success: " << success;
}

endInsertRows();

return success;

}

The debug output for the method above:

getNode: successful 
parentName: "childNode1" 
inside loop 
position:  0 row:  1 
called inserchild 
success:  true 

I have no idea why this happens becuase the debug output seems right and it should be basically the same as insert the node directly via the insertChild method.

I hope that someone has an idea why it doesn't work.

Best regards, Michael

Foi útil?

Solução

Almost everything is correct. Just this two lines not:

TransformNode *childNode = new TransformNode("insertedNode");
success = parentNode->insertChild(i, childNode);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top