QTableView 类

The QTableView 类提供表格视图的默认模型/视图实现。 更多...

头: #include <QTableView>
qmake: QT += widgets
继承: QAbstractItemView
继承者: QTableWidget

特性

公共函数

QTableView (QWidget * parent = nullptr)
virtual ~QTableView ()
void clearSpans ()
int columnAt (int x ) const
int columnSpan (int row , int column ) const
int columnViewportPosition (int column ) const
int columnWidth (int column ) const
Qt::PenStyle gridStyle () const
QHeaderView * horizontalHeader () const
bool isColumnHidden (int column ) const
bool isCornerButtonEnabled () const
bool isRowHidden (int row ) const
bool isSortingEnabled () const
int rowAt (int y ) const
int rowHeight (int row ) const
int rowSpan (int row , int column ) const
int rowViewportPosition (int row ) const
void setColumnHidden (int column , bool hide )
void setColumnWidth (int column , int width )
void setCornerButtonEnabled (bool enable )
void setGridStyle (Qt::PenStyle style )
void setHorizontalHeader (QHeaderView * header )
void setRowHeight (int row , int height )
void setRowHidden (int row , bool hide )
void setSortingEnabled (bool enable )
void setSpan (int row , int column , int rowSpanCount , int columnSpanCount )
void setVerticalHeader (QHeaderView * header )
void setWordWrap (bool on )
bool showGrid () const
void sortByColumn (int column , Qt::SortOrder order )
QHeaderView * verticalHeader () const
bool wordWrap () const

重实现公共函数

virtual QModelIndex indexAt (const QPoint & pos ) const override
virtual void setModel (QAbstractItemModel * model ) override
virtual void setRootIndex (const QModelIndex & index ) override
virtual void setSelectionModel (QItemSelectionModel * selectionModel ) override

公共槽

void hideColumn (int column )
void hideRow (int row )
void resizeColumnToContents (int column )
void resizeColumnsToContents ()
void resizeRowToContents (int row )
void resizeRowsToContents ()
void selectColumn (int column )
void selectRow (int row )
void setShowGrid (bool show )
void showColumn (int column )
void showRow (int row )

静态公共成员

const QMetaObject staticMetaObject

重实现保护函数

virtual void currentChanged (const QModelIndex & current , const QModelIndex & previous ) override
virtual int horizontalOffset () const override
virtual bool isIndexHidden (const QModelIndex & index ) const override
virtual QModelIndex moveCursor (QAbstractItemView::CursorAction cursorAction , Qt::KeyboardModifiers modifiers ) override
virtual void paintEvent (QPaintEvent * event ) override
virtual QModelIndexList selectedIndexes () const override
virtual void selectionChanged (const QItemSelection & selected , const QItemSelection & deselected ) override
virtual void setSelection (const QRect & rect , QItemSelectionModel::SelectionFlags flags ) override
virtual int sizeHintForColumn (int column ) const override
virtual int sizeHintForRow (int row ) const override
virtual void timerEvent (QTimerEvent * event ) override
virtual void updateGeometries () override
virtual int verticalOffset () const override
virtual QStyleOptionViewItem viewOptions () const override
virtual QSize viewportSizeHint () const override

保护槽

void columnCountChanged (int oldCount , int newCount )
void columnMoved (int column , int oldIndex , int newIndex )
void columnResized (int column , int oldWidth , int newWidth )
void rowCountChanged (int oldCount , int newCount )
void rowMoved (int row , int oldIndex , int newIndex )
void rowResized (int row , int oldHeight , int newHeight )

额外继承成员

详细描述

The QTableView 类提供表格视图的默认模型/视图实现。

A QTableView 实现从模型显示项的表格视图。此类用于提供先前由 QTable 类提供的标准表格,但使用由 Qt 模型/视图体系结构提供的更灵活方式。

The QTableView 类是一种 模型/视图类 且属于 Qt 的 模型/视图框架 .

QTableView 实现接口的定义通过 QAbstractItemView 类以允许它显示提供数据,通过模型派生自 QAbstractItemModel 类。

