QT6 源(162)模型视图架构里的树表窗体视图 QTreeWidget 的条目 QTreeWidgetItem:本条目只有公共成员函数,没有属性与槽函数。以及本类的源代码带注释

(1) 我们以此程序为例,学习树形视图的使用

在这里插入图片描述

++汉语引起阅读性变差, 无法理解汉字的 unicode 编码 。换这个得了

在这里插入图片描述

(2) 这是本程序的 ui_mainwindow . h 头文件,最标准的,编译器生成的 UI 代码,以此学习树表视图的成员函数的使用

#ifndef UI_MAINWINDOW_H
#define UI_MAINWINDOW_H

#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QTreeWidget>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>

QT_BEGIN_NAMESPACE

class Ui_MainWindow
{
public:
    QWidget     * centralwidget   ;     //这没有采用 QWidget,而是 QMainWindow,
    QVBoxLayout * verticalLayout_2;     //所以布局麻烦了一些。
    QPushButton * pushButton;
    QTreeWidget * treeWidget;

    void setupUi(QMainWindow *MainWindow)
    {
        if (MainWindow->objectName().isEmpty())
            MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
        MainWindow->resize(229, 253);   //成成主窗体,并采用垂直布局
        centralwidget = new QWidget(MainWindow);
        centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
        verticalLayout_2 = new QVBoxLayout(centralwidget);
        verticalLayout_2->setObjectName(QString::fromUtf8("verticalLayout_2"));

        pushButton = new QPushButton(centralwidget);  //生成按钮,并加入布局
        pushButton->setObjectName(QString::fromUtf8("pushButton"));
        verticalLayout_2->addWidget(pushButton);

        treeWidget = new QTreeWidget(centralwidget);  //生成树形窗体
        //老师讲过,树形窗体里的一行是一个条目,一个单元。

        QTreeWidgetItem * __qtreewidgetitem  = new QTreeWidgetItem(treeWidget);

        QTreeWidgetItem * __qtreewidgetitem1 = new QTreeWidgetItem(__qtreewidgetitem);
        new QTreeWidgetItem(__qtreewidgetitem1);
        new QTreeWidgetItem(__qtreewidgetitem1);

        QTreeWidgetItem * __qtreewidgetitem2 = new QTreeWidgetItem(__qtreewidgetitem);
        new QTreeWidgetItem(__qtreewidgetitem2);
        new QTreeWidgetItem(__qtreewidgetitem2);

        //这是分水岭。***********************************************
        //以上是为 item  加入了 item1 和 item2  **********************
        //以下是为 item3 加入了 item4 和 item5  **********************

        QTreeWidgetItem * __qtreewidgetitem3 = new QTreeWidgetItem(treeWidget);

        QTreeWidgetItem * __qtreewidgetitem4 = new QTreeWidgetItem(__qtreewidgetitem3);
        new QTreeWidgetItem(__qtreewidgetitem4);
        new QTreeWidgetItem(__qtreewidgetitem4);

        QTreeWidgetItem * __qtreewidgetitem5 = new QTreeWidgetItem(__qtreewidgetitem3);
        new QTreeWidgetItem(__qtreewidgetitem5);
        new QTreeWidgetItem(__qtreewidgetitem5);

        treeWidget->setObjectName(QString::fromUtf8("treeWidget"));

        verticalLayout_2->addWidget(treeWidget); //树表窗体被加入布局

        MainWindow->setCentralWidget(centralwidget); //进程窗体接收了中心窗体

        retranslateUi(MainWindow);

        QMetaObject::connectSlotsByName(MainWindow); //完善信号与槽
    } // setupUi

