The QTreeWidget class provides a tree view that uses a predefined tree model. More...
Header: | #include |
qmake: | QT += widgets |
Inherits: | QTreeView |
QTreeWidget(QWidget *parent = nullptr) | |
virtual | ~QTreeWidget() |
void | addTopLevelItem(QTreeWidgetItem *item) |
void | addTopLevelItems(const QList |
void | closePersistentEditor(QTreeWidgetItem *item, int column = 0) |
int | columnCount() const |
int | currentColumn() const |
QTreeWidgetItem * | currentItem() const |
void | editItem(QTreeWidgetItem *item, int column = 0) |
QList |
findItems(const QString &text, Qt::MatchFlags flags, int column = 0) const |
QTreeWidgetItem * | headerItem() const |
int | indexOfTopLevelItem(QTreeWidgetItem *item) const |
void | insertTopLevelItem(int index, QTreeWidgetItem *item) |
void | insertTopLevelItems(int index, const QList |
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 |
QWidget * | itemWidget(QTreeWidgetItem *item, int column) const |
void | openPersistentEditor(QTreeWidgetItem *item, int column = 0) |
void | removeItemWidget(QTreeWidgetItem *item, int column) |
QList |
selectedItems() const |
void | setColumnCount(int columns) |
void | setCurrentItem(QTreeWidgetItem *item) |
void | setCurrentItem(QTreeWidgetItem *item, int column) |
void | setCurrentItem(QTreeWidgetItem *item, int column, QItemSelectionModel::SelectionFlags command) |
void | setHeaderItem(QTreeWidgetItem *item) |
void | setHeaderLabel(const QString &label) |
void | setHeaderLabels(const QStringList &labels) |
void | setItemWidget(QTreeWidgetItem *item, int column, QWidget *widget) |
int | sortColumn() const |
void | sortItems(int column, Qt::SortOrder order) |
QTreeWidgetItem * | takeTopLevelItem(int index) |
QTreeWidgetItem * | topLevelItem(int index) const |
int | topLevelItemCount() const |
QRect | visualItemRect(const QTreeWidgetItem *item) const |
virtual void | setSelectionModel(QItemSelectionModel *selectionModel) override |
void | clear() |
void | collapseItem(const QTreeWidgetItem *item) |
void | expandItem(const QTreeWidgetItem *item) |
void | scrollToItem(const QTreeWidgetItem *item, QAbstractItemView::ScrollHint hint = EnsureVisible) |
void | currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous) |
void | itemActivated(QTreeWidgetItem *item, int column) |
void | itemChanged(QTreeWidgetItem *item, int column) |
void | itemClicked(QTreeWidgetItem *item, int column) |
void | itemCollapsed(QTreeWidgetItem *item) |
void | itemDoubleClicked(QTreeWidgetItem *item, int column) |
void | itemEntered(QTreeWidgetItem *item, int column) |
void | itemExpanded(QTreeWidgetItem *item) |
void | itemPressed(QTreeWidgetItem *item, int column) |
void | itemSelectionChanged() |
virtual bool | dropMimeData(QTreeWidgetItem *parent, int index, const QMimeData *data, Qt::DropAction action) |
QModelIndex | indexFromItem(const QTreeWidgetItem *item, int column = 0) const |
QTreeWidgetItem * | itemFromIndex(const QModelIndex &index) const |
virtual QMimeData * | mimeData(const QList |
virtual QStringList | mimeTypes() const |
virtual Qt::DropActions | supportedDropActions() const |
virtual void | dropEvent(QDropEvent *event) override |
virtual bool | event(QEvent *e) override |
The QTreeWidget class is a convenience class that provides a standard tree widget with a classic item-based interface similar to that used by the QListView class in Qt 3. This class is based on Qt's Model/View architecture and uses a default model to hold items, each of which is a QTreeWidgetItem.
Developers who do not need the flexibility of the Model/View framework can use this class to create simple hierarchical lists very easily. A more flexible approach involves combining a QTreeView with a standard item model. This allows the storage of data to be separated from its representation.
In its simplest form, a tree widget can be constructed in the following way:
QTreeWidget *treeWidget = new QTreeWidget();
treeWidget->setColumnCount(1);
QList items;
for (int i = 0; i < 10; ++i)
items.append(new QTreeWidgetItem((QTreeWidget*)0, QStringList(QString("item: %1").arg(i))));
treeWidget->insertTopLevelItems(0, items);
Before items can be added to the tree widget, the number of columns must be set with setColumnCount(). This allows each item to have one or more labels or other decorations. The number of columns in use can be found with the columnCount() function.
The tree can have a header that contains a section for each column in the widget. It is easiest to set up the labels for each section by supplying a list of strings with setHeaderLabels(), but a custom header can be constructed with a QTreeWidgetItem and inserted into the tree with the setHeaderItem() function.
The items in the tree can be sorted by column according to a predefined sort order. If sorting is enabled, the user can sort the items by clicking on a column header. Sorting can be enabled or disabled by calling setSortingEnabled(). The isSortingEnabled() function indicates whether sorting is enabled.
See also QTreeWidgetItem, QTreeWidgetItemIterator, QTreeView, Model/View Programming, and Settings Editor Example.
1.insertTopLevelItems,insertTopLevelItem用来添加顶层的item
QTreeWidget *treeWidget = new QTreeWidget(this);//创建
treeWidget->setColumnCount(1); //设置列
treeWidget->setHeaderLabel(tr("item tree")); //设置标题
treeWidget->setGeometry(0,0,this->width(),this->height()); //设置大小
QList items;
//创建两个节点
QTreeWidgetItem *fItem1 = new QTreeWidgetItem(treeWidget,QStringList(QString("f1")));
QTreeWidgetItem *fItem2 = new QTreeWidgetItem(treeWidget,QStringList(QString("f2")));
items.append(fItem1);
items.append(fItem2);
//添加顶层节点
treeWidget->insertTopLevelItems(0,items);
//节点始终保持展开
treeWidget->setItemsExpandable(false);
treeWidget->expandAll();
2.父节点通过addChild增加子节点
//创建子节点
QTreeWidgetItem *fItem1a = new QTreeWidgetItem(fItem1,QStringList(QString("f1a")));
QTreeWidgetItem *fItem1b = new QTreeWidgetItem(fItem1,QStringList(QString("f1b")));
QTreeWidgetItem *fItem2a = new QTreeWidgetItem(fItem2,QStringList(QString("f2a")));
QTreeWidgetItem *fItem2b = new QTreeWidgetItem(fItem2,QStringList(QString("f2b")));
//添加字节点
fItem1->addChild(fItem1a);
fItem1->addChild(fItem1b);
fItem2->addChild(fItem2a);
fItem2->addChild(fItem2b);
3.建立鼠标点击右键点击信号检测
//检测点击事件
connect(treeWidget,SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(itemClick(QTreeWidgetItem*,int)));
//检测鼠标右键
treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
connect(treeWidget,SIGNAL(customContextMenuRequested(const QPoint&)), this,SLOT(popMenu(const QPoint&)));
4.槽函数
void Widget::itemClick(QTreeWidgetItem *item, int column)
{
QTreeWidgetItem *parent = item->parent(); //获取父节点
if(NULL==parent) //注意:最顶端项是没有父节点的
return;
int col = parent->indexOfChild(item); //item在父项中的节点行号(从0开始)
}
//弹出菜单
void Widget::popMenu(const QPoint &)
{
QTreeWidgetItem* curItem=treeWidget->currentItem(); //**获取当前被点击的节点
if(curItem == NULL || curItem->parent() == NULL)
return; //右键的位置在空白位置右击或者点击的是顶层item
//创建一个action
QAction deleteItem(QString::fromLocal8Bit("&删除"),this);
connect(&deleteItem, SIGNAL(triggered()), this, SLOT(deleteItem()));
QPoint pos;
//创建一个菜单栏
QMenu menu(treeWidget);
menu.addAction(&deleteItem);
menu.exec(QCursor::pos()); //在当前鼠标位置显示
}
//删除item
void Widget::deleteItem()
{
QTreeWidgetItem* curItem=treeWidget->currentItem();
curItem->parent()->removeChild(treeWidget->currentItem());
}
#ifndef TREEWIDGET_H
#define TREEWIDGET_H
#include
class TreeWidget : public QWidget
{
public:
TreeWidget();
private:
QTreeWidget *tree;
};
#endif // TREEWIDGET_H
#include "treewidget.h"
TreeWidget::TreeWidget()
{
tree = new QTreeWidget(this);
tree->setColumnCount(1);
QTreeWidgetItem *root = new QTreeWidgetItem(tree, QStringList(QString("Root")));
QTreeWidgetItem *leaf = new QTreeWidgetItem(root, QStringList(QString("Leaf 1")));
root->addChild(leaf);
QTreeWidgetItem *leaf2 = new QTreeWidgetItem(root, QStringList(QString("Leaf 1")));
leaf2->setCheckState(0, Qt::Checked);
root->addChild(leaf2);
QList rootList;
rootList << root;
tree->insertTopLevelItems(0, rootList);
}