可以通过采用鼠标点击单元格 (或通过使用方向键) 以导航表格单元格。因为 QTableView 启用 tabKeyNavigation 默认情况下,也可以命中 Tab 和 Backtab 键以从一单元格移至另一单元格。

视觉外观

表格拥有的垂直 Header 头部可以获得使用 verticalHeader () 函数,和水平 Header 头部获得透过 horizontalHeader () 函数。可以找到表格中每行的高度通过使用 rowHeight ();同样,可以找到列的宽度使用 columnWidth ()。由于这两者是纯 Widget,可以隐藏它们使用 hide () 函数。

行和列可以被隐藏和展示采用 hideRow (), hideColumn (), showRow (),和 showColumn ()。可以选择它们采用 selectRow () 和 selectColumn ()。表格将展示栅格从属 showGrid 特性。

像其它项视图中的项,表格视图中展示的项的渲染和编辑是使用标准 delegates 。不管怎样,对于某些任务,取而代之,有时能将 Widget 插入表格很有用。设置特定索引的 Widget 是采用 setIndexWidget() 函数,和稍后检索采用 indexWidget() .

默认情况下,不会展开表格中的单元格以填充可用空间。

可以使单元格填充可用空间,通过拉伸最后 Header 头部区间。访问相关 Header 头部使用 horizontalHeader () 或 verticalHeader () 并设置 Header 头部 stretchLastSection 特性。

要根据每列 (或每行) 的空间要求分发可用空间,调用视图的 resizeColumnsToContents () 或 resizeRowsToContents () 函数。

坐标系

对于某些专用形式的表格而言,能够在行、列索引和 Widget 坐标之间转换很有用。 rowAt () 函数提供指定行视图中的 Y 坐标;使用行索引可以获得相应 Y 坐标采用 rowViewportPosition ()。 columnAt () 和 columnViewportPosition () 函数提供 X 坐标和列索引之间的等价转换操作。

另请参阅 QTableWidget , 视图类 , QAbstractItemModel , QAbstractItemView , 图表范例 , 像素器范例 ,和 表格模型范例 .

特性文档编制

cornerButtonEnabled : bool

此特性保持是否启用左上角按钮

若此特性为 true 则启用表格视图左上角按钮。点击此按钮将选择表格视图中的所有单元格。

此特性是 true 在默认情况下。

该特性在 Qt 4.3 引入。

访问函数:

bool isCornerButtonEnabled () const
void setCornerButtonEnabled (bool enable )

gridStyle : Qt::PenStyle

此特性保持用于绘制栅格的钢笔样式。

此特性保持绘制栅格时的使用样式 (见 showGrid ).

访问函数:

Qt::PenStyle gridStyle () const
void setGridStyle (Qt::PenStyle style )

showGrid : bool

此特性保持是否展示栅格

若此特性为 true 为表格绘制栅格;若特性为 false ,不绘制栅格。默认值为 true。

访问函数:

bool showGrid () const
void setShowGrid (bool show )

sortingEnabled : bool

此特性保持是否启用排序

若此特性为 true , sorting is enabled for the table. If this property is false , sorting is not enabled. The default value is false.

注意: . Setting the property to true with setSortingEnabled () immediately triggers a call to sortByColumn () with the current sort section and order.

该特性在 Qt 4.2 引入。

访问函数:

bool isSortingEnabled () const
void setSortingEnabled (bool enable )

另请参阅 sortByColumn ().

wordWrap : bool

此特性保持项文本的自动换行策略

若此特性为 true 那么有必要在单词分割处换行项文本;否则,根本不换行。此特性为 true 在默认情况下。

Note that even of wrapping is enabled, the cell will not be expanded to fit all text. Ellipsis will be inserted according to the current textElideMode .

该特性在 Qt 4.3 引入。

访问函数:

bool wordWrap () const
void setWordWrap (bool on )

成员函数文档编制

QTableView:: QTableView ( QWidget * parent = nullptr)

构造表格视图采用 parent 以表示数据。

