QT6 源(163)模型视图架构里的树表窗体视图 QTreeWidget :给出源代码,已整理并带注释

(1)只给出源码,不再详细注释与测试了。这势必导致以后应用的时候,手生 。本源代码定义于头文件 qtreewidget . h

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

Detailed Description :
一个便捷类,提供了一种标准的树形控件,
具有与 Qt3中的 QListView类所使用的经典基于项的界 QTreeWidget类是面类似的用户界面。
该类基于Qt的模型/视图架构,并使用默认模型来承载项,而每个项都是一个QTreeWidgetltem.

那些不需要模型/视图框架灵活性的开发者可以轻松使用这个类来创建简单的层次列表。
一个更灵活的方法是将QTreeView与标准的项模型结合使用,这样可以将数据存储与其表示方式分离。

在最简单的情况下,树形小部件可以按照以下方式构建:
     QTreeWidget * treeWidget = new QTreeWidget();
     treeWidget->setColumnCount(1);
     QList<QTreeWidgetItem *> items; //此容器列表包含许多  QTreeWidgetItem
     for (int i = 0; i < 10; ++i)    //晦涩难懂,不重要
         items.append(new QTreeWidgetItem(static_cast<QTreeWidget *>(nullptr),
            QStringList(QString("item: %1").arg(i))));
     treeWidget->insertTopLevelItems(0, items);

在将项目添加到树状控件之前,必须使用`setColumnCount()、函数设置列的数目。
这允许每个项目拥有、-个或多个标签或其他装饰项。
可以通过`columnCount()函数获取正在使用的列的数目。

树可以有一个标题,其中包含小组件中每列的部分。
通过使用setHeaderLabels ()提供字符串列表来为每个部分设置标签是最容易的,
但是可以使用QTreeWidgetItem 构造自定义标头,并使用 setHeaderItem ()函数插入到树中。

