QTableView 類

The QTableView 類提供錶格視圖的默認模型/視圖實現。 更多...

頭: #include <QTableView>
繼承: QAbstractItemView
繼承者: QTableWidget

特性

公共函數

QTableView (QWidget * parent = 0)
~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
virtual void setModel (QAbstractItemModel * model )
virtual void setRootIndex (const QModelIndex & index )
virtual void setSelectionModel (QItemSelectionModel * selectionModel )

公共槽

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 )

重實現保護函數

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

保護槽

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 implements a table view that displays items from a model. This class is used to provide standard tables that were previously provided by the 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 坐標和列索引之間的等價轉換操作。

樣式

QTableView is styled appropriately for each platform. The following images show how it looks on three different platforms. Go to the Qt Widget 圖庫 to see its appearance in other styles.

Screenshot of a Windows XP style table view Screenshot of a Macintosh style table view Screenshot of a Plastique style table view
A Windows XP style table view. A Macintosh style table view. A Plastique style table view.

另請參閱 QTableWidget , 視圖類 , QAbstractItemModel , QAbstractItemView , 圖錶範例 , 像素器範例 ,和 錶格模型範例 .

特性文檔編製

cornerButtonEnabled : bool

This property holds whether the button in the top-left corner is enabled.

If this property is true then button in the top-left corner of the table view is enabled. Clicking on this button will select all the cells in the table view.

This property is true by default.

該特性在 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

This property holds whether the grid is shown.

If this property is true a grid is drawn for the table; if the property is false, no grid is drawn. The default value is true.

訪問函數:

bool showGrid () const
void setShowGrid (bool show )

sortingEnabled : bool

This property holds whether sorting is enabled.

If this property is 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 () 按當前排序區間和次序。

該特性在 Qt 4.2 引入。

訪問函數:

bool isSortingEnabled () const
void setSortingEnabled (bool enable )

另請參閱 sortByColumn ().

wordWrap : bool

This property holds the item text word-wrapping policy.

If this property is true then the item text is wrapped where necessary at word-breaks; otherwise it is not wrapped at all. This property is true by default.

注意,即使啓用瞭換行,也不會展開單元格以擬閤所有文本。將插入省略號根據當前 textElideMode .

該特性在 Qt 4.3 引入。

訪問函數:

bool wordWrap () const
void setWordWrap (bool on )

成員函數文檔編製

QTableView:: QTableView ( QWidget * parent = 0)

構造錶格視圖采用 parent 以錶示數據。

另請參閱 QAbstractItemModel .

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 )

此槽被調用,每當添加 (或刪除) 列時。之前列數的指定是通過 oldCount ,和新列數的指定是通過 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

返迴錶格元素的列跨度在 ( row , column )。默認為 1。

該函數在 Qt 4.2 引入。

另請參閱 setSpan () 和 rowSpan ().

int QTableView:: columnViewportPosition ( int column ) const

返迴內容坐標中的 X 坐標為給定 column .

int QTableView:: columnWidth ( int column ) const

返迴寬度為給定 column .

另請參閱 setColumnWidth (), resizeColumnToContents (),和 rowHeight ().