另请参阅 QAbstractItemModel .

[virtual] QTableView:: ~QTableView ()

销毁表格视图。

void QTableView:: clearSpans ()

移除表格视图中的所有行、列跨度。

该函数在 Qt 4.4 引入。

另请参阅 setSpan ().

int QTableView:: columnAt ( int x ) const

返回列在给定 X 坐标 x ,按内容坐标定位。

注意: 此函数返回 -1 若给定坐标无效 (没有列)。

另请参阅 rowAt ().

[protected slot] void QTableView:: columnCountChanged ( int oldCount , int newCount )

This slot is called whenever columns are added or deleted. The previous number of columns is specified by oldCount , and the new number of columns is specified by newCount .

[protected slot] void QTableView:: columnMoved ( int column , int oldIndex , int newIndex )

此槽被调用以改变索引为给定 column 在表格视图。旧索引指定通过 oldIndex ,和新索引通过 newIndex .

另请参阅 rowMoved ().

[protected slot] void QTableView:: columnResized ( int column , int oldWidth , int newWidth )

调用此槽改变宽度为给定 column 。旧宽度的指定通过 oldWidth ,和新宽度通过 newWidth .

另请参阅 rowResized ().

int QTableView:: columnSpan ( int row , int column ) const

Returns the column span of the table element at ( row , column )。默认为 1。

该函数在 Qt 4.2 引入。

另请参阅 setSpan () 和 rowSpan ().

int QTableView:: columnViewportPosition ( int column ) const

Returns the x-coordinate in contents coordinates of the given column .

int QTableView:: columnWidth ( int column ) const

返回宽度为给定 column .

另请参阅 setColumnWidth (), resizeColumnToContents (),和 rowHeight ().

[override virtual protected] void QTableView:: currentChanged (const QModelIndex & current , const QModelIndex & previous )

重实现自 QAbstractItemView::currentChanged ().

[slot] void QTableView:: hideColumn ( int column )

隐藏给定 column .

另请参阅 showColumn () 和 hideRow ().

[slot] void QTableView:: hideRow ( int row )

隐藏给定 row .

另请参阅 showRow () 和 hideColumn ().

QHeaderView *QTableView:: horizontalHeader () const

返回表格视图的水平 Header 头。

另请参阅 setHorizontalHeader (), verticalHeader (),和 QAbstractItemModel::headerData ().

[override virtual protected] int QTableView:: horizontalOffset () const

重实现自 QAbstractItemView::horizontalOffset ().

返回表格视图中项的水平偏移。

Note that the table view uses the horizontal header section positions to determine the positions of columns in the view.

另请参阅 verticalOffset ().

[override virtual] QModelIndex QTableView:: indexAt (const QPoint & pos ) const

重实现自 QAbstractItemView::indexAt ().

Returns the index position of the model item corresponding to the table item at position pos in contents coordinates.

bool QTableView:: isColumnHidden ( int column ) const

返回 true 若给定 column 被隐藏;否则返回 false .

另请参阅 isRowHidden ().

[override virtual protected] bool QTableView:: isIndexHidden (const QModelIndex & index ) const

重实现自 QAbstractItemView::isIndexHidden ().

bool QTableView:: isRowHidden ( int row ) const

返回 true 若给定 row 被隐藏;否则返回 false .

另请参阅 isColumnHidden ().

[override virtual protected] QModelIndex QTableView:: moveCursor ( QAbstractItemView::CursorAction cursorAction , Qt::KeyboardModifiers modifiers )

重实现自 QAbstractItemView::moveCursor ().

Moves the cursor in accordance with the given cursorAction , using the information provided by the modifiers .

另请参阅 QAbstractItemView::CursorAction .

[override virtual protected] void QTableView:: paintEvent ( QPaintEvent * event )

重实现自 QAbstractScrollArea::paintEvent ().

描绘表格当收到给定描绘事件 event .

[slot] void QTableView:: resizeColumnToContents ( int column )

重置大小为给定 column based on the size hints of the delegate used to render each item in the column.

