I'm getting a little stuck here guys!

I'm using a QStandardItemModel (mymodel) for storing hierarchical data. At the point of filling the model with data I do not necessarily know the parent/child relationship in the dataset and the user need to be able to change that after data load. So I've divided the loading process in two steps. (1) Load the model from a database source and (2) create tree structure on the model.
  1. Load the model

    Qt Code:
    1. void MainWindow::LoadMyModel()
    2. {
    3. --Code snipped has been cut down to save space
    4. while (query.next())
    5. {
    6. for(int col=0; col < rec.count(); col++)
    7. {
    8. item->setData(query.value(col),Qt::DisplayRole);
    9. mymodel->setItem(row,col,item);
    10. }
    11. ++row;
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 
  2. Create tree structure
    Qt Code:
    1. void MainWindow::createtreestructure()
    2. {
    3. QStandardItem *parentItem = mymodel->invisibleRootItem(); //rootItem
    4. QStandardItem *oldparentItem ;//placeholder for Item->parent()
    5. for(int row = 0; row < mymodel->rowCount(); ++row ) {
    6. QStandardItem *item = mymodel->item(row,parent_acc_id);//Grap Item
    7. int parentid = item->data(Qt::DisplayRole).toInt();//this integer determines the (new) parent of Item
    8. oldparentItem = static_cast<QStandardItem *>(item->parent()); //cast parent in order to use QStandardItem::takeChild
    9. if(oldparentItem){
    10. oldparentItem->takeChild(item->row(),item->column());//remove (but do not delete) Item from model to avoid double entry
    11. }
    12. if( parentid >0 ) { //ParentKeys has been declared earlier as QMap<int,QStandardItem*> ParentKeys
    13. main->ParentKeys.value(parentid)->setChild(0,item); //Asign child to new parent
    14. } else {
    15. parentItem->setChild(0,item); //no parentid was found = assign to rootItem
    16. }
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 


All this seems pretty straight forwarded but I'm simply not able to assign the QStandardItem to a new/other parent Item. I keep getting errors like 'QStandardItem::setChild: Ignoring duplicate insertion of item 0x94a6a70'.

An other approach would be to subclass an abstractmodel (like in the simply treeview example shipped with Qt) to get the necessary functionality but it seems like QStandardItemModel has all the functionally required to create and maintain a tree structure.

Initially it wouldn't be a problem to load the model and apply the tree structure from the beginning for instance by using the example provided by Qt...

Qt Code:
  1. QStandardItem *parentItem = model.invisibleRootItem();
  2. for (int i = 0; i < 4; ++i) {
  3. QStandardItem *item = new QStandardItem(QString("item %0").arg(i));
  4. parentItem->appendRow(item);
  5. parentItem = item;
  6. }
To copy to clipboard, switch view to plain text mode 

...but I still need to be able to rearrange the parent/child relationship without going through the trouble of creating my own treemodel.

Is there anyone who has experience with QStandardItemModel on how to assign parent/child relationship or how to rearrange parent/child after the model has been created? I'm running out of ideas!

Thanks