Qt学习笔记之常用控件QTreeWidget

一、QTreeWidget Class

The QTreeWidget class provides a tree view that uses a predefined tree model. More...

Header:#include <QTreeWidget>
qmake:QT += widgets
Inherits:QTreeView

Properties

Public Functions

 QTreeWidget(QWidget *parent = nullptr)
virtual~QTreeWidget()
voidaddTopLevelItem(QTreeWidgetItem *item)
voidaddTopLevelItems(const QList<QTreeWidgetItem *> &items)
voidclosePersistentEditor(QTreeWidgetItem *item, int column = 0)
intcolumnCount() const
intcurrentColumn() const
QTreeWidgetItem *currentItem() const
voideditItem(QTreeWidgetItem *item, int column = 0)
QList<QTreeWidgetItem *>findItems(const QString &text, Qt::MatchFlags flags, int column = 0) const
QTreeWidgetItem *headerItem() const
intindexOfTopLevelItem(QTreeWidgetItem *item) const
voidinsertTopLevelItem(int index, QTreeWidgetItem *item)
voidinsertTopLevelItems(int index, const QList<QTreeWidgetItem *> &items)
QTreeWidgetItem *invisibleRootItem() const
boolisPersistentEditorOpen(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
voidopenPersistentEditor(QTreeWidgetItem *item, int column = 0)
voidremoveItemWidget(QTreeWidgetItem *item, int column)
QList<QTreeWidgetItem *>selectedItems() const
voidsetColumnCount(int columns)
voidsetCurrentItem(QTreeWidgetItem *item)
voidsetCurrentItem(QTreeWidgetItem *item, int column)
voidsetCurrentItem(QTreeWidgetItem *item, int column, QItemSelectionModel::SelectionFlags command)
voidsetHeaderItem(QTreeWidgetItem *item)
voidsetHeaderLabel(const QString &label)
voidsetHeaderLabels(const QStringList &labels)
voidsetItemWidget(QTreeWidgetItem *item, int column, QWidget *widget)
intsortColumn() const
voidsortItems(int column, Qt::SortOrder order)
QTreeWidgetItem *takeTopLevelItem(int index)
QTreeWidgetItem *topLevelItem(int index) const
inttopLevelItemCount() const
QRectvisualItemRect(const QTreeWidgetItem *item) const

Reimplemented Public Functions

virtual voidsetSelectionModel(QItemSelectionModel *selectionModel) override

Public Slots

voidclear()
voidcollapseItem(const QTreeWidgetItem *item)
voidexpandItem(const QTreeWidgetItem *item)
voidscrollToItem(const QTreeWidgetItem *item, QAbstractItemView::ScrollHint hint = EnsureVisible)

Signals

voidcurrentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
voiditemActivated(QTreeWidgetItem *item, int column)
voiditemChanged(QTreeWidgetItem *item, int column)
voiditemClicked(QTreeWidgetItem *item, int column)
voiditemCollapsed(QTreeWidgetItem *item)
voiditemDoubleClicked(QTreeWidgetItem *item, int column)
voiditemEntered(QTreeWidgetItem *item, int column)
voiditemExpanded(QTreeWidgetItem *item)
voiditemPressed(QTreeWidgetItem *item, int column)
voiditemSelectionChanged()

Protected Functions

virtual booldropMimeData(QTreeWidgetItem *parent, int index, const QMimeData *data, Qt::DropAction action)
QModelIndexindexFromItem(const QTreeWidgetItem *item, int column = 0) const
QTreeWidgetItem *itemFromIndex(const QModelIndex &index) const
virtual QMimeData *mimeData(const QList<QTreeWidgetItem *> items) const
virtual QStringListmimeTypes() const
virtual Qt::DropActionssupportedDropActions() const

Reimplemented Protected Functions

virtual voiddropEvent(QDropEvent *event) override
virtual boolevent(QEvent *e) override

Detailed Description

windows-treeview.pnguploading.4e448015.gif转存失败重新上传取消

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<QTreeWidgetItem *> 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 QTreeWidgetItemQTreeWidgetItemIteratorQTreeViewModel/View Programming, and Settings Editor Example.

二、QTreeWidget的一些基本操作

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<QTreeWidgetItem *> 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());
}

三、QTreeWidget例子

#ifndef TREEWIDGET_H 
#define TREEWIDGET_H 

#include <QtGui> 

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<QTreeWidgetItem *> rootList; 
        rootList << root; 
        tree->insertTopLevelItems(0, rootList); 
} 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值