注意: Only visible columns will be resized. Reimplement sizeHintForColumn () to resize hidden columns as well.

另请参阅 resizeColumnsToContents (), sizeHintForColumn (),和 QHeaderView::resizeContentsPrecision ().

[slot] void QTableView:: resizeColumnsToContents ()

基于用于渲染列中各项的委托的大小提示,重置所有列的大小。

另请参阅 resizeColumnToContents (), sizeHintForColumn (),和 QHeaderView::resizeContentsPrecision ().

[slot] void QTableView:: resizeRowToContents ( int row )

重置大小为给定 row based on the size hints of the delegate used to render each item in the row.

另请参阅 resizeRowsToContents (), sizeHintForRow (),和 QHeaderView::resizeContentsPrecision ().

[slot] void QTableView:: resizeRowsToContents ()

Resizes all rows based on the size hints of the delegate used to render each item in the rows.

另请参阅 resizeRowToContents (), sizeHintForRow (),和 QHeaderView::resizeContentsPrecision ().

int QTableView:: rowAt ( int y ) const

返回在给定 Y 坐标处的行, y ,按内容坐标定位。

注意: 此函数返回 -1 若给定坐标无效 (没有行)。

另请参阅 columnAt ().

[protected slot] void QTableView:: rowCountChanged ( int oldCount , int newCount )

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 .

int QTableView:: rowHeight ( int row ) const

返回高度为给定 row .

另请参阅 setRowHeight (), resizeRowToContents (),和 columnWidth ().

[protected slot] void QTableView:: rowMoved ( int row , int oldIndex , int newIndex )

此槽被调用以改变索引为给定 row 在表格视图。旧索引指定通过 oldIndex ,和新索引通过 newIndex .

另请参阅 columnMoved ().

[protected slot] void QTableView:: rowResized ( int row , int oldHeight , int newHeight )

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 .

另请参阅 columnResized ().

int QTableView:: rowSpan ( int row , int column ) const

返回表格元素的行跨度在 ( row , column )。默认为 1。

该函数在 Qt 4.2 引入。

另请参阅 setSpan () 和 columnSpan ().

int QTableView:: rowViewportPosition ( int row ) const

以内容坐标形式返回 Y 坐标为给定 row .

[slot] void QTableView:: selectColumn ( int column )

选择给定 column 在表格视图若当前 SelectionMode and SelectionBehavior 允许选中列。

另请参阅 selectRow ().

[slot] void QTableView:: selectRow ( int row )

选择给定 row 在表格视图若当前 SelectionMode and SelectionBehavior 允许选中行。

另请参阅 selectColumn ().

[override virtual protected] QModelIndexList QTableView:: selectedIndexes () const

重实现自 QAbstractItemView::selectedIndexes ().

[override virtual protected] void QTableView:: selectionChanged (const QItemSelection & selected , const QItemSelection & deselected )

重实现自 QAbstractItemView::selectionChanged ().

void QTableView:: setColumnHidden ( int column , bool hide )

hide is true the given column will be hidden; otherwise it will be shown.

另请参阅 isColumnHidden () 和 setRowHidden ().

void QTableView:: setColumnWidth ( int column , int width )

设置宽度为给定 column width .

该函数在 Qt 4.1 引入。

另请参阅 columnWidth ().

void QTableView:: setHorizontalHeader ( QHeaderView * header )

将用于水平 Header 头部的 Widget 设为 header .

另请参阅 horizontalHeader () 和 setVerticalHeader ().

[override virtual] void QTableView:: setModel ( QAbstractItemModel * model )

重实现自 QAbstractItemView::setModel ().

[override virtual] void QTableView:: setRootIndex (const QModelIndex & index )

重实现自 QAbstractItemView::setRootIndex ().

void QTableView:: setRowHeight ( int row , int height )

设置高度为给定 row height .

该函数在 Qt 4.1 引入。

另请参阅 rowHeight ().

void QTableView:: setRowHidden ( int row , bool hide )

hide 为 true row 将被隐藏,否则它将被展示。