    void retranslateUi(QMainWindow *MainWindow)
    {
        MainWindow->setWindowTitle(
            QCoreApplication::translate("MainWindow", "T"         , nullptr));
        pushButton->setText(
            QCoreApplication::translate("MainWindow", "PushButton", nullptr));

        QTreeWidgetItem * ___qtreewidgetitem = treeWidget->headerItem(); //视图的头部条目
        ___qtreewidgetitem->setText(
            0, QCoreApplication::translate("MainWindow", "zone"   , nullptr));
        ___qtreewidgetitem->setText(
            1, QCoreApplication::translate("MainWindow", "bigShow", nullptr));

        const bool __sortingEnabled = treeWidget->isSortingEnabled(); //修改表前取消视图的排序
        treeWidget->setSortingEnabled(false);

        QTreeWidgetItem * ___qtreewidgetitem1 = treeWidget->topLevelItem(0); //顶层河南条目
        ___qtreewidgetitem1->setText(
            0, QCoreApplication::translate("MainWindow", "henan", nullptr));
        ___qtreewidgetitem1->setText(
            1, QCoreApplication::translate("MainWindow", "HENAN", nullptr));

        QTreeWidgetItem * ___qtreewidgetitem2 = ___qtreewidgetitem1->child(0);//完善郑州条目
        ___qtreewidgetitem2->setText(
            1, QCoreApplication::translate("MainWindow", "ZHENGZHOU", nullptr));
        ___qtreewidgetitem2->setText(
            0, QCoreApplication::translate("MainWindow", "zhengzhou", nullptr));

        QTreeWidgetItem * ___qtreewidgetitem3 = ___qtreewidgetitem2->child(0); //完善新郑县
        ___qtreewidgetitem3->setText(
            1, QCoreApplication::translate("MainWindow", "XINZHENG", nullptr));
        ___qtreewidgetitem3->setText(
            0, QCoreApplication::translate("MainWindow", "xinzheng", nullptr));

        QTreeWidgetItem * ___qtreewidgetitem4 = ___qtreewidgetitem2->child(1); //完善金水区
        ___qtreewidgetitem4->setText(
            1, QCoreApplication::translate("MainWindow", "JINSHUI", nullptr));
        ___qtreewidgetitem4->setText(
            0, QCoreApplication::translate("MainWindow", "jinshui", nullptr));

        QTreeWidgetItem *___qtreewidgetitem5 = ___qtreewidgetitem1->child(1); //完善周口市
        ___qtreewidgetitem5->setText(
            1, QCoreApplication::translate("MainWindow", "ZHOUKOU", nullptr));
        ___qtreewidgetitem5->setText(
            0, QCoreApplication::translate("MainWindow", "zhoukou", nullptr));

        QTreeWidgetItem *___qtreewidgetitem6 = ___qtreewidgetitem5->child(0); //完善西华县
        ___qtreewidgetitem6->setText(
            1, QCoreApplication::translate("MainWindow", "XIHUA", nullptr));
        ___qtreewidgetitem6->setText(
            0, QCoreApplication::translate("MainWindow", "xihua", nullptr));

        QTreeWidgetItem *___qtreewidgetitem7 = ___qtreewidgetitem5->child(1); //完善商水县
        ___qtreewidgetitem7->setText(
            1, QCoreApplication::translate("MainWindow", "SHANGSHUI", nullptr));
        ___qtreewidgetitem7->setText(
            0, QCoreApplication::translate("MainWindow", "shangshui", nullptr));

        QTreeWidgetItem * ___qtreewidgetitem8 = treeWidget->topLevelItem(1);  //山东省
        ___qtreewidgetitem8->setText(
            1, QCoreApplication::translate("MainWindow", "SHANDONG", nullptr));
        ___qtreewidgetitem8->setText(
            0, QCoreApplication::translate("MainWindow", "shandong", nullptr));

        QTreeWidgetItem * ___qtreewidgetitem9 = ___qtreewidgetitem8->child(0); //日照市
        ___qtreewidgetitem9->setText(
            1, QCoreApplication::translate("MainWindow", "RIZHAO", nullptr));
        ___qtreewidgetitem9->setText(
            0, QCoreApplication::translate("MainWindow", "rizhao", nullptr));

        QTreeWidgetItem * ___qtreewidgetitem10 = ___qtreewidgetitem9->child(0); //岚山区
        ___qtreewidgetitem10->setText(
            1, QCoreApplication::translate("MainWindow", "LANSHAN", nullptr));
        ___qtreewidgetitem10->setText(
            0, QCoreApplication::translate("MainWindow", "lanshan", nullptr));

        QTreeWidgetItem * ___qtreewidgetitem11 = ___qtreewidgetitem9->child(1); //莒县
        ___qtreewidgetitem11->setText(
            1, QCoreApplication::translate("MainWindow", "JUXIAN", nullptr));
        ___qtreewidgetitem11->setText(
            0, QCoreApplication::translate("MainWindow", "juxian", nullptr));

        QTreeWidgetItem * ___qtreewidgetitem12 = ___qtreewidgetitem8->child(1); //济南市
        ___qtreewidgetitem12->setText(
            1, QCoreApplication::translate("MainWindow", "JINAN", nullptr));
        ___qtreewidgetitem12->setText(
            0, QCoreApplication::translate("MainWindow", "jinan", nullptr));

        QTreeWidgetItem * ___qtreewidgetitem13 = ___qtreewidgetitem12->child(0);//章丘县
        ___qtreewidgetitem13->setText(
            1, QCoreApplication::translate("MainWindow", "ZHANGQIU", nullptr));
        ___qtreewidgetitem13->setText(
            0, QCoreApplication::translate("MainWindow", "zhangqiu", nullptr));

        QTreeWidgetItem * ___qtreewidgetitem14 = ___qtreewidgetitem12->child(1);//莱芜区
        ___qtreewidgetitem14->setText(
            1, QCoreApplication::translate("MainWindow", "LAIWU", nullptr));
        ___qtreewidgetitem14->setText(
            0, QCoreApplication::translate("MainWindow", "laiwu", nullptr));

        treeWidget->setSortingEnabled(__sortingEnabled); //恢复树表的排序设置

    } // retranslateUi

};

