Мне нужны ваши драгоценные знания для моего проекта;) Мне нужно сделать дерево, по крайней мере, с тремя столбцами. Что у меня есть список «Действия», который является следующим:Несколько столбцов в QTree/QStandardItemModel
typedef struct Action
{
int ID;
int parentID;
char* ident;
char* text;
}
В списке «Действия» (pListActions) является, как показано ниже:
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"
...
...
И соответствующее дерево можно произвести с мой код:
ID ident text
1
|-2
|-3
4
Как видите, у меня есть только первый столбец, но мне нужно иметь другие столбцы. Я попытался с помощью метода setItem, но я не знаю, как найти правильную строку ... На самом деле мне нужно «связать» все содержимое строки; Если я вставляю новую строку, я хочу сохранить связь между идентификатором и соответствующим идентификатором/текстом.
Мой код, который генерирует дерево (1-й столбец):
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();
И конечный результат я хотел бы иметь:
ID ident text
1 root this is my root
|-2 element1 1st element
|-3 element2 2nd element
4 root2 this is another root