[Solved] How to track hierarchy of a QTreeWidget items ?



  • Hi,
    I use drag-drop mechanism to generate a QTreeWidget. Now I want to save the hierarchy of the items. I use code below to traverse the tree.

    @
    void Widget::saveProject()
    {
    int numberOfTopLevelItems = flowTreeView->topLevelItemCount();
    for ( int topLevelindex=0 ; topLevelindex < numberOfTopLevelItems ; topLevelindex++)
    {
    QTreeWidgetItem *item = flowTreeView->topLevelItem(topLevelindex);
    qDebug() << item->text(0) ;
    processItem(item);
    }
    }

    void Widget::processItem(QTreeWidgetItem * parent)
    {
    for (int childIndex = 0 ; childIndex < parent->childCount(); childIndex++)
    {
    QTreeWidgetItem *child = parent->child(childIndex);
    qDebug() << child->text(0);
    processItem(child);
    }
    }
    @

    I want to achieve the following :
    Tree
    @
    A
    |---B
    |---C
    |---D
    |---E
    |---F
    |---G
    @
    Element -> Hierarchy level
    A->0
    B->1
    C->2
    D->2
    E->3
    F->2
    G->1

    I am aware I can get the parent() information but how to track the hierarchy .... Even the following solution will help
    A -> B
    A -> B -> C
    A -> B -> D etc.

    Please help. How should I implement it ?



  • @void Widget::saveProject()
    {
    int numberOfTopLevelItems = flowTreeView->topLevelItemCount();
    for ( int topLevelindex=0 ; topLevelindex < numberOfTopLevelItems ; topLevelindex++)
    {
    QTreeWidgetItem *item = flowTreeView->topLevelItem(topLevelindex);
    qDebug() << item->text(0);
    processItem(item);
    }
    }

    void Widget::processItem(QTreeWidgetItem * parent)
    {
    static int level = 0;
    level += 4;
    for (int childIndex = 0 ; childIndex < parent->childCount(); childIndex++)
    {
    QTreeWidgetItem *child = parent->child(childIndex);
    qDebug() << QString( " " ).repeated( level ) << child->text(0);
    processItem(child);
    }
    level -= 4;
    }@

    Something like this (I didn't test this code so it may contain errors) though I would not recommend using of static variable. You can change processItem signature to pass level as an argument.

    If you want to print child not with spaces indicating level but with all parents you can change "int level" to "QString parents" and change it in the same way as level.



  • I advocate against a static variable, that's not needed. Just pass the current level to you processItem method:

    @
    void Widget::saveProject()
    {
    int numberOfTopLevelItems = flowTreeView->topLevelItemCount();
    for ( int topLevelindex=0 ; topLevelindex < numberOfTopLevelItems ; topLevelindex++)
    {
    QTreeWidgetItem *item = flowTreeView->topLevelItem(topLevelindex);
    qDebug() << "#" << 0 << "#" << item->text(0);
    processItem(item, 1);
    }
    }

    void Widget::processItem(QTreeWidgetItem * parent, int level)
    {
    for (int childIndex = 0 ; childIndex < parent->childCount(); childIndex++)
    {
    QTreeWidgetItem *child = parent->child(childIndex);
    qDebug() << "#" << level << "#" << child->text(0);
    processItem(child, level + 1);
    }
    }
    @

    BTW:
    To save your items state, you can use "QTreeWidgetItem::write":/doc/qt-4.8/qtreewidgetitem.html#write or QDataStream and operator<<.



  • Perfect :) Thank you p-himik and Volker.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.