namespace Ui {
    class MainWindow: public Ui_MainWindow {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_MAINWINDOW_H

(3) 上面的 setupUi 函数里的指针太多,绕来绕去,这些树表条目指针搭建的是这样的结构

在这里插入图片描述

(4)以上面的例子框架,来学习本树表窗体及其条目的使用先是构造函数,本类没有定义属性

在这里插入图片描述

++测试一下

在这里插入图片描述

++以及

在这里插入图片描述

++以及

在这里插入图片描述

++测试一下

在这里插入图片描述

++以及 copy构造函数

在这里插入图片描述

(5)继续这是全局函数,但打印的关于条目的有效信息很少

在这里插入图片描述

(6)继续成员函数

在这里插入图片描述

++测试一下

在这里插入图片描述

++以及

在这里插入图片描述

++以及

在这里插入图片描述

(7)

在这里插入图片描述

(8)

在这里插入图片描述

++测试一下

在这里插入图片描述

(9)

在这里插入图片描述

++测试一下

在这里插入图片描述

(10)最后的一些非公开的成员函数

在这里插入图片描述

(11)本源代码定义于 qtreewidget . h

/*
The QTreeWidgetItem class provides an item for use with the
    QTreeWidget convenience class.

Detailed Description  :
树状图小部件项用于承载树状图小部件的信息行。   //这里的意思是 QTreeWidgetItem代表树表中的一行
这些行通常包含多列数据,每列可以包含一个文本标签和一个图标

QTreeWidgetItem 类是一个方便的类,它取代了 Qt3中的 QListViewItem类。
Qt提供了一个用于 QTreeWidget 类的项目。

项目通常由一个父级构造,
该父级是QTreeWidget(用于顶级项目)或QTreeWidgetItem(用于树较低级别的项目)。
例如,以下代码构造一个顶级项来表示世界城市,并为 0slo 添加一个条目作为子项:
     QTreeWidgetItem * cities = new QTreeWidgetItem(treeWidget);
     cities->setText(0, tr("Cities"));
     QTreeWidgetItem * osloItem = new QTreeWidgetItem(cities);
     osloItem->setText(0, tr("Oslo"));
     osloItem->setText(1, tr("Yes"));

可以通过指定它们在构建时的后续项目来按特定顺序添加项目:
     QTreeWidgetItem * planets = new QTreeWidgetItem(treeWidget, cities);
     planets->setText(0, tr("Planets"));

-个项中的每一列都可以有自己的背景刷,这通过`setBackground ()、函数来设置。
当前的背景刷可以通过background ()、函数来获取。
每一列的文本标签可以使用各自的字体和刷子来呈现,
    这些通过'setFont ()和setForeground()、函数进行指定,
    并通过`font()`和`foreground()'函数来读取。

顶级项与树中较低级别的项之间的主要区别在于,顶级项没有“parent()”属性。
这一信息可用于区分项,并且在从树中插入或移除项时非常有用。
一个项的子项可以通过调用“takeChild()”移除,
    而通过调用“insertChild()”函数可以在子项列表中的指定位置插入子项。

默认情况下,项目被启用,可被选择,可被勾选,并且可以成为拖放操作的源。
每个项目的标志可以通过调用setFlags()函数并传入相应的值(见Qt::ItemFlags)来更改。
可勾选的项目可以通过调用setCheckState()函数进行勾选和取消勾选。
相应的checkState()函数则指示该项目当前是否被勾选。

Subclassing :
当子类化 QTreeWidgetItem 以提供自定义项时,可以为它们定义新类型,以便将它们与标准项区分开来。
需要此功能的子类的构造函数需要使用等于或大于 UserType 的新类型值调用基类构造函数。

*/

class Q_WIDGETS_EXPORT QTreeWidgetItem
{
    friend class QTreeModel;
    friend class QTreeWidget;
    friend class QTreeWidgetPrivate;
    friend class QTreeWidgetItemIterator;
    friend class QTreeWidgetItemPrivate;

private:
    //void sortChildren(int column, Qt::SortOrder order, bool climb);

