Multiple columns in a QTree / QStandardItemModel

1.4k views Asked by At

I need your precious knowledge for my project ;) I need to make a tree with, at least, 3 columns. What I have is a list of "Actions" which is as following :

typedef struct Action
{
    int ID;
    int parentID;
    char* ident;
    char* text;
}

The "Actions" list (pListActions) is like below :

ID  ParentID     ident              text
1   0           "root"        "this is my root"
2   1           "element1"    "1st element"
3   1           "element2"    "2nd element"
4   0           "root2"       "this is another root"
...
...

And the corresponding Tree I can generate with my code :

ID      ident       text

1
|-2
|-3
4

As you can see I only have the first column but I need to have the other columns. I have tried with the setItem method but I don't know how to find the correct row ... In fact I need to "link" all the content of a row together; If I insert a new row I want to keep the link between the ID and the corresponding ident/text.

My code which generates the tree (the 1st column) :

QStandardItemModel *standardModel = new QStandardItemModel; //My model for the tree
standardModel->setColumnCount(3);
QStandardItem *rootNode = standardModel->invisibleRootItem();

for (auto it=std::begin(*this->pListActions);it!=std::end(*this->pListActions);it++) //I add all the elements in my list of actions
    {
        Action* pa = *it;
        QStandardItem *myNewItem= new QStandardItem(QString::number(pa->ID));   //The new item ID
        myNewItem->setCheckable(1);

    //Looking for a potential parent (with the action->parentID value
    //FindItemParent is the index of the element il the standardModel with the same ID as the current parentID (only one or zero because the ID is unique)
        QModelIndexList FindItemParent= standardModel->match(standardModel->index(0,0),Qt::DisplayRole,QVariant::fromValue(QString::number(pa->parentID)),2,Qt::MatchRecursive);

        if(!FindItemParent.empty())//If a parent exists
        {
            standardModel->itemFromIndex(FindItemParent.front())->appendRow(myNewItem);//add the current item to the parent in the standardModel

        }
        else {  //No parents
            rootNode->appendRow(myNewItem); //add the element to the root

        }
    }
    //drawing the tree
    QTreeView *tree = new QTreeView;    //Arbre affiché à l'aide du QTreeView
    tree->setModel(standardModel);
    tree->expandAll();
    tree->show();

And the final result I would like to have :

ID      ident       text
1       root        this is my root
|-2     element1    1st element
|-3     element2    2nd element
4       root2       this is another root
1

There are 1 answers

0
drlk On

I have finally found how to do it with a QTreeWidget which is easier. Here is how to do it :

QTreeWidget *finalTree = new QTreeWidget;   
finalTree->setColumnCount(3);
QTreeWidgetItem *root = new QTreeWidgetItem;    //Racine de la séquence
root->setText(0,"ROOT");
finalTree->addTopLevelItem(root);

for (auto it=std::begin(*this->pListActions);it!=std::end(*this->pListActions);it++)
{
    Action* pa = *it; 
        QTreeWidgetItem *myNewItem = new QTreeWidgetItem;   
        myNewItem->setText(0,QString::number(pa->ID)); 
        myNewItem->setText(1,pa->ident);
        myNewItem->setText(1,pa->type);


        QList<QTreeWidgetItem*> foundAParent;
        foundAParent = finalTree->findItems(QString::number(pa->parentID),Qt::MatchContains | Qt::MatchRecursive,0);
        if (!foundAParent.empty())
        {
            foundAParent.front()->addChild(myNewItem);
        }
        else
        {
            root->addChild(myNewItem);
        }
        finalTree->addTopLevelItem(root);
}


finalTree->show();