QTableView Class

The QTableView class provides a default model/view implementation of a table view. More...

Header: #include <QTableView>
qmake: QT += widgets
Inherits: QAbstractItemView
Inherited By:

QTableWidget

Properties

Public Functions

Qt::PenStyle gridStyle() const
bool isCornerButtonEnabled() const
bool isSortingEnabled() const
void setCornerButtonEnabled(bool enable)
void setGridStyle(Qt::PenStyle style)
void setSortingEnabled(bool enable)
void setWordWrap(bool on)
bool showGrid() const
bool wordWrap() const

Public Slots

void setShowGrid(bool show)

Detailed Description

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 class, but using the more flexible approach provided by Qt's model/view architecture.

The QTableView class is one of the Model/View Classes and is part of Qt's model/view framework.

QTableView implements the interfaces defined by the QAbstractItemView class to allow it to display data provided by models derived from the QAbstractItemModel class.

You can navigate the cells in the table by clicking on a cell with the mouse, or by using the arrow keys. Because QTableView enables tabKeyNavigation by default, you can also hit Tab and Backtab to move from cell to cell.

Visual Appearance

The table has a vertical header that can be obtained using the verticalHeader() function, and a horizontal header that is available through the horizontalHeader() function. The height of each row in the table can be found by using rowHeight(); similarly, the width of columns can be found using columnWidth(). Since both of these are plain widgets, you can hide either of them using their hide() functions. Each header is configured with its highlightSections and sectionsClickable properties set to true.

Rows and columns can be hidden and shown with hideRow(), hideColumn(), showRow(), and showColumn(). They can be selected with selectRow() and selectColumn(). The table will show a grid depending on the showGrid property.

The items shown in a table view, like those in the other item views, are rendered and edited using standard delegates. However, for some tasks it is sometimes useful to be able to insert widgets in a table instead. Widgets are set for particular indexes with the setIndexWidget() function, and later retrieved with indexWidget().

By default, the cells in a table do not expand to fill the available space.

You can make the cells fill the available space by stretching the last header section. Access the relevant header using horizontalHeader() or verticalHeader() and set the header's stretchLastSection property.

To distribute the available space according to the space requirement of each column or row, call the view's resizeColumnsToContents() or resizeRowsToContents() functions.

Coordinate Systems

For some specialized forms of tables it is useful to be able to convert between row and column indexes and widget coordinates. The rowAt() function provides the y-coordinate within the view of the specified row; the row index can be used to obtain a corresponding y-coordinate with rowViewportPosition(). The columnAt() and columnViewportPosition() functions provide the equivalent conversion operations between x-coordinates and column indexes.

See also QTableWidget, View Classes, QAbstractItemModel, QAbstractItemView, Chart Example, Pixelator Example, and Table Model Example.

Property Documentation

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.

This property was introduced in Qt 4.3.

Access functions:

bool isCornerButtonEnabled() const
void setCornerButtonEnabled(bool enable)

gridStyle : Qt::PenStyle

This property holds the pen style used to draw the grid.

This property holds the style used when drawing the grid (see showGrid).

Access functions:

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.

Access functions:

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.

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

This property was introduced in Qt 4.2.

Access functions:

bool isSortingEnabled() const
void setSortingEnabled(bool enable)

See also 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.

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.

This property was introduced in Qt 4.3.

Access functions:

bool wordWrap() const
void setWordWrap(bool on)