(3) 这些保护权限的成员函数,全是继承于基类的虚函数,故不再注释了 :
protected: //这些保护权限的成员函数,全是继承于基类的虚函数,故不再注释了。
QTableView(QTableViewPrivate & , QWidget * parent); //有参构造函数
void scrollContentsBy(int dx, int dy) override;
void initViewItemOption(QStyleOptionViewItem * option) const override;
void paintEvent(QPaintEvent * e ) override;
void timerEvent(QTimerEvent * event) override;
int horizontalOffset() const override;
int verticalOffset() const override;
QModelIndex moveCursor(CursorAction cursorAction, //键盘操作的下一个条目
Qt::KeyboardModifiers modifiers ) override;
QModelIndexList selectedIndexes() const override;
void setSelection(const QRect & rect,
QItemSelectionModel::SelectionFlags command) override;
QRegion visualRegionForSelection(const QItemSelection & selection) const override;
void updateGeometries() override;
QSize viewportSizeHint ( ) const override;
int sizeHintForRow (int row ) const override;
int sizeHintForColumn(int column) const override;
void verticalScrollbarAction(int action) override;
void horizontalScrollbarAction(int action) override;
bool isIndexHidden(const QModelIndex & index) const override;
void selectionChanged(const QItemSelection & selected, //基类里是protected Q_SLOTS
const QItemSelection & deselected) override;
void currentChanged(const QModelIndex & current, //基类里是protected Q_SLOTS
const QModelIndex & previous) override;
public Q_SLOTS:
(4)接下来开始 public 权限的槽函数学习 :
++
++给出测试 :
++
++测试一下 :
++
(5)这些槽函数,无法在外部调用 :
(6)本表视图的源代码定义于头文件 qtableview . h :
#ifndef QTABLEVIEW_H
#define QTABLEVIEW_H
#include <QtWidgets/qtwidgetsglobal.h>
#include <QtWidgets/qabstractitemview.h>
QT_REQUIRE_CONFIG(tableview);
QT_BEGIN_NAMESPACE
class QHeaderView;
class QTableViewPrivate;
/*
The QTableView class provides a default model/view implementation of a table view。
Detailed Description:
A QTableView实现了一个表格视图,用于显示来自模型的项。
该类用于提供先前由 QTable 类提供的标准表格,但采用了 Qt的模型/视图架构所提供的更为灵活的方式。
QTableView 类是模型/视图类之一,是 Qt 模型/视图框架的一部分。
QTableView 实现了 QAbstractltemView 类定义的接口,
以允许它显示从 QAbstractltemModel类派生的模型提供的数据。
Navigation :
您可以通过用鼠标点击某个单元格,或使用箭头键来在表格中的单元格间进行导航。
由于QTableView默认启用了tabKeyavigation功能,因此您还可以通过按下Tab键和Backtab键来在单元格间移动。
Visual Appearance :
该表格有一个纵向标题,自可以通过`verticalHeader()、函数获取,
还有一个横向标题,可以通过函数 horizontalHeader() 获取。
表格中每一行的高度可以通过`rowHeight()、函数找到;同理,列的宽度可以通过`columnWidth()函数找到。
由于这两个都是普通的控件,您可以使用它们的`hide()、函数来隐藏其中任意一个。
行和列可以通过`hideRow()、hidecolumn ()`showRow()、和`showColumn()、方法隐藏或显示。
它们也可以通过selectRow()和`selectColumn()、方法进行选择。根据`showGrid`属性,表格将显示网格状结构。
在表格视图中显示的项,与在其他项视图中显示的项一样,都是使用标准委托进行渲染和编辑的。
然而,对干某些任务,有时更有用的是能够在表格中插入小部件。
通过调用`setlndexWidget()、函数可以为特定的索引设置小部件,
而后再通过`indexWidget()、函数来检索这些小部件。(这俩成员函数继承于抽象基类)。
默认情况下,表格中的单元格不会自动扩展以填充可用空间。
您可以通过拉伸最后一个标题段来使单元格填充可用空间。
使用`horizontalHeader()或verticalHeader()、访问相应的标题,并设置标题的`stretchLastSection`属性。
为了根据每个列或行的空间需求来分配可用空间,
应调用视图中的resizeColumnsToContents或resizeRowsToContents函数。
Coordinate Systems :
对于一些特定的表格形式,能够在行索引和列索引以及小部件坐标之间进行转换是非常有用的。
`rowAt()函数提供指定行在视图中的y坐标;
而`rowIndex`则可用于通过`rowViewportPosition()'函数获取相应的y坐标。
columnAt ()和`columnViewportPosition()、函数则提供了在x坐标和列索引之间进行等效转换的操作。
*/
class Q_WIDGETS_EXPORT QTableView : public QAbstractItemView
{
Q_OBJECT
//This property holds whether the grid is shown。
//如果此属性为真,则在表格上绘制网格;如果属性为假,则不绘制网格。默认值为真。
Q_PROPERTY(bool showGrid READ showGrid WRITE setShowGrid)
//This property holds the pen style used to draw the grid.
Q_PROPERTY(Qt::PenStyle gridStyle READ gridStyle WRITE setGridStyle)
/*
enum Qt::PenStyle { // pen style
NoPen,
SolidLine,
DashLine,
DotLine,
DashDotLine,
DashDotDotLine,
CustomDashLine
MPenStyle = 0x0f
};
*/
//This property holds whether sorting is enabled。
//如果此属性为true,则在表格上启用排序。如果此属性为false,则不启用排序。默认值为false。
//注意:使用setSortingEnabled()将属性设置为true会立即触发对sortByColumn()的调用,
// 传入当前的排序部分和顺序。
//Note: Setting the property to true with setSortingEnabled() immediately triggers a
// call to sortByColumn() with the current sort section and order.
Q_PROPERTY(bool sortingEnabled READ isSortingEnabled WRITE setSortingEnabled)
//This property holds the item text word-wrapping policy。
//如果此属性为真,则会在必要的换行处对项目文本进行换行处理;否则将完全不进行换行处理。
//此属性默认即为真。请注意,即使启用了折叠功能,单元格也不会被扩展以容纳所有文本。
//会根据当前的文本缩短模式 textElideMode 插入省略号。
Q_PROPERTY(bool wordWrap READ wordWrap WRITE setWordWrap)
//This property holds whether the button in the top-left corner is enabled。
//如果这个属性为真,那么表格视图左上角的按钮将被激活。点击该按钮将选择表格视图中的所有单元格。
//此属性默认为真。
Q_PROPERTY(bool cornerButtonEnabled
READ isCornerButtonEnabled WRITE setCornerButtonEnabled)
private:
friend class QAccessibleItemView;
int visualIndex(const QModelIndex &index) const;
Q_DECLARE_PRIVATE(QTableView)
Q_DISABLE_COPY(QTableView)
Q_PRIVATE_SLOT(d_func(), void _q_selectRow(int))
Q_PRIVATE_SLOT(d_func(), void _q_selectColumn(int))
Q_PRIVATE_SLOT(d_func(), void _q_updateSpanInsertedRows(QModelIndex,int,int))
Q_PRIVATE_SLOT(d_func(), void _q_updateSpanInsertedColumns(QModelIndex,int,int))
Q_PRIVATE_SLOT(d_func(), void _q_updateSpanRemovedRows(QModelIndex,int,int))
Q_PRIVATE_SLOT(d_func(), void _q_updateSpanRemovedColumns(QModelIndex,int,int))
Q_PRIVATE_SLOT(d_func(), void _q_sortIndicatorChanged(int column, Qt::SortOrder order))
public:
explicit QTableView(QWidget *parent = nullptr);
~QTableView();
// Q_PROPERTY(bool showGrid //是否显示条目边框,默认显示实线边框
// READ showGrid WRITE setShowGrid)
bool showGrid() const;
public Q_SLOTS:
void setShowGrid(bool show);
public :
// Q_PROPERTY(Qt::PenStyle gridStyle //条目边框的线型
// READ gridStyle WRITE setGridStyle)
Qt::PenStyle gridStyle() const;
void setGridStyle(Qt::PenStyle style);
// Q_PROPERTY(bool sortingEnabled //是否在表格上启用排序,默认 false
// READ isSortingEnabled WRITE setSortingEnabled)
bool isSortingEnabled() const;
void setSortingEnabled(bool enable);
// Q_PROPERTY(bool wordWrap //是否启用条目内的内容换行,默认为真。
// READ wordWrap
// WRITE setWordWrap) //若内容太多,永远都会启用省略号,不会扩大单元格
bool wordWrap() const;
void setWordWrap(bool on);
// Q_PROPERTY(bool cornerButtonEnabled //是否启用表格左上角的全选按钮,默认为真。
// READ isCornerButtonEnabled WRITE setCornerButtonEnabled)
bool isCornerButtonEnabled() const;
void setCornerButtonEnabled(bool enable);
//以下这些是重写父类的虚函数,不再注释
void setModel(QAbstractItemModel * model) override;
void setRootIndex(const QModelIndex & index) override;
void setSelectionModel(QItemSelectionModel * selectionModel) override;
void doItemsLayout() 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 QStandardItemModel::setHorizontalHeaderLabels(const QStringList & labels)
//class QHeaderView : public QAbstractItemView,这个类以后再学.从模式里设置表头也可以。
//Returns the table view's horizontal header.
QHeaderView * horizontalHeader() const;
void setHorizontalHeader(QHeaderView * header);
//Sets the widget to use for the horizontal header to header.
QHeaderView * verticalHeader () const;
void setVerticalHeader (QHeaderView * header);
//Returns the y/x-coordinate in contents coordinates of the given row/column.
int rowViewportPosition(int row ) const; //返回行 row 的起始坐标。
int columnViewportPosition(int column) const; //返回列 column的起始坐标。
//Returns the row in which the given y-coordinate, y,
// in contents coordinates is located.
int rowAt(int y) const; //返回 y 坐标在第几行。
int columnAt(int x) const; //返回 x 坐标在第几列。
int rowHeight(int row) const; //Returns the height of the given row.
void setRowHeight(int row, int height);
int columnWidth(int column) const; //Returns the width of the given column.
void setColumnWidth(int column, int width);
//Returns true if the given row is hidden; otherwise returns false.
bool isRowHidden(int row) const; //某行某列被隐藏后,其位置也会被取代,
void setRowHidden(int row, bool hide); //导致本行本列看起来完全消失了。
bool isColumnHidden(int column) const;
void setColumnHidden(int column, bool hide);
//If hide is true the given column will be hidden; otherwise it will be shown.
//Returns the row(column) span of the table element at (row, column). The default is 1.
int rowSpan(int row, int column) const; //返回条目[r,c]占据的行数或列数。
int columnSpan(int row, int column) const; //以及设置该条目占据的行数与列数。
void setSpan(int row, int column, int rowSpan, int columnSpan);
//以[r,c]开始的 row行元素,c列元素,都会被清空,完全被放大后的 [r,c]占据。
//Sets the span of the table element at (row, column) to the
// number of rows and columns specified by (rowSpanCount, columnSpanCount).
void clearSpans(); //清除所有的元素跨行跨列现象。
//Removes all row and column spans in the table view.
protected: //这些保护权限的成员函数,全是继承于基类的虚函数,故不再注释了。
QTableView(QTableViewPrivate & , QWidget * parent); //有参构造函数
void scrollContentsBy(int dx, int dy) override;
void initViewItemOption(QStyleOptionViewItem * option) const override;
void paintEvent(QPaintEvent * e ) override;
void timerEvent(QTimerEvent * event) override;
int horizontalOffset() const override;
int verticalOffset() const override;
QModelIndex moveCursor(CursorAction cursorAction, //键盘操作的下一个条目
Qt::KeyboardModifiers modifiers ) override;
QModelIndexList selectedIndexes() const override;
void setSelection(const QRect & rect,
QItemSelectionModel::SelectionFlags command) override;
QRegion visualRegionForSelection(const QItemSelection & selection) const override;
void updateGeometries() override;
QSize viewportSizeHint ( ) const override;
int sizeHintForRow (int row ) const override;
int sizeHintForColumn(int column) const override;
void verticalScrollbarAction(int action) override;
void horizontalScrollbarAction(int action) override;
bool isIndexHidden(const QModelIndex & index) const override;
void selectionChanged(const QItemSelection & selected, //基类里是protected Q_SLOTS
const QItemSelection & deselected) override;
void currentChanged(const QModelIndex & current, //基类里是protected Q_SLOTS
const QModelIndex & previous) override;
public Q_SLOTS:
//void setShowGrid(bool show);
//Selects the given row in the table view if the
// current SelectionMode and SelectionBehavior allows rows to be selected.
void selectRow (int row ); //以代码的方式选中表格里的 row行或者 column列。
void selectColumn(int column);
void hideRow(int row);
void hideColumn(int column);
/* 类似功能的 public 成员函数
bool isRowHidden(int row) const; //某行某列被隐藏后,其位置也会被取代,
void setRowHidden(int row, bool hide); //导致本行本列看起来完全消失了。
bool isColumnHidden(int column) const;
void setColumnHidden(int column, bool hide);
*/
void showRow(int row);
void showColumn(int column);
void resizeRowToContents(int row);
void resizeRowsToContents();
//Resizes all rows based on the size hints of the
// delegate used to render each item in the rows.
void resizeColumnToContents(int column);
void resizeColumnsToContents();
//按给定列中的值对模型进行排序并指定顺序。
//Sorts the model by the values in the given column and order.
//列可以是 -1,在这种情况下将不显示排序指示符,模型将返回到其自然的未排序顺序。
//column may be -1, in which case no sort indicator will be shown and the
//model will return to its natural, unsorted order.
//请注意,并非所有模型都支持这一点,甚至在此情况下可能崩溃。
//Note that not all models support this and may even crash in this case.
void sortByColumn(int column, Qt::SortOrder order);
//按某列排序,不仅仅是该列的数据会改变。以行为单位,所有的行,整张表里的数据都会改变。
protected Q_SLOTS: //这些槽函数是保护权限,无法在外部调用。似乎是为了响应模型的改表信号。
//此插槽用于更改表视图中给定行的索引。
//This slot is called to change the index of the given row in the table view.
//The old index is specified by oldIndex, and the new index by newIndex.
void rowMoved(int row , int oldIndex, int newIndex);
void columnMoved(int column, int oldIndex, int newIndex); //不知道啥意思
/* 可以为整行和整列设置
QAbstractItemDelegate * itemDelegateForRow (int row) const; //返回本行使用的代理。
void setItemDelegateForRow (int row, QAbstractItemDelegate * delegate);
QAbstractItemDelegate * itemDelegateForColumn(int column) const;
void setItemDelegateForColumn(int column, QAbstractItemDelegate * delegate);
*/
//This slot is called to change the height of the given row.
//The old height is specified by oldHeight, and the new height by newHeight.
void rowResized(int row , int oldHeight, int newHeight);
void columnResized(int column, int oldWidth , int newWidth );
//This slot is called whenever rows are added or deleted.
//The previous number of rows is specified by oldCount,
//and the new number of rows is specified by newCount.
void rowCountChanged(int oldCount, int newCount);
void columnCountChanged(int oldCount, int newCount);
}; //完结 class QTableView : public QAbstractItemView
QT_END_NAMESPACE
#endif // QTABLEVIEW_H
(7)
谢谢