    //void executePendingSort() const;  //无注释

    QVariant childrenCheckState(int column) const;

    void itemChanged();

    QTreeModel * treeModel(QTreeWidget * v = nullptr) const;

    QTreeWidgetItemPrivate        * d        ;
    int                             rtti     ; //本条目的类型 type信息
    Qt::ItemFlags                   itemFlags; //本条目的操作标志
    // One item has a vector of column entries.
    //  Each column has a vector of (role, value) pairs.
    QList<QList<QWidgetItemData>>   values   ; //本条目里的所有数据,各列以及各列里的数据。
    QTreeWidget                   * view     ; //本条目所在的树窗体视图
    QTreeWidgetItem               * par      ; //本条目的父节点
    QList<QTreeWidgetItem *>        children ; //本条目的孩子们

public:
    //这个枚举描述了用于描述树形小部件项的类型。
    //你可以在QTreeWidgetltem子类中定义新的用户类型,以确保自定义项目得到特殊处理;
    //例如,在对项目进行排序时。
    enum ItemType {
        Type     =    0, //The default type for tree widget items.
        UserType = 1000  //自定义类型的最小值。低于UserType的值由Qt保留。
    };

    //Constructs a tree widget item of the specified type.
    //The item must be inserted into a tree widget.
    explicit             //创建树条目,具有类型 type。类型的默认值是枚举量 0.
    QTreeWidgetItem(                                      int type = Type);

    //Constructs a tree widget item of the specified type.
    //The item must be inserted into a tree widget.
    //The given list of strings will be set as the
    //  item text for each column in the item.
    //本条目里各列的文本由形参 strings提供。
    explicit  //一个 QTreeWidgetItem是一行,包含多个列。故需要 QStringList。
    QTreeWidgetItem(const   QStringList       & strings , int type = Type);

    //Constructs a tree widget item of the specified type and
    //  appends it to the items in the given parent.
    explicit             //创建一个条目并追加到树表 parent的末尾
    QTreeWidgetItem(        QTreeWidget       * parent, int type = Type);

    //Constructs a tree widget item of the specified type and
    //  inserts it into the given parent after the preceding item.
    //创建顶层条目,但要插在 preceding条目的后面。
    QTreeWidgetItem(        QTreeWidget       * parent,
                            QTreeWidgetItem   * preceding,int type = Type);