另请参阅 isRowHidden () 和 setColumnHidden ().

[override virtual protected] void QTableView:: setSelection (const QRect & rect , QItemSelectionModel::SelectionFlags flags )

重实现自 QAbstractItemView::setSelection ().

Selects the items within the given rect and in accordance with the specified selection flags .

[override virtual] void QTableView:: setSelectionModel ( QItemSelectionModel * selectionModel )

重实现自 QAbstractItemView::setSelectionModel ().

void QTableView:: setSortingEnabled ( bool enable )

enable is true, enables sorting for the table and immediately trigger a call to sortByColumn () with the current sort section and order

注意: Setter 函数对于特性 sortingEnabled .

另请参阅 isSortingEnabled ().

void QTableView:: setSpan ( int row , int column , int rowSpanCount , int columnSpanCount )

Sets the span of the table element at ( row , column ) to the number of rows and columns specified by ( rowSpanCount , columnSpanCount ).

该函数在 Qt 4.2 引入。

另请参阅 rowSpan () 和 columnSpan ().

void QTableView:: setVerticalHeader ( QHeaderView * header )

将用于垂直 Header 头部的 Widget 设为 header .

另请参阅 verticalHeader () 和 setHorizontalHeader ().

[slot] void QTableView:: showColumn ( int column )

展示给定 column .

另请参阅 hideColumn () 和 showRow ().

[slot] void QTableView:: showRow ( int row )

展示给定 row .

另请参阅 hideRow () 和 showColumn ().

[override virtual protected] int QTableView:: sizeHintForColumn ( int column ) const

重实现自 QAbstractItemView::sizeHintForColumn ().

返回大小提示为给定 column 的宽度或 -1 若没有模型。

若需要将给定列宽度设为固定值,调用 QHeaderView::resizeSection () on the table's horizontal header.

If you reimplement this function in a subclass, note that the value you return will be used when resizeColumnToContents () 或 QHeaderView::resizeSections () is called. If a larger column width is required by either the horizontal header or the item delegate, the larger width will be used instead.

另请参阅 QWidget::sizeHint , horizontalHeader (),和 QHeaderView::resizeContentsPrecision ().

[override virtual protected] int QTableView:: sizeHintForRow ( int row ) const

重实现自 QAbstractItemView::sizeHintForRow ().

返回大小提示为给定 row 的高度或 -1 若没有模型。

If you need to set the height of a given row to a fixed value, call QHeaderView::resizeSection () on the table's vertical header.

If you reimplement this function in a subclass, note that the value you return is only used when resizeRowToContents () is called. In that case, if a larger row height is required by either the vertical header or the item delegate, that width will be used instead.

另请参阅 QWidget::sizeHint , verticalHeader (),和 QHeaderView::resizeContentsPrecision ().

void QTableView:: sortByColumn ( int column , Qt::SortOrder order )

Sorts the model by the values in the given column 以给定 order .

该函数在 Qt 4.2 引入。

另请参阅 sortingEnabled .

[override virtual protected] void QTableView:: timerEvent ( QTimerEvent * event )

重实现自 QAbstractItemView::timerEvent ().

[override virtual protected] void QTableView:: updateGeometries ()

重实现自 QAbstractItemView::updateGeometries ().

QHeaderView *QTableView:: verticalHeader () const

返回表格视图的垂直 Header 头部。

另请参阅 setVerticalHeader (), horizontalHeader (),和 QAbstractItemModel::headerData ().

[override virtual protected] int QTableView:: verticalOffset () const

重实现自 QAbstractItemView::verticalOffset ().

Returns the vertical offset of the items in the table view.

Note that the table view uses the vertical header section positions to determine the positions of rows in the view.

另请参阅 horizontalOffset ().

[override virtual protected] QStyleOptionViewItem QTableView:: viewOptions () const

重实现自 QAbstractItemView::viewOptions ().

[override virtual protected] QSize QTableView:: viewportSizeHint () const

重实现自 QAbstractItemView::viewportSizeHint ().