Hi all,
I am currently developping a custom tree view and I would like to enable drag and drop,
From the tree picture attached to this message, I would like to be able to reorder 1rst level element.
For now, I was able to enable the DnD function but I am somewhat lost with my model implementation.
From the Model/View programming tutorial, I should implement all those methods: QAbstractItemModel::insertRows(), QAbstractItemModel::removeRows(), QAbstractItemModel::dropMimeData() and QAbstractItemModel::setData().
With the available examples and tutorial, I don't understand this mechanism.
In my case, I do have a tree model which contains nodes. Those nodes contains Tree Item (== data). When I insert a child to an existing node, I have the following code:
bool ResourceTreeNode::insertChildren(int position, int count, CommonTreeItem* treeItem)
{
if (position < 0 || position > m_childrens.size()) {
return false;
}
for (int row = 0; row < count; ++row) {
ResourceTreeNode* nodePtr = new ResourceTreeNode(treeItem, this);
m_childrens.insert(position, nodePtr);
}
return true;
}
bool ResourceTreeNode::insertChildren(int position, int count, CommonTreeItem* treeItem)
{
if (position < 0 || position > m_childrens.size()) {
return false;
}
for (int row = 0; row < count; ++row) {
ResourceTreeNode* nodePtr = new ResourceTreeNode(treeItem, this);
m_childrens.insert(position, nodePtr);
}
return true;
}
To copy to clipboard, switch view to plain text mode
From my understanding I should write a method QAbstractItemModel::insertRows(int row, int count, const QModelIndex &parent) in my custom model which call the method ResourceTreeNode::insertChildren.
The problem is that I don't get how I can create my TreeItem (contained in my tree node) from those attributes: row, count and parent index? I guess I should get the datas from the node I am dropping to create the new one but I really don't see the trick?
Does somebody have an example using a custom model (with more than one column)?
Sometimes I even read people who implement drag and drop event. Should I do that also? and why?
Thank you for helping me.
Best Regards,
Lionel
Bookmarks