    //Constructs a tree widget item of the specified type and appends it to the
    //  items in the given parent. The given list of strings will be set as the
    //  item text for each column in the item. //在表 parent里追加一行条目
    //本构造函数说明了可以在构造条目的同时将其放入树表里,一举两得,但只能是追加条目。
    QTreeWidgetItem(        QTreeWidget       * parent,
                    const   QStringList       & strings , int type = Type);

    //Constructs a tree widget item and append it to the given parent.
    explicit        //往子树表 parent里添加条目
    QTreeWidgetItem(        QTreeWidgetItem   * parent  , int type = Type);

    //Constructs a tree widget item of the specified type that is inserted into the
    //  parent after the preceding child item. //往子表里的 preceding后面插入条目
    QTreeWidgetItem(        QTreeWidgetItem   * parent  ,
                            QTreeWidgetItem   * preceding,int type = Type);

    //Constructs a tree widget item and append it to the given parent.
    //The given list of strings will be set as the
    //  item text for each column in the item. //往子表添加条目并指定了新条目的列名
    QTreeWidgetItem(        QTreeWidgetItem   * parent  ,
                    const   QStringList       & strings , int type = Type);

    //Constructs a copy of other. Note that type() and treeWidget() are not copied.
    //This function is useful when reimplementing clone(). //不会指定新条目的类型与父类
    QTreeWidgetItem(const   QTreeWidgetItem   & other);         //copy 构造函数

    QTreeWidgetItem & operator=(const QTreeWidgetItem & other); //copy赋值运算符函数


    virtual ~QTreeWidgetItem(); //虚析构函数

//**************************************************************************
//*************************以下专注于条目的属性操作*****************************
//**************************************************************************

/*
enum ItemFlag { //这是描述 QTableWidget 表格等窗体中的条目的属性
    NoItemFlags          =   0, //It does not have any properties set.
    ItemIsSelectable     =   1, //It can be selected.
    ItemIsEditable       =   2, //It can be edited.
    ItemIsDragEnabled    =   4, //It can be dragged.
    ItemIsDropEnabled    =   8, //It can be used as a drop target.
    ItemIsUserCheckable  =  16, //可由用户更改复选状态
                                //It can be checked or unchecked by the user.
    ItemIsEnabled        =  32, //The user can interact with the item.
    ItemIsAutoTristate   =  64, //该项目的状态取决于其子项的状态。这使得在 QTreeWidget 中
        //可以自动管理父项的状态(如果所有子项均被选中,则父项也被选中 checked;
        //如果所有子项均未被选中,则父项未被选中 unchecked;
        //如果只有部分子项被选中,则父项处于部分选中状态 partially checked )。

    ItemNeverHasChildren = 128, //The item never has child items.
                                //This is used for optimization purposes only.

    ItemIsUserTristate   = 256  //The user can cycle through three separate states.
};
Q_DECLARE_FLAGS(ItemFlags, ItemFlag)     // ItemFlags = QFlags<ItemFlag>
Q_DECLARE_OPERATORS_FOR_FLAGS(ItemFlags) //这俩宏以完善对枚举量的运算定义
*/
    Qt::ItemFlags             flags() const;
    void                   setFlags(Qt::ItemFlags flags);


    bool                    isSelected() const; //对应枚举值 1
    void                   setSelected(bool select);

    inline
    bool                    isDisabled() const  //对应枚举值 4
    {   return !( flags() & Qt::ItemIsEnabled ); }
    inline
    void                   setDisabled(bool disabled)
    {
        setFlags(
                disabled
                ? ( flags() & ~Qt::ItemIsEnabled )
                :   flags() |  Qt::ItemIsEnabled);
    }


//**************************************************************************
//*******************以上是条目的属性设置,以下是本条目的数据************************
//**************************************************************************

    virtual
    QVariant                  data(int column, int role) const;
    virtual
    void                   setData(int column, int role, const QVariant & value);

    inline
    QString                   text(int column) const         //角色 0
    {   return data(column, Qt::DisplayRole).toString(); }
    inline
    void                   setText(int column, const QString & text)
    {   setData(column, Qt::DisplayRole, text); }

