Links between items in QTreeItemView
-
Hello. I would like to have links between items in a tree so that when I change something in one branch, changes should become immediately visible in a linked branch. Please see the picture below for an example:
This tree can have hundreds of thousands of items and several links to the same item should be possible.
I have a custom implementation of the model (inherits from QAbstractItemModel) and items. I use this method to insert children (I took the idea from Qt “Editable Tree Model” example):// Count – how many empty children I would like to insert bool SqlTreeItem::insertChildren(int position, int count, int columns, int childId) { if (position < 0 || position > childItems.size()) return false; for (int row = 0; row < count; ++row) { QVector<QVariant> data(columns); SqlTreeItem *item = new SqlTreeItem(data, childId, this); childItems.insert(position, item); } return true; }
When I need to create a link, I do that like this:
bool SqlTreeItem::linkChildren(int position, SqlTreeItem *to) { if (position < 0 || position > childItems.size()) return false; SqlTreeItem *item = to; childItems.insert(position, item); return true; }
Thus, when I want to insert a link, I just insert a pointer of the existing branch. I didn’t find anything in the documentation about this way but I still decided to try. It works fine sometimes, but sometimes does not or works incorrectly.
Could somebody tell me, what could be the esiest way and what could be the correct way to implement this? -
@user931357 said in Links between items in QTreeItemView:
but sometimes does not or works incorrectly.
Can you be more specific about what the problem is?
My first thoughts are:
- When inserting children you are not signaling it (
beginInsertRows
/beginInsertRows
) - Run your model alongside the model test. The amount of problems it spots is invaluable
- When inserting children you are not signaling it (