#include <QTreeWidget>
#include <QHeaderView>
{
// 实例化树结构
QTreeWidget *pTreeWidget = new QTreeWidget(this);
// 设置标头
pTreeWidget->header()->hide();
pTreeWidget->header()->setResizeMode(QHeaderView::ResizeToContents);
pTreeWidget->header()->setStretchLastSection(false);
// 开启滚动条
pTreeWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
pTreeWidget0>setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
// 去除光标
pTreeWidget->setFocusPolicy(Qt::NoFocus);
// 禁止编辑
pTreeWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
}
1、声明自定义数据类型
Q_DECLARE_METATYPE(CTest*);
EItemType
{
E_ITEM_ROOT = 0,
E_ITEM_NODE
}
2、添加自定义类型和数据
CTest *pTest = new CTest(this);
QTreeWidgetItem *pItem = new QTreeWidgetItem(QStringList(tr("测试")));
pItem->setData(0, Qt::UserRole, E_ITEM