    inline
    QIcon                     icon(int column) const         //角色 1
    {   return qvariant_cast<QIcon>(data(column, Qt::DecorationRole)); }
    inline
    void                   setIcon(int column, const QIcon & icon)
    {   setData(column, Qt::DecorationRole, icon); }

    inline
    QString                   toolTip(int column) const       //角色 3
    {   return data(column, Qt::ToolTipRole).toString(); }
    inline
    void                   setToolTip(int column, const QString & toolTip)
    {   setData(column, Qt::ToolTipRole, toolTip); }

    inline
    QString                   statusTip(int column) const     //角色 4
    {   return data(column, Qt::StatusTipRole).toString(); }
    inline
    void                   setStatusTip(int column, const QString & statusTip)
    {   setData(column, Qt::StatusTipRole, statusTip); }


    inline
    QString                   whatsThis(int column) const     //角色 5
    {   return data(column, Qt::WhatsThisRole).toString(); }
    inline
    void                   setWhatsThis(int column, const QString & whatsThis)
    {   setData(column, Qt::WhatsThisRole, whatsThis); }


    inline
    QFont                     font(int column) const          //角色 6
    {   return qvariant_cast<QFont>(data(column, Qt::FontRole)); }
    inline
    void                   setFont(int column, const QFont & font)
    {   setData(column, Qt::FontRole, font); }

    inline
    int                       textAlignment(int column) const //角色 7
    { return data(column, Qt::TextAlignmentRole).toInt(); }
    inline
    void                   setTextAlignment(int column, int alignment)
    { setData(column, Qt::TextAlignmentRole, alignment); }


    inline
    QBrush                    background(int column) const    //角色 8
    {   return qvariant_cast<QBrush>(data(column, Qt::BackgroundRole)); }
    inline
    void                   setBackground(int column, const QBrush &brush)
    {
        setData(column,
                Qt::BackgroundRole,
                brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant());
    }

    inline
    QBrush                    foreground(int column) const    //角色 9
    {   return qvariant_cast<QBrush>(data(column, Qt::ForegroundRole)); }
    inline
    void                   setForeground(int column, const QBrush & brush)
    {
        setData(column,
                Qt::ForegroundRole,
                brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant());
    }

    inline                    //检查本条目里某一列的复选属性
    Qt::CheckState            checkState(int column) const  //角色10
    {
        return static_cast<Qt::CheckState>(data(column, Qt::CheckStateRole).toInt());
    }
    inline
    void                   setCheckState(int column, Qt::CheckState state)
    {   setData(column, Qt::CheckStateRole, state); }


    inline
    QSize                     sizeHint(int column) const    //角色13
    {   return qvariant_cast<QSize>(data(column, Qt::SizeHintRole)); }
    inline
    void                   setSizeHint(int column, const QSize & size)
    {
        setData(column,
                Qt::SizeHintRole,
                size.isValid() ? QVariant(size) : QVariant());
    }

//**************************************************************************
//************************以下开始本条目的别的成员函数***************************
//**************************************************************************

    //Creates a deep copy of the item and of its children.
    virtual QTreeWidgetItem *   clone() const; //连此条目的子树也会被克隆。

    virtual bool operator< (const QTreeWidgetItem & other) const; //无注释

    bool                      isHidden  () const;
    void                     setHidden  (bool hide);
    //注:如果该项当前不在视图中,调用此函数将没有效果。
    //具体而言,先在某个项上设置setHidden(true),然后再将其添加到视图中,将导致该项显示出来。

    //这几个成员函数是以代码的方式来操纵树表里的条目,隐藏,打开,折叠等。
    bool                      isExpanded() const;
    void                     setExpanded(bool expand);
    //警告:必须在调用此函数之前将 QTreeWidgetItem添加到 QTreeWidget中。

    bool                      isFirstColumnSpanned() const;
    void                     setFirstColumnSpanned(bool span);
    //Sets the first section to span all columns if span is true;
    //otherwise all item sections are shown.