树中的项可以根据预定义的排序顺序按列进行排序。
如果启用了排序功能,用户可以通过点击列标题来对项进行排序。
排序功能可以通过调用setSortingEnabled()'来启用或禁用。
`isSortingEnabled()'函数用于指示是否启用了排序功能。


*/

class Q_WIDGETS_EXPORT QTreeWidget : public QTreeView
{
    Q_OBJECT
    //此属性表示在树形控件中显示的列数。默认情况下,此属性的值为1。
    Q_PROPERTY(int columnCount READ columnCount WRITE setColumnCount)

    //此属性持有顶级项目的数量默认情况下,此属性的值为0。
    Q_PROPERTY(int topLevelItemCount  READ  topLevelItemCount)

    friend class QTreeModel;
    friend class QTreeWidgetItem;

public:
    //Constructs a tree widget with the given parent.
    explicit QTreeWidget(QWidget * parent = nullptr);

    ~QTreeWidget();

//   Q_PROPERTY(int     columnCount
//              READ    columnCount     WRITE     setColumnCount)
                int     columnCount() const;
                void setColumnCount(int columns);

//   Q_PROPERTY(int     topLevelItemCount   READ   topLevelItemCount)
                int     topLevelItemCount() const;


    inline        //Same as setHeaderLabels(QStringList(label)).
    void                    setHeaderLabel (const QString     & label );
    void                    setHeaderLabels(const QStringList & labels);
    //把新标签 labels作为新的表头
    //Returns the item used for the tree widget's header.
    QTreeWidgetItem *          headerItem  () const;
    void                    setHeaderItem  (QTreeWidgetItem * item);
    //设置树形控件的标题项。标题中每列的标签由项目中的相应标签提供。
    //树形组件接管该项的所有权。


/*
inline void QTreeWidget::removeItemWidget(QTreeWidgetItem *item, int column)
{ setItemWidget(item, column, nullptr); }

inline QTreeWidgetItem *QTreeWidget::itemAt(int ax, int ay) const
{ return itemAt(QPoint(ax, ay)); }

inline void QTreeWidget::setHeaderLabel(const QString &alabel)
{ setHeaderLabels(QStringList(alabel)); }

*/
    //返回树小部件的不可见根项。这个隐式的根项通过 QTreeWidgetItem API提供对树状控件顶级项的访问,
    //  从而使得编写能够以统-方式处理顶级项及其子项的函数成为可能;例如,递归函数。
    QTreeWidgetItem *   invisibleRootItem  () const;
    void setSelectionModel(QItemSelectionModel * selectionModel) override; //虚函数
    QList<QTreeWidgetItem *> selectedItems () const;
    //Returns a list of all selected non-hidden items.

    //Returns the index of the given top-level item, or -1 if the item cannot be found.
    int               indexOfTopLevelItem  (           QTreeWidgetItem * item) const;
    QTreeWidgetItem *        topLevelItem  (int index) const;
    //Returns the top level item at the given index,
    //  or nullptr if the item does not exist.
    void               insertTopLevelItem  (int index, QTreeWidgetItem * item);
    //Inserts the item at index in the top level in the view.
    //If the item has already been inserted somewhere else it won't be inserted.
    void               insertTopLevelItems (int index,
                                            const QList<QTreeWidgetItem *> & items);
    //Inserts the list of items at index in the top level in the view.
    void                  addTopLevelItem  (           QTreeWidgetItem * item);
    void                  addTopLevelItems (const QList<QTreeWidgetItem*>  & items);
    //Appends the list of items as a top-level items in the widget.
    QTreeWidgetItem *    takeTopLevelItem  (int index);
    //Removes the top-level item at the given index in the tree and returns it,
    //  otherwise returns nullptr;

    //Sorts the items in the widget in the
    //  specified order by the values in the given column.
    void                         sortItems(int column, Qt::SortOrder order);
    int                          sortColumn() const; //看看是按哪列进行排序。
    //Returns the column used to sort the contents of the widget.

    //Returns the current column in the tree widget.
    int                       currentColumn() const;
    QTreeWidgetItem *         currentItem  () const;
    //Sets the current item in the tree widget.
    //Unless the selection mode is NoSelection, the item is also selected.
    void                   setCurrentItem  (QTreeWidgetItem * item);
    //Sets the current item in the tree widget and the current column to column.
    void                   setCurrentItem  (QTreeWidgetItem * item, int column);
    //Sets the current item in the tree widget and the current column to column,
    //  using the given command.
    void                   setCurrentItem  (QTreeWidgetItem * item, int column,
                                            QItemSelectionModel::SelectionFlags command);
    /*
    enum QItemSelectionModel::SelectionFlag {
        NoUpdate       = 0x0000,
        Clear          = 0x0001,
        Select         = 0x0002,
        Deselect       = 0x0004,
        Toggle         = 0x0008,
        Current        = 0x0010,
        Rows           = 0x0020,
        Columns        = 0x0040,
        SelectCurrent  = Select | Current,
        ToggleCurrent  = Toggle | Current,
        ClearAndSelect = Clear  | Select
    };
    */

    QTreeWidgetItem *                itemAt(const QPoint & p) const;
    inline
    QTreeWidgetItem *                itemAt(int x, int y) const;
    QRect                      visualItemRect(const QTreeWidgetItem * item) const;
    //Returns a list of items that match the given text,
    //  using the given flags, in the given column.
    QList<QTreeWidgetItem *>     findItems (const QString & text, Qt::MatchFlags flags,
                                            int column = 0) const;
    QModelIndex             indexFromItem  (const QTreeWidgetItem *item,
                                            int column = 0) const;
    QTreeWidgetItem *                itemFromIndex(const QModelIndex & index) const;

    //Returns the item above the given item.
    QTreeWidgetItem *                itemAbove(const QTreeWidgetItem * item ) const;
    QTreeWidgetItem *                itemBelow(const QTreeWidgetItem * item ) const;
    //Returns the item visually below the given item.

    //Starts editing the item in the given column if it is editable.
    void                         editItem(QTreeWidgetItem * item, int column = 0);

    using  QAbstractItemView::isPersistentEditorOpen;
    //bool QAbstractItemView::isPersistentEditorOpen(QModelIndex & index);

    bool     isPersistentEditorOpen(QTreeWidgetItem * item, int column = 0) const;
    void   openPersistentEditor    (QTreeWidgetItem * item, int column = 0);
    void  closePersistentEditor    (QTreeWidgetItem * item, int column = 0);


    QWidget *       itemWidget(QTreeWidgetItem * item, int column) const;
    void         setItemWidget(QTreeWidgetItem * item, int column, QWidget * widget);
    inline
    void      removeItemWidget(QTreeWidgetItem * item, int column);

public Q_SLOTS:
    //通过移除其所有项目和选择项来清除树形控件。
    //注意:由于每个项目在删除之前都会从树小部件中删除,
    //因此当从项目的析构函数调用时,QTreeWidgetItemtreeWidget()的返回值将无效。
    void  clear(); //就是清空整个树表。但仍会保留表头。

    void  scrollToItem             (const QTreeWidgetItem * item,
                        QAbstractItemView::ScrollHint hint = EnsureVisible);
/*
enum QAbstractItemView::ScrollHint {
    EnsureVisible   , //Scroll to ensure that the item is visible.
    PositionAtTop   , //Scroll to position the item at the top    of the viewport.
    PositionAtBottom, //Scroll to position the item at the bottom of the viewport.
    PositionAtCenter  //Scroll to position the item at the center of the viewport.
};
Q_ENUM(ScrollHint)    //滚动屏幕到条目 index处,并指明了 index条目的新的位置 
*/   

    void    expandItem             (const QTreeWidgetItem * item);
    void  collapseItem             (const QTreeWidgetItem * item);

Q_SIGNALS:
    void          itemExpanded     (QTreeWidgetItem * item);
    void          itemCollapsed    (QTreeWidgetItem * item);

    void          itemEntered      (QTreeWidgetItem * item, int column);
    void          itemPressed      (QTreeWidgetItem * item, int column);
    void          itemClicked      (QTreeWidgetItem * item, int column);
    void          itemDoubleClicked(QTreeWidgetItem * item, int column);
    void          itemActivated    (QTreeWidgetItem * item, int column);

    //This signal is emitted when the contents of the column in the specified item changes.
    void          itemChanged      (QTreeWidgetItem * item, int column);
    //This signal is emitted when the current item changes.
    //The current item is specified by current,
    //  and this replaces the previous current item.
    void   currentItemChanged(QTreeWidgetItem * current, QTreeWidgetItem * previous);
    void          itemSelectionChanged();
    //This signal is emitted when the selection changes in the tree widget.
    //The current selection can be found with selectedItems().

protected:
    bool     event(QEvent     * e    ) override;
    void dropEvent(QDropEvent * event) override;

    virtual QStringList        mimeTypes() const;

    virtual QMimeData   *      mimeData(const QList<QTreeWidgetItem *> & items) const;
    virtual bool           dropMimeData(QTreeWidgetItem * parent,
                                        int index, const QMimeData * data,
                                        Qt::DropAction action);

    virtual Qt::DropActions supportedDropActions() const;

private:
    void setModel(QAbstractItemModel * model) override; //此函数作废

    Q_DECLARE_PRIVATE(QTreeWidget)
    Q_DISABLE_COPY(QTreeWidget)

    Q_PRIVATE_SLOT(d_func(), void _q_emitItemPressed(const QModelIndex &index))
    Q_PRIVATE_SLOT(d_func(), void _q_emitItemClicked(const QModelIndex &index))
    Q_PRIVATE_SLOT(d_func(), void _q_emitItemDoubleClicked(const QModelIndex &index))
    Q_PRIVATE_SLOT(d_func(), void _q_emitItemActivated(const QModelIndex &index))
    Q_PRIVATE_SLOT(d_func(), void _q_emitItemEntered(const QModelIndex &index))
    Q_PRIVATE_SLOT(d_func(), void _q_emitItemChanged(const QModelIndex &index))
    Q_PRIVATE_SLOT(d_func(), void _q_emitItemExpanded(const QModelIndex &index))
    Q_PRIVATE_SLOT(d_func(), void _q_emitItemCollapsed(const QModelIndex &index))
    Q_PRIVATE_SLOT(d_func(), void _q_emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &current))
    Q_PRIVATE_SLOT(d_func(), void _q_sort())
    Q_PRIVATE_SLOT(d_func(), void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight))
    Q_PRIVATE_SLOT(d_func(), void _q_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected))

}; //完结 class QTreeWidget : public QTreeView

(2)

谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zhangzhangkeji

谢谢支持

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

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

打赏作者

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

抵扣说明:

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

余额充值