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.
- Load the model
Qt Code:
void MainWindow::LoadMyModel() { --Code snipped has been cut down to save space while (query.next()) { for(int col=0; col < rec.count(); col++) { item->setData(query.value(col),Qt::DisplayRole); mymodel->setItem(row,col,item); } ++row; } }To copy to clipboard, switch view to plain text mode- Create tree structure
Qt Code:
void MainWindow::createtreestructure() { for(int row = 0; row < mymodel->rowCount(); ++row ) { int parentid = item->data(Qt::DisplayRole).toInt();//this integer determines the (new) parent of Item oldparentItem = static_cast<QStandardItem *>(item->parent()); //cast parent in order to use QStandardItem::takeChild if(oldparentItem){ oldparentItem->takeChild(item->row(),item->column());//remove (but do not delete) Item from model to avoid double entry } if( parentid >0 ) { //ParentKeys has been declared earlier as QMap<int,QStandardItem*> ParentKeys main->ParentKeys.value(parentid)->setChild(0,item); //Asign child to new parent } else { parentItem->setChild(0,item); //no parentid was found = assign to rootItem } } }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:
QStandardItemModel model; for (int i = 0; i < 4; ++i) { parentItem->appendRow(item); parentItem = item; }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
Bookmarks