该系列持续更新,喜欢请一键三连,感谢各位大佬。
QT5.14.2 参考官方QT助手
文章目录
- QTreeWidget Class
- properties
- Public Functions
- 默认构造函数
- 默认析构函数
- 添加根节点void addTopLevelItem(QTreeWidgetItem *item)
- 添加多个根节点void addTopLevelItems(const QList<QTreeWidgetItem *> &items)
- 关闭编辑void closePersistentEditor(QTreeWidgetItem *item, int column = 0)
- 获取列数int columnCount() const
- 返回当前选中列int currentColumn() const
- 返回当前选中QTreeWidgetItem *currentItem() const
- 没用void editItem(QTreeWidgetItem *item, int column = 0)
- 查找满足条件的item返回QList<QTreeWidgetItem *> findItems(const QString &text, Qt::MatchFlags flags, int column = 0) const
- 返回表头QTreeWidgetItem *headerItem() const
- 返回index索引根据item int indexOfTopLevelItem(QTreeWidgetItem *item) const
- 在指定索引位置插入新的top节点void insertTopLevelItem(int index, QTreeWidgetItem *item)
- 插入多个top节点void insertTopLevelItems(int index, const QList<QTreeWidgetItem *> &items)
- 递归访问所有根节点 返回一个模拟节点作为所有根节点的父节点QTreeWidgetItem *invisibleRootItem() const
- 节点是否为可编辑状态bool isPersistentEditorOpen(QTreeWidgetItem *item, int column = 0) const
- 不分层级获取上层相邻节点QTreeWidgetItem *itemAbove(const QTreeWidgetItem *item) const
- 根据坐标返回节点QTreeWidgetItem *itemAt(const QPoint &p) const
- 同上直接输入坐标QTreeWidgetItem *itemAt(int x, int y) const
- 返回显示出的下层节点QTreeWidgetItem *itemBelow(const QTreeWidgetItem *item) const
QTreeWidget Class
The QTreeWidget class provides a tree view that uses a predefined tree model. More…
QTreeWidget类提供了一个使用预定义树模型的树视图。
Header:#include
qmake:QT += widgets
Inherits:QTreeView
List of all members, including inherited members
所有成员的列表,包括继承的成员
Obsolete members
过时的成员
properties
columnCount : int
topLevelItemCount : const int
Public Functions
默认构造函数
QTreeWidget::QTreeWidget(QWidget *parent = nullptr)
示例:
// 创建 QTreeWidget 对象
QTreeWidget *tree = new QTreeWidget;
tree->setColumnCount(3); // 设置列数为 3
tree->setHeaderLabels({ "Name", "Value", "Data" }); // 设置列标签
tree->header()->setSectionResizeMode(QHeaderView::ResizeToContents); // 自适应列宽
默认析构函数
QTreeWidget::~QTreeWidget()
添加根节点void addTopLevelItem(QTreeWidgetItem *item)
示例:
// 创建根节点
QTreeWidgetItem *root = new QTreeWidgetItem(tree);
root->setText(0, "Root");
root->setText(1, "0");
root->setData(2, Qt::UserRole, "root data");
tree->addTopLevelItem(root);
添加多个根节点void addTopLevelItems(const QList<QTreeWidgetItem *> &items)
示例:
// 创建根节点
QTreeWidgetItem *root = new QTreeWidgetItem(tree);
root->setText(0, "Root");
root->setText(1, "0");
root->setData(2, Qt::UserRole, "root data");
//tree->addTopLevelItem(root);
QList<QTreeWidgetItem* > all_root_list;
all_root_list.push_back(root);
QTreeWidgetItem *root1 = new QTreeWidgetItem(tree);
root1->setText(0, "Root2");
all_root_list.push_back(root1);
tree->addTopLevelItems(all_root_list);
关闭编辑void closePersistentEditor(QTreeWidgetItem *item, int column = 0)
关闭item编辑器,如果是关闭的则此函数无效。
先打开看效果是这样的:
tree->openPersistentEditor(root);
然后再调用closePersistentEditor
tree->openPersistentEditor(root);
tree->closePersistentEditor(root);
获取列数int columnCount() const
qDebug() << "tree->columnCount:"<<tree->columnCount();
返回当前选中列int currentColumn() const
默认-1,点击选中哪列返回列号,索引从0开始。
qDebug() << "tree->currentColumn:" << tree->currentColumn();
connect(tree, &QTreeWidget::itemClicked, this, [=] {qDebug() << "tree->currentColumn:" << tree->currentColumn();
});
返回当前选中QTreeWidgetItem *currentItem() const
默认是NULL
QTreeWidgetItem* selectItem = tree->currentItem();
没用void editItem(QTreeWidgetItem *item, int column = 0)
查找满足条件的item返回QList<QTreeWidgetItem *> findItems(const QString &text, Qt::MatchFlags flags, int column = 0) const
查找包含字符串ro的item,以字符串不分大小写匹配(第二个参数是匹配规则为枚举类型详见qt官方文档说明)
QList<QTreeWidgetItem*> find_items = tree->findItems("ro",Qt::MatchContains);
for (auto item:find_items)
{qDebug() << item->text(0);
}
返回表头QTreeWidgetItem *headerItem() const
QTreeWidgetItem* hearderItem = tree->headerItem();
qDebug() << hearderItem->text(0);
qDebug() << hearderItem->text(1);
qDebug() << hearderItem->text(2);
qDebug() << hearderItem->text(3);
返回index索引根据item int indexOfTopLevelItem(QTreeWidgetItem *item) const
只能获取到top节点索引
qDebug() << tree->indexOfTopLevelItem(nullptr);
qDebug() << tree->indexOfTopLevelItem(root);
qDebug() << tree->indexOfTopLevelItem(child2);
qDebug() << tree->indexOfTopLevelItem(root1);
在指定索引位置插入新的top节点void insertTopLevelItem(int index, QTreeWidgetItem *item)
插入qstring超过列数则不显示
QStringList insertitem;
insertitem << "insertroot" << "i2" << "2133" << "122344";
tree->insertTopLevelItem(1, new QTreeWidgetItem(insertitem));
插入多个top节点void insertTopLevelItems(int index, const QList<QTreeWidgetItem *> &items)
QStringList insertitem;
insertitem << "insertroot" << "i2" << "2133";
QList<QTreeWidgetItem*> qtree_widget_list;
qtree_widget_list.push_back(new QTreeWidgetItem(insertitem));
insertitem.clear();
insertitem << "insertroot2";
qtree_widget_list.push_back(new QTreeWidgetItem(insertitem));
tree->insertTopLevelItems(0, qtree_widget_list);
递归访问所有根节点 返回一个模拟节点作为所有根节点的父节点QTreeWidgetItem *invisibleRootItem() const
以该形式遍历所有根节点:
QTreeWidgetItem* one = tree->invisibleRootItem();
int nums = one->childCount();
for (int i=0;i<nums;++i)
{qDebug() << one->child(i)->text(0);
}
节点是否为可编辑状态bool isPersistentEditorOpen(QTreeWidgetItem *item, int column = 0) const
获取item是否为可编辑状态,qt5.10之后版本才有这个函数
qDebug()<< tree->isPersistentEditorOpen(root);
tree->openPersistentEditor(root);
qDebug() << tree->isPersistentEditorOpen(root);
tree->closePersistentEditor(root);
qDebug() << tree->isPersistentEditorOpen(root);
不分层级获取上层相邻节点QTreeWidgetItem *itemAbove(const QTreeWidgetItem *item) const
只能获取到展开节点,所以开始之前先调用expandAll(),否则获取到的为显示的相邻节点。
若没有显示出来的上层节点则返回空指针。
tree->expandAll();
QTreeWidgetItem* t1 = tree->itemAbove(root);
if (t1)qDebug() << t1->text(0);
t1 = tree->itemAbove(child1);
if (t1)qDebug() << t1->text(0);
t1 = tree->itemAbove(root1);
if (t1)qDebug() << t1->text(0);
根据坐标返回节点QTreeWidgetItem *itemAt(const QPoint &p) const
树形控件内部坐标,左上角为0,0点,如果对应坐标没有节点返回NULL
QPoint* p1 = new QPoint(0, 0);
QTreeWidgetItem* t1 = tree->itemAt(*p1);
if (t1)qDebug() << t1->text(0);
QPoint* p2 = new QPoint(0, 40);
t1 = tree->itemAt(*p2);
if (t1)qDebug() << t1->text(0);
同上直接输入坐标QTreeWidgetItem *itemAt(int x, int y) const
同上:
返回显示出的下层节点QTreeWidgetItem *itemBelow(const QTreeWidgetItem *item) const
同itemAbove,只是取得下层节点,没有下层则返回空指针