    enum   ChildIndicatorPolicy {
            ShowIndicator,  //即使没有子项,此项目的展开和折叠控件也会显示。
        DontShowIndicator,  //即使有子节点,扩展和折叠控件也绝不会显示。
                            //如果该节点被强制展开,用户将无法扩展或折叠该条目。
        DontShowIndicatorWhenChildless //如果项目包含子项,则将显示展开和折叠的控件。
    };               //返回项目指示器策略。此策略决定何时显示树分支展开/折叠指示器。
    QTreeWidgetItem::ChildIndicatorPolicy childIndicatorPolicy() const;
    void setChildIndicatorPolicy(QTreeWidgetItem::ChildIndicatorPolicy policy);
    //Sets the item indicator policy.
    //The default value is DontShowIndicatorWhenChildless.默认策略是第三个

    //Reads the item from stream in. This only reads data into a single item.
    virtual void read (QDataStream & in );
    virtual void write(QDataStream & out) const; //Writes the item to stream out.

    // /Returns the type passed to the QTreeWidgetItem constructor.
    inline int                   type      () const { return rtti; }
    inline QTreeWidget     *     treeWidget() const { return view; }
    //Returns the tree widget that contains the item.
    //Returns the item's parent.
    inline QTreeWidgetItem *     parent    () const { return par ; }
    //Returns the number of columns in the item.
    inline int                  columnCount() const { return values  .count(); }
    //Returns the number of child items. //这里只返回本条目的直接子节点的数量,不进行递归计算
    inline int                   childCount() const { return children.count(); }
    inline QTreeWidgetItem *     child     (int index) const
    { //Returns the item at the given index in the list of the item's children.
        if (index < 0 || index >= children.size())
            return nullptr;

        executePendingSort();  //本类的虚函数,无注释,在下面

        return children.at(index);
    }

    //Returns the index of the given child in the item's list of children.
    inline int            indexOfChild   (           QTreeWidgetItem     * child) const
    {   //返回形参子条目的索引
        executePendingSort();  //本类的虚函数,无注释,在下面
        return children.indexOf(child);
    }

    //在子项列表中的索引 index处插入子项 child。如果子项已经在其他地方插入过,则不会再次插入。
    void                   insertChild   (int index, QTreeWidgetItem     * child);
    //Inserts the given list of children into the list of the item children at index .
    void                   insertChildren(int index, //在某索引处插入多个子条目
                                        const QList<QTreeWidgetItem *>   & children);
    //Appends the child item to the list of children.
    void                      addChild   (           QTreeWidgetItem     * child);
    void                      addChildren(const QList<QTreeWidgetItem *> & children);
    //Appends the given list of children to the item.
    void                   removeChild   (           QTreeWidgetItem     * child);
    //经 remove后的条目的内存空间仍然在,需 delete显式删除。
    //Removes the given item indicated by child. The removed item will not be deleted.
    QTreeWidgetItem *        takeChild   (int index);
    //Removes the item at index and returns it, otherwise return 0.
    QList<QTreeWidgetItem *> takeChildren();
    //Removes the list of children and returns it, otherwise returns an empty list.

    //根据给定列中的值,按给定顺序对项目的孩子进行排序。
    inline  //注意:如果项目没有与QTreeWidget关联,则此函数不会执行任何操作。
    void                     sortChildren(int column, Qt::SortOrder order)
    {   sortChildren(column, order, false); } //有同名的私有成员函数

protected:
    //导致与该项目关联的模型为此项目发出dataChanged()信号。
    //通常情况下,只有在你子类化了QTreeWidgetItem并重写了data()和/或setData()时才需要调用此函数。
    void emitDataChanged();

private:
    void sortChildren(int column, Qt::SortOrder order, bool climb);

    void executePendingSort() const;  //无注释

}; //完结 class QTreeWidgetItem


Q_WIDGETS_EXPORT //说明本条目可以与数据流交互,但打印的关于条目的有效信息很少。只打印条目地址。
QDataStream & operator<<(QDataStream & out, const QTreeWidgetItem & item);
Q_WIDGETS_EXPORT
QDataStream & operator>>(QDataStream & in ,       QTreeWidgetItem & item);

(12)

谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangzhangkeji

谢谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值