garbart
garbart

Reputation: 485

QTreeWidgetItem with two parents

Can I somehow attach one QTreeWidgetItem to two (or more) nodes at once? Just like this:

parent1
 └child1
parent2
 └child1

If I just do addChild() on both parents, the child appears only on the first parent. Is that even possible? Or such result can be achieved only by completely copying of descendant?

Upvotes: 1

Views: 103

Answers (1)

garbart
garbart

Reputation: 485

The instructions say:

If the child has already been inserted somewhere else it won't be inserted again.

This means that only option is to copy the child completely. Roughly so:

QTreeWidgetItem* copy(QTreeWidgetItem* item)
{
    if (item == nullptr)
    {
        return nullptr;
    }

    QTreeWidgetItem* out = new QTreeWidgetItem(*item);
    for (int i = 0; i < item->childCount(); i++)
    {
        out->addChild(copy(item->child(i)));
    }

    return out;
}

Upvotes: 3

Related Questions