(7)开始本类的槽函数的学习 :
++
++ 测试一下 :
++接着给出本类的 protected 权限的槽函数 :
++本类的槽函数学习完毕。
(8)接着学习本类的俩信号函数 :
++本类的信号函数学习完毕。
(9) 本源代码定义于头文件 qtreeview . h :
#ifndef QTREEVIEW_H
#define QTREEVIEW_H
#include <QtWidgets/qtwidgetsglobal.h>
#include <QtWidgets/qabstractitemview.h>
class tst_QTreeView;
QT_REQUIRE_CONFIG(treeview);
QT_BEGIN_NAMESPACE
class QTreeViewPrivate;
class QHeaderView;
/*
The QTreeView class provides a default model/view implementation of a tree view.
Detailed Description :
A QTreeView 实现了来自模型的项的树状表示。
该类用于提供先前由 QListView 类提供的标准层次列表,但采用了 Ot的模型/视图架构所提供的更灵活的方法。
QTreeView类是Model/iew类之一,是Qt模型/视图框架的一部分。
QTreeView实现 QAbstractltemView类定义的接口,
以允许它显示从 QAbstractItemModel类派生的模型提供的数据。
构建一个显示模型数据的树状视图非常简单。
在下面的示例中,目录的内容由 QFileSystemModel提供,并以树状形式显示 :
QFileSystemModel * model = new QFileSystemModel;
model->setRootPath(QDir::currentPath());
QTreeView * tree = new QTreeView(splitter); //需要给视图找个容器
tree->setModel(model);
模型/视图架构确保树形视图的内容随着模型的变化而更新。
具有子项的项可以处于展开(子项可见)或折叠(子项隐藏)状态。
当这种状态发生变化时,会发出一个带有相关项模型索引的 collapsed() or expanded() signal。
用于表示层次结构级别的缩进量由缩进属性控制。
树状视图中的标题是通过使用 QHeaderView类构建的,并且可以使用 header()->hide()方法隐藏。
请注意,每个标题都被配置为将其 stretchLastSection 属性设置为 true,
以确保视图不会浪费分配给其标题的任何空间。
如果将此值设置为 true,则该属性将覆盖标题中最后一个部分的调整大小模式。
默认情况下,树视图中的所有列都是可移动的,除了第一个列。
要禁用这些列的移动,请使用QHeaderView的setSectionsMovable()函数。
关于重新排列节段的信息,请参阅“移动头部节段”.
Key Bindings :
QTreeView 支持一组键绑定,使用户能够在视图中导航并与项目内容进行交互:表略。
Improving Performance :
可以通过提供关于所处理数据的视图提示,来改善在显示大量项目时的性能。
对于旨在显示等高项目的视图,可以采用一种方法,即设置“uniformRowHeights”属性为true。
*/
class Q_WIDGETS_EXPORT QTreeView : public QAbstractItemView
{
Q_OBJECT
// 此属性在拖放操作期间,表示树中项目打开前的延迟时间。
//此属性表示用户在通过某个节点时必须等待的毫秒数,之后该节点才会自动开启或关闭。
//如果设置的等待时间小于0,则该属性将不会生效。// 涉及拖放,暂不考虑。
//默认情况下,此属性的值为-1,表示自动扩展被禁用。
Q_PROPERTY(int autoExpandDelay READ autoExpandDelay WRITE setAutoExpandDelay)
//树视图中项目的缩进。 此属性存储的是树视图中每个层级项的缩进量,以像素为单位。
//对于顶级项,缩进量表示从视口边缘到第一列项的横向距离;对于子项,它表示子项相对于其父项的缩进量。
//默认情况下,此属性的值是依赖于样式的。因此,当样式发生变化时,此属性会从样式中更新。
//调用setIndentation()会停止更新,而调用`resetIndentation()将恢复默认行为。
Q_PROPERTY(int indentation //默认 15像素
READ indentation WRITE setIndentation RESET resetIndentation)
//此属性表示是否显示用于展开和折叠顶级项目的控件。
//带有儿童的项目通常带有可展开和倒塌的控件,允许显示或隐藏其儿童。
//如果此属性为false,则不会为顶级项显示这些控件。
//这可以用来使单个层次的树结构看起来像一个简单的项目列表。
//默认情况下,此属性为真。 //意思是为带子表的顶层节点,装饰一个右箭头符号。
Q_PROPERTY(bool rootIsDecorated READ rootIsDecorated WRITE setRootIsDecorated)
//此属性表示树视图中的所有项目是否具有相同的高度。
//只有在确保视图中的所有项具有相同高度的情况下,才应将此属性设置为true。这有助于视图进行一些优化。
//高度从视图中的第一个项目获取。当该项目的数据发生变化时,它会更新。
//注意:如果编辑器大小提示大于单元格大小提示,则将使用编辑器的大小提示。
Q_PROPERTY(bool uniformRowHeights //默认情况下,此属性为false。
READ uniformRowHeights WRITE setUniformRowHeights)
//此属性表示项目是否可以由用户扩展。//若为 fasle则用户无法打开被折叠的子表。
//此属性表示用户是否可以交互式地展开和折叠项目默认情况下,
Q_PROPERTY(bool itemsExpandable //此属性为true。
READ itemsExpandable WRITE setItemsExpandable)
//此属性表示是否启用排序。
//如果此属性为true,则对树启用排序;如果属性为false,则不启用排序。默认值为false。
//注:为避免性能问题,建议在将项插入树状结构后启用排序功能。
//或者,也可以在将项插入树状结构之前先将其插入列表中。
Q_PROPERTY(bool sortingEnabled READ isSortingEnabled WRITE setSortingEnabled)
//比属性表示是否启用动画。如果此属性为真,则树视图将呈现分支的展开和折叠动画效果。
//如果此属性为假,则树视图将立即展开或折叠分支,而不显示动画效果。
//动画会较明显地给出子表的向下显出与向上收入的过程。
Q_PROPERTY(bool animated //默认情况下,此属性为false。
READ isAnimated WRITE setAnimated)
//此属性表示项目是否应使用所有列显示键盘焦点。
//如果此属性为true,所有列将显示焦点,否则仅一个列将显示焦点。默认值为false。
//点击时候,都是选中整行。但为true,则会为选中行添加一个边框。没啥用。
Q_PROPERTY(bool allColumnsShowFocus
READ allColumnsShowFocus WRITE setAllColumnsShowFocus)
//此属性持有项目文本的换行策略。
//如果此属性为真,则会在必要的词断处对项目文本进行换行处理;否则,文本将不会进行任何换行处理。
//请注意,即使启用了折叠功能,单元格也不会被扩展以容纳所有文本。会根据当前的文本缩短模式插入省略号。
Q_PROPERTY(bool wordWrap READ wordWrap WRITE setWordWrap)//该属性默认情况下为假。
//此属性表示是否显示标题。
//如果此属性为true,则不显示标题,否则显示。默认值为false。
Q_PROPERTY(bool headerHidden READ isHeaderHidden WRITE setHeaderHidden)
//此属性表示是否可以通过双击来展开项目。
//此属性表示用户是否可以通过双击来展开和折叠项目。默认值为true。
Q_PROPERTY(bool expandsOnDoubleClick
READ expandsOnDoubleClick WRITE setExpandsOnDoubleClick)
private:
friend class ::tst_QTreeView;
friend class QAccessibleTable;
friend class QAccessibleTree;
friend class QAccessibleTableCell;
int visualIndex(const QModelIndex &index) const;
Q_DECLARE_PRIVATE(QTreeView)
Q_DISABLE_COPY(QTreeView)
Q_PRIVATE_SLOT(d_func(), void _q_endAnimatedOperation())
Q_PRIVATE_SLOT(d_func(), void _q_modelAboutToBeReset())
Q_PRIVATE_SLOT(d_func(), void _q_sortIndicatorChanged(int column, Qt::SortOrder order))
public:
//Constructs a tree view with a parent to represent a model's data.
//Use setModel() to set the model.
explicit QTreeView(QWidget * parent = nullptr); //构造函数
~QTreeView();
// Q_PROPERTY(int autoExpandDelay //默认值为-1,表示自动扩展被禁用。关于拖放的
// READ autoExpandDelay WRITE setAutoExpandDelay)
int autoExpandDelay() const;
void setAutoExpandDelay(int delay);
// Q_PROPERTY(int indentation //树视图中项目的缩进。默认 15像素
// READ indentation
// WRITE setIndentation
// RESET resetIndentation)
int indentation() const;
void setIndentation(int i);
void resetIndentation();
// Q_PROPERTY(bool rootIsDecorated //默认为真,为带子表的顶层节点,装饰一个右箭头符号。
// READ rootIsDecorated WRITE setRootIsDecorated)
bool rootIsDecorated() const;
void setRootIsDecorated(bool show);
// Q_PROPERTY(bool uniformRowHeights //行高相同?默认情况下,此属性为false。
// READ uniformRowHeights WRITE setUniformRowHeights)
bool uniformRowHeights() const;
void setUniformRowHeights(bool uniform);
// Q_PROPERTY(bool itemsExpandable //默认为true。表用户可打开折叠的子表。
// READ itemsExpandable WRITE setItemsExpandable)
bool itemsExpandable() const;
void setItemsExpandable(bool enable);
// Q_PROPERTY(bool sortingEnabled //默认为false,则不启用排序。
// READ isSortingEnabled WRITE setSortingEnabled)
bool isSortingEnabled() const;
void setSortingEnabled(bool enable);
// Q_PROPERTY(bool animated //默认为false。不使用关开子表时的动画。
// READ isAnimated WRITE setAnimated)
bool isAnimated() const;
void setAnimated(bool enable);
// Q_PROPERTY(bool allColumnsShowFocus //默认为 F,不为选中行加边框
// READ allColumnsShowFocus WRITE setAllColumnsShowFocus)
bool allColumnsShowFocus() const;
void setAllColumnsShowFocus(bool enable);
// Q_PROPERTY(bool wordWrap //该属性默认为假,不使用换行策略。
// READ wordWrap WRITE setWordWrap)
bool wordWrap() const;
void setWordWrap(bool on);
// Q_PROPERTY(bool headerHidden //默认为false。要显示标题。
// READ isHeaderHidden WRITE setHeaderHidden)
bool isHeaderHidden() const;
void setHeaderHidden(bool hide);
// Q_PROPERTY(bool expandsOnDoubleClick //默认为true,允许用户通过双击来展开项目。
// READ expandsOnDoubleClick WRITE setExpandsOnDoubleClick)
bool expandsOnDoubleClick() const;
void setExpandsOnDoubleClick(bool enable);
//*********************以下是实现继承来的虚函数,不必注释了************************
void setModel (QAbstractItemModel * model) override;
void setRootIndex(const QModelIndex & index) override;
void setSelectionModel(QItemSelectionModel * selectionModel) override;
void keyboardSearch(const QString & search) override;
QRect visualRect(const QModelIndex & index) const override;
void scrollTo(const QModelIndex & index, ScrollHint hint = EnsureVisible) override;
QModelIndex indexAt(const QPoint & p) const override;
void doItemsLayout() override;
void reset() override;
void dataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight,
const QList<int> & roles = QList<int>()) override;
void selectAll() override;
//*********************以上是实现继承来的虚函数,不必注释了************************
// /Returns the header for the tree view.
QHeaderView * header() const; //返回本视图的表头
void setHeader(QHeaderView * header); //本视图会主动删除旧标题。
//Sets the header for the tree view, to the given header.
//The view takes ownership over the given header and
// deletes it when a new header is set.
//Returns true if the item in the given row of the parent is hidden;
// otherwise returns false.
bool isRowHidden(int row, const QModelIndex & parent) const;
void setRowHidden(int row, const QModelIndex & parent, bool hide);
//If hide is true the row with the given parent is hidden, otherwise the row is shown.
bool isColumnHidden(int column) const;
void setColumnHidden(int column, bool hide);
public Q_SLOTS:
//注:此函数应仅在模型初始化后调用,因为视图需要知道列的数目才能隐藏列。
void hideColumn(int column); //Hides the column given.
void showColumn(int column); //Shows the given column in the tree view.
public:
//Returns true if the item in first column in the given row of the
// parent is spanning all the columns; otherwise returns false.
//意思是对于含有子表的父类行,只显示第一列,不再显示父类行里的后面几列,就是 span跨行。
bool isFirstColumnSpanned(int row, const QModelIndex & parent) const;
void setFirstColumnSpanned(int row, const QModelIndex & parent, bool span);
//If span is true the item in the first column in the row with the
// given parent is set to span all columns, otherwise all items on the row are shown.
//Returns the column in the tree view whose header covers the x coordinate given.
int columnAt(int x) const; //返回视图里横坐标占据位置 x的列号
int columnViewportPosition(int column) const; //返回第 column列的横坐标
// /Returns the horizontal position of the column in the viewport.
//Returns the width of the column.
int columnWidth(int column) const;
void setColumnWidth(int column, int width);
//Sets the width of the given column to the width specified.
//返回树设置的逻辑索引。如果返回值为-1,则树放置在视觉索引0上。
int treePosition() const; //经测试,返回值是 0
void setTreePosition(int logicalIndex);
//这指定树结构应放置在逻辑索引index处。如果设置为-1,则树将始终遵循视觉索引0。
//Returns the model index of the item above / below index.
//测试发现,不会进入形参 index的子表里。这只是同级索引之间的检索与遍历。
QModelIndex indexAbove(const QModelIndex & index) const;
QModelIndex indexBelow(const QModelIndex & index) const;
//Returns true if the model item index is expanded; otherwise returns false.
bool isExpanded(const QModelIndex & index) const; //用代码的方式打开子表
void setExpanded(const QModelIndex & index, bool expand);
//Sets the item referred to by index to either collapse or expanded,
// depending on the value of expanded.
public Q_SLOTS:
//Expands the model item specified by the index.
void expand (const QModelIndex & index);
//Expands all expandable items to the given depth.
//Note: This function will not try to fetch more data.
void expandToDepth ( int depth); //这里的展开是从根节点开始
void expandRecursively(const QModelIndex & index, int depth = -1);
//Expands the item at the given index and all its children to the given depth.
//The depth is relative to the given index. //从节点 index开始展开
//A depth of -1 will expand all children,
//a depth of 0 will only expand the given index.
void expandAll(); //Expands all expandable items.
//Collapses the model item specified by the index.
void collapse (const QModelIndex & index);
void collapseAll(); //Collapses all expanded items.
//Resizes the column given to the size of its contents.
void resizeColumnToContents(int column); //调整列宽,缩小到等于内容宽
void sortByColumn(int column, Qt::SortOrder order);
//Sorts the model by the values in the given column and order.
//列可以是-1,在这种情况下将不显示排序指示符,模型将返回到其自然的未排序顺序。
//请注意,并非所有模型都支持这一点,甚至在此情况下可能崩溃。
//注:此函数应仅在模型初始化后调用,因为视图需要知道列的数目才能隐藏列。
//void hideColumn(int column); //Hides the column given. //这俩是函数
//void showColumn(int column); //Shows the given column in the tree view.
protected Q_SLOTS:
//This function is called whenever column's size is changed in the header.
//oldSize and newSize give the previous size and the new size in pixels.
void columnResized(int column, int oldSize, int newSize); //列的大小变了
void columnCountChanged(int oldCount, int newCount); //列的数量变了
//Informs the tree view that the number of columns in the
// tree view has changed from oldCount to newCount.
void columnMoved(); //This slot is called whenever a column has been moved.列被移动了
void reexpand (); //无注释
void rowsRemoved(const QModelIndex & parent, int first, int last); //行被移动了
//Informs the view that the rows from the start row to the
// end row inclusive have been removed from the given parent model item.
void verticalScrollbarValueChanged(int value) override; //垂直滚动条的值改变了
Q_SIGNALS:
//This signal is emitted when the item specified by index is expanded.
void expanded(const QModelIndex & index); //终于出个正常的了。
void collapsed(const QModelIndex & index);
//This signal is emitted when the item specified by index is collapsed.
protected:
QTreeView(QTreeViewPrivate & dd, QWidget * parent = nullptr);
//Returns the height of the row indicated by the given index.
int rowHeight (const QModelIndex & index) const;
int indexRowSizeHint(const QModelIndex & index) const;
//Returns the size hint for the row indicated by index.
//Draws the part of the tree intersecting the given region using the specified painter.
void drawTree(QPainter * painter, const QRegion & region) const;
//Draws the row in the tree view that contains the model item index,
// using the painter given. The option controls how the item is displayed.
virtual void drawRow ( QPainter * painter,
const QStyleOptionViewItem & options,
const QModelIndex & index) const;
virtual void drawBranches( QPainter * painter,
const QRect & rect,
const QModelIndex & index) const;
//Draws the branches in the tree view on the same row as the model item index,
// using the painter given. The branches are drawn in the rectangle specified by rect.
//*********************以下是实现继承来的虚函数,不必注释了************************
void scrollContentsBy(int dx, int dy) override;
void rowsInserted(const QModelIndex & parent, int start, int end) override;
void rowsAboutToBeRemoved(const QModelIndex & parent, int start, int end) override;
QModelIndex moveCursor(CursorAction cursorAction,
Qt::KeyboardModifiers modifiers) override;
int horizontalOffset() const override;
int verticalOffset() const override;
void setSelection(const QRect & rect,
QItemSelectionModel::SelectionFlags command) override;
QRegion visualRegionForSelection(const QItemSelection & selection) const override;
QModelIndexList selectedIndexes() const override;
void changeEvent(QEvent * event) override;
void timerEvent(QTimerEvent * event) override;
void paintEvent(QPaintEvent * event) override;
void mousePressEvent(QMouseEvent * event) override;
void mouseReleaseEvent(QMouseEvent * event) override;
void mouseDoubleClickEvent(QMouseEvent * event) override;
void mouseMoveEvent(QMouseEvent * event) override;
void keyPressEvent(QKeyEvent * event) override;
void dragMoveEvent(QDragMoveEvent * event) override;
bool viewportEvent(QEvent * event) override;
void updateGeometries() override;
QSize viewportSizeHint() const override;
int sizeHintForColumn(int column) const override;
void horizontalScrollbarAction(int action) override;
bool isIndexHidden(const QModelIndex & index) const override;
void selectionChanged(const QItemSelection & selected,
const QItemSelection & deselected) override;
void currentChanged(const QModelIndex & current,
const QModelIndex & previous) override;
}; //完结 class TreeView : public QAbstractItemView
QT_END_NAMESPACE
#endif // QTREEVIEW_H
(10)
谢谢