[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 ().

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

重實現自 QAbstractItemView::horizontalOffset ().

返迴錶格視圖中項的水平偏移。

注意,錶格視圖使用水平 Header (頭部) 區間位置,來確定視圖中列的位置。

另請參閱 verticalOffset ().

[虛擬] QModelIndex QTableView:: indexAt (const QPoint & pos ) const

重實現自 QAbstractItemView::indexAt ().

返迴對應錶格項的模型項的索引位置,基於位置 pos 按內容坐標。

bool QTableView:: isColumnHidden ( int column ) const

Returns true if the given column is hidden; otherwise returns false.

另請參閱 isRowHidden ().

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

重實現自 QAbstractItemView::isIndexHidden ().

bool QTableView:: isRowHidden ( int row ) const

Returns true if the given row is hidden; otherwise returns false.

另請參閱 isColumnHidden ().

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

重實現自 QAbstractItemView::moveCursor ().

移動光標根據給定 cursorAction ,使用信息提供通過 modifiers .

另請參閱 QAbstractItemView::CursorAction .

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

重實現自 QWidget::paintEvent ().

描繪錶格當收到給定描繪事件 event .

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

重置大小為給定 column 基於用於渲染列中各項的委托的大小提示。

注意: 僅重置可見列的大小。重實現 sizeHintForColumn () 以重置隱藏列的大小。

[slot] void QTableView:: resizeColumnsToContents ()

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

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

重置大小為給定 row 基於用於渲染行中各項的委托的大小提示。

[slot] void QTableView:: resizeRowsToContents ()

重置所有行大小,基於用於渲染行中各項的委托的大小提示。

int QTableView:: rowAt ( int y ) const

返迴在給定 Y 坐標處的行, y ,按內容坐標定位。

注意: 此函數返迴 -1 若給定坐標無效 (沒有行)。

另請參閱 columnAt ().

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

此槽被調用,每當添加 (或刪除) 行時。之前行數的指定是通過 oldCount ,和新行數的指定是通過 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 )

此槽被調用以改變高度為給定 row 。舊高度的指定是通過 oldHeight ,和新高度通過 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 ().

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

重實現自 QAbstractItemView::selectedIndexes ().

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

重實現自 QAbstractItemView::selectionChanged ().

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

hide 為 true 給定 column 將被隱藏;否則,會展示它。

另請參閱 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 ().

[虛擬] void QTableView:: setModel ( QAbstractItemModel * model )

重實現自 QAbstractItemView::setModel ().

[虛擬] 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 ().

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

重實現自 QAbstractItemView::setSelection ().

選擇項基於給定 rect 和根據指定選定 flags .

[虛擬] void QTableView:: setSelectionModel ( QItemSelectionModel * selectionModel )

重實現自 QAbstractItemView::setSelectionModel ().

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

設置錶格元素的跨度按 ( row , column ) 到行/列數指定通過 ( 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 ().

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

重實現自 QAbstractItemView::sizeHintForColumn ().

返迴大小提示為給定 column 的寬度或 -1 若沒有模型。

若需要把給定列寬度設為固定值,調用 QHeaderView::resizeSection () 在錶格水平 Header (頭部)。

若在子類中重實現此函數,注意,將使用返迴值當 resizeColumnToContents () 或 QHeaderView::resizeSections () 被調用。若水平 Header (頭部) 或項委托要求更大列寬度,將使用更大寬度代替。

另請參閱 QWidget::sizeHint and horizontalHeader ().

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

重實現自 QAbstractItemView::sizeHintForRow ().

返迴大小提示為給定 row 的高度或 -1 若沒有模型。

若需要把給定行高度設為固定值,調用 QHeaderView::resizeSection () 在錶格垂直 Header (頭部)。

若在子類中重實現此函數,注意,將僅使用返迴值當 resizeRowToContents () 被調用。在此情況下,若垂直 Header (頭部) 或項委托要求更大行高度,將使用寬度代替。

另請參閱 QWidget::sizeHint and verticalHeader ().

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

按值排序模型根據給定 column 以給定 order .

該函數在 Qt 4.2 引入。

另請參閱 sortingEnabled .

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

重實現自 QObject::timerEvent ().

[virtual protected] void QTableView:: updateGeometries ()

重實現自 QAbstractItemView::updateGeometries ().

QHeaderView * QTableView:: verticalHeader () const

返迴錶格視圖的垂直 Header (頭部)。

另請參閱 setVerticalHeader (), horizontalHeader (),和 QAbstractItemModel::headerData ().

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

重實現自 QAbstractItemView::verticalOffset ().

返迴錶格視圖中項的垂直偏移。

注意,錶格視圖使用垂直 Header (頭部) 區間位置,來確定視圖中行的位置。

另請參閱 horizontalOffset ().

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

重實現自 QAbstractItemView::viewOptions ().