void buildTreeItem(QObjectList* list,QStandardItem* item)
{
foreach(auto object,*list)
{
auto current = new QStandardItem();
current->setText(object->objectName());
item->appendRow(current);
auto chidren = object->children();
if(!chidren.isEmpty())
buildTreeItem(&chidren,current);
}
}
void buildTree(QTreeView* view,const QList<QObject*>& list)
{
view->header()->hide();
view->setColumnHidden(0,true);
QStandardItemModel* model = new QStandardItemModel(view);
foreach(auto object,list)
{
auto item = new QStandardItem(object->objectName());
auto list = object->children();
buildTreeItem(&list,item);
model->appendRow(item);
}
view->setModel(model);
}
QObject* buildObjectTree(QObject* parent,const QString& text)
{
auto object = new QObject(parent);
object->setObjectName(text);
return object;
}
QWidget widget;
auto box = new QVBoxLayout();
widget.setLayout(box);
QTreeView* tree = new QTreeView(&widget);
box->addWidget(tree);
QList<QObject*> list;
auto object = buildObjectTree(nullptr,"1");
list.append(object);
buildObjectTree(object, "2");
buildObjectTree(object, "3");
object = buildObjectTree(object, "4");
buildObjectTree(object, "5");
buildObjectTree(object, "6");
object = buildObjectTree(object, "7");
buildObjectTree(object, "8");
buildObjectTree(object, "9");
buildObjectTree(object, "10");
buildObjectTree(object, "11");
buildTree(tree,list);
widget.show();