QTableView Class Reference

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

  1. #include <QTableView>

Inherits: QAbstractItemView.

Inherited by: QTableWidget.

Detailed Description

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

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.

Navigation

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.

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.

Styles

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

Screenshot of a Windows XP style table view

Screenshot of a Windows XP style table view

Screenshot of a Macintosh style table view

Screenshot of a Macintosh style table view

Screenshot of a Plastique 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.

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

Properties

Toggle detailscornerButtonEnabledcornerButtonEnabled : 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.

Access functions:

Look up this member in the source code.

Toggle detailsgridStylegridStyle : 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:

Look up this member in the source code.

Toggle detailsshowGridshowGrid : 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:

Look up this member in the source code.

Toggle detailssortingEnabledsortingEnabled : 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.

See also sortByColumn().

Access functions:

Look up this member in the source code.

Toggle detailswordWrapwordWrap : 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.

Access functions:

Look up this member in the source code.

    Public Functions

    Toggle details QTableView

    QTableViewQTableView ( QWidget *parent=0 ) ( QWidget *parent=0 )

    Constructs a table view with a parent to represent the data.

    See also QAbstractItemModel.

    Look up this member in the source code.

    Toggle details QTableView

    ~QTableView~QTableView () ()

    Destroys the table view.

    Look up this member in the source code.

    Toggle details void QTableView

    clearSpansclearSpans () ()

    Removes all row and column spans in the table view.

    See also setSpan().

    Look up this member in the source code.

    Toggle details int QTableView

    columnAtcolumnAt ( int x ) ( int x )const

    Returns the column in which the given x-coordinate, x, in contents coordinates is located.

    Note: This function returns -1 if the given coordinate is not valid (has no column).

    See also rowAt().

    Look up this member in the source code.

    Toggle details int QTableView

    columnSpancolumnSpan ( int row , int column ) ( int row , int column )const

    Returns the column span of the table element at (row, column). The default is 1.

    See also setSpan() and rowSpan().

    Look up this member in the source code.

    Toggle details int QTableView

    columnViewportPositioncolumnViewportPosition ( int column ) ( int column )const

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

    Look up this member in the source code.

    Toggle details int QTableView

    columnWidthcolumnWidth ( int column ) ( int column )const

    Returns the width of the given column.

    See also setColumnWidth(), resizeColumnToContents(), and rowHeight().

    Look up this member in the source code.

    Toggle details void QTableView

    currentChangedcurrentChanged ( const QModelIndex &current , const QModelIndex &previous ...) ( const QModelIndex &current , const QModelIndex &previous ) [virtual protected]

    Reimplemented from QAbstractItemView::currentChanged().

    Look up this member in the source code.

    Toggle details QHeaderView * QTableView

    horizontalHeaderhorizontalHeader () ()const

    Returns the table view's horizontal header.

    See also setHorizontalHeader(), verticalHeader(), and QAbstractItemModel::headerData().

    Look up this member in the source code.

    Toggle details int QTableView

    horizontalOffsethorizontalOffset () ()const [virtual protected]

    Reimplemented from QAbstractItemView::horizontalOffset().

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

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

    See also verticalOffset().

    Look up this member in the source code.

    Toggle details QModelIndex QTableView

    indexAtindexAt ( const QPoint &pos ) ( const QPoint &pos )const [virtual]

    Reimplemented from QAbstractItemView::indexAt().

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

    Look up this member in the source code.

    Toggle details bool QTableView

    isColumnHiddenisColumnHidden ( int column ) ( int column )const

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

    See also isRowHidden().

    Look up this member in the source code.

    Toggle details bool QTableView

    isIndexHiddenisIndexHidden ( const QModelIndex &index ) ( const QModelIndex &index )const [virtual protected]

    Reimplemented from QAbstractItemView::isIndexHidden().

    Look up this member in the source code.

    Toggle details bool QTableView

    isRowHiddenisRowHidden ( int row ) ( int row )const

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

    See also isColumnHidden().

    Look up this member in the source code.

    Toggle details QModelIndex QTableView

    moveCursormoveCursor ( CursorAction cursorAction , Qt::KeyboardModifiers modifiers ...) ( CursorAction cursorAction , Qt::KeyboardModifiers modifiers ) [virtual protected]

    Reimplemented from QAbstractItemView::moveCursor().

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

    See also QAbstractItemView::CursorAction.

    Look up this member in the source code.

    Toggle details void QTableView

    paintEventpaintEvent ( QPaintEvent *event ) ( QPaintEvent *event ) [virtual protected]

    Reimplemented from QWidget::paintEvent().

    Paints the table on receipt of the given paint event event.

    Look up this member in the source code.

    Toggle details int QTableView

    rowAtrowAt ( int y ) ( int y )const

    Returns the row in which the given y-coordinate, y, in contents coordinates is located.

    Note: This function returns -1 if the given coordinate is not valid (has no row).

    See also columnAt().

    Look up this member in the source code.

    Toggle details int QTableView

    rowHeightrowHeight ( int row ) ( int row )const

    Returns the height of the given row.

    See also setRowHeight(), resizeRowToContents(), and columnWidth().

    Look up this member in the source code.

    Toggle details int QTableView

    rowSpanrowSpan ( int row , int column ) ( int row , int column )const

    Returns the row span of the table element at (row, column). The default is 1.

    See also setSpan() and columnSpan().

    Look up this member in the source code.

    Toggle details int QTableView

    rowViewportPositionrowViewportPosition ( int row ) ( int row )const

    Returns the y-coordinate in contents coordinates of the given row.

    Look up this member in the source code.

    Toggle details QModelIndexList QTableView

    selectedIndexesselectedIndexes () ()const [virtual protected]

    Reimplemented from QAbstractItemView::selectedIndexes().

    Look up this member in the source code.

    Toggle details void QTableView

    selectionChangedselectionChanged ( const QItemSelection &selected , const QItemSelection &deselected ...) ( const QItemSelection &selected , const QItemSelection &deselected ) [virtual protected]

    Reimplemented from QAbstractItemView::selectionChanged().

    Look up this member in the source code.

    Toggle details void QTableView

    setColumnHiddensetColumnHidden ( int column , bool hide ) ( int column , bool hide )

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

    See also isColumnHidden() and setRowHidden().

    Look up this member in the source code.

    Toggle details void QTableView

    setColumnWidthsetColumnWidth ( int column , int width ) ( int column , int width )

    Sets the width of the given column to be width.

    See also columnWidth().

    Look up this member in the source code.

    Toggle details void QTableView

    setHorizontalHeadersetHorizontalHeader ( QHeaderView *header ) ( QHeaderView *header )

    Sets the widget to use for the horizontal header to header.

    See also horizontalHeader() and setVerticalHeader().

    Look up this member in the source code.

    Toggle details void QTableView

    setModelsetModel ( QAbstractItemModel *model ) ( QAbstractItemModel *model ) [virtual]

    Reimplemented from QAbstractItemView::setModel().

    Look up this member in the source code.

    Toggle details void QTableView

    setRootIndexsetRootIndex ( const QModelIndex &index ) ( const QModelIndex &index ) [virtual]

    Reimplemented from QAbstractItemView::setRootIndex().

    Look up this member in the source code.

    Toggle details void QTableView

    setRowHeightsetRowHeight ( int row , int height ) ( int row , int height )

    Sets the height of the given row to be height.

    See also rowHeight().

    Look up this member in the source code.

    Toggle details void QTableView

    setRowHiddensetRowHidden ( int row , bool hide ) ( int row , bool hide )

    If hide is true row will be hidden, otherwise it will be shown.

    See also isRowHidden() and setColumnHidden().

    Look up this member in the source code.

    Toggle details void QTableView

    setSelectionsetSelection ( const QRect &rect , QItemSelectionModel::SelectionFlags flags ...) ( const QRect &rect , QItemSelectionModel::SelectionFlags flags ) [virtual protected]

    Reimplemented from QAbstractItemView::setSelection().

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

    Look up this member in the source code.

    Toggle details void QTableView

    setSelectionModelsetSelectionModel ( QItemSelectionModel *selectionModel ...) ( QItemSelectionModel *selectionModel ) [virtual]

    Reimplemented from QAbstractItemView::setSelectionModel().

    Look up this member in the source code.

    Toggle details void QTableView

    setSpansetSpan ( int row , int column , int rowSpanCount , int columnSpanCount ...) ( 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).

    See also rowSpan() and columnSpan().

    Look up this member in the source code.

    Toggle details void QTableView

    setVerticalHeadersetVerticalHeader ( QHeaderView *header ) ( QHeaderView *header )

    Sets the widget to use for the vertical header to header.

    See also verticalHeader() and setHorizontalHeader().

    Look up this member in the source code.

    Toggle details int QTableView

    sizeHintForColumnsizeHintForColumn ( int column ) ( int column )const [virtual protected]

    Reimplemented from QAbstractItemView::sizeHintForColumn().

    Returns the size hint for the given column's width or -1 if there is no model.

    If you need to set the width of a given column to a fixed value, call 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() or 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.

    See also QWidget::sizeHint and horizontalHeader().

    Look up this member in the source code.

    Toggle details int QTableView

    sizeHintForRowsizeHintForRow ( int row ) ( int row )const [virtual protected]

    Reimplemented from QAbstractItemView::sizeHintForRow().

    Returns the size hint for the given row's height or -1 if there is no model.

    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.

    See also QWidget::sizeHint and verticalHeader().

    Look up this member in the source code.

    Toggle details void QTableView

    sortByColumnsortByColumn ( int column , Qt::SortOrder order ( int column , Qt::SortOrder order )

    Sorts the model by the values in the given column in the given order.

    See also sortingEnabled.

    Look up this member in the source code.

    Toggle details void QTableView

    timerEventtimerEvent ( QTimerEvent *event ) ( QTimerEvent *event ) [virtual protected]

    Reimplemented from QObject::timerEvent().

    Look up this member in the source code.

    Toggle details void QTableView

    updateGeometriesupdateGeometries () () [virtual protected]

    Reimplemented from QAbstractItemView::updateGeometries().

    Look up this member in the source code.

    Toggle details QHeaderView * QTableView

    verticalHeaderverticalHeader () ()const

    Returns the table view's vertical header.

    See also setVerticalHeader(), horizontalHeader(), and QAbstractItemModel::headerData().

    Look up this member in the source code.

    Toggle details int QTableView

    verticalOffsetverticalOffset () ()const [virtual protected]

    Reimplemented from 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.

    See also horizontalOffset().

    Look up this member in the source code.

    Toggle details QStyleOptionViewItem QTableView

    viewOptionsviewOptions () ()const [virtual protected]

    Reimplemented from QAbstractItemView::viewOptions().

    Look up this member in the source code.

    Public Slots

    Toggle details void QTableView

    columnCountChangedcolumnCountChanged ( int oldCount , int newCount ...) ( int oldCount , int newCount )[protected] [slot]

    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.

    Look up this member in the source code.

    Toggle details void QTableView

    columnMovedcolumnMoved ( int column , int oldIndex , int newIndex ...) ( int column , int oldIndex , int newIndex )[protected] [slot]

    This slot is called to change the index of the given column in the table view. The old index is specified by oldIndex, and the new index by newIndex.

    See also rowMoved().

    Look up this member in the source code.

    Toggle details void QTableView

    columnResizedcolumnResized ( int column , int oldWidth , int newWidth ...) ( int column , int oldWidth , int newWidth )[protected] [slot]

    This slot is called to change the width of the given column. The old width is specified by oldWidth, and the new width by newWidth.

    See also rowResized().

    Look up this member in the source code.

    Toggle details void QTableView

    hideColumnhideColumn ( int column ) ( int column ) [slot]

    Hide the given column.

    See also showColumn() and hideRow().

    Look up this member in the source code.

    Toggle details void QTableView

    hideRowhideRow ( int row ) ( int row ) [slot]

    Hide the given row.

    See also showRow() and hideColumn().

    Look up this member in the source code.

    Toggle details void QTableView

    resizeColumnToContentsresizeColumnToContents ( int column ) ( int column ) [slot]

    Resizes the given column based on the size hints of the delegate used to render each item in the column.

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

    Look up this member in the source code.

    Toggle details void QTableView

    resizeColumnsToContentsresizeColumnsToContents () () [slot]

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

    Look up this member in the source code.

    Toggle details void QTableView

    resizeRowToContentsresizeRowToContents ( int row ) ( int row ) [slot]

    Resizes the given row based on the size hints of the delegate used to render each item in the row.

    Look up this member in the source code.

    Toggle details void QTableView

    resizeRowsToContentsresizeRowsToContents () () [slot]

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

    Look up this member in the source code.

    Toggle details void QTableView

    rowCountChangedrowCountChanged ( int oldCount , int newCount ) ( int oldCount , int newCount )[protected] [slot]

    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.

    Look up this member in the source code.

    Toggle details void QTableView

    rowMovedrowMoved ( int row , int oldIndex , int newIndex ( int row , int oldIndex , int newIndex )[protected] [slot]

    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.

    See also columnMoved().

    Look up this member in the source code.

    Toggle details void QTableView

    rowResizedrowResized ( int row , int oldHeight , int newHeight ...) ( int row , int oldHeight , int newHeight )[protected] [slot]

    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.

    See also columnResized().

    Look up this member in the source code.

    Toggle details void QTableView

    selectColumnselectColumn ( int column ) ( int column ) [slot]

    Selects the given column in the table view if the current SelectionMode and SelectionBehavior allows columns to be selected.

    See also selectRow().

    Look up this member in the source code.

    Toggle details void QTableView

    selectRowselectRow ( int row ) ( int row ) [slot]

    Selects the given row in the table view if the current SelectionMode and SelectionBehavior allows rows to be selected.

    See also selectColumn().

    Look up this member in the source code.

    Toggle details void QTableView

    showColumnshowColumn ( int column ) ( int column ) [slot]

    Show the given column.

    See also hideColumn() and showRow().

    Look up this member in the source code.

    Toggle details void QTableView

    showRowshowRow ( int row ) ( int row ) [slot]

    Show the given row.

    See also hideRow() and showColumn().

    Look up this member in the source code.

    Toggle details void QTableView

    sortByColumnsortByColumn ( int column ) ( int column ) [slot] Obsolete function

    This is an overloaded function.

    Sorts the model by the values in the given column.

    Look up this member in the source code.

    Notes provided by the Qt Community
    Informative
    • 0

    Votes: 0

    Coverage: Qt library 4.7

    Picture of Hostel Hostel

    Lab Rat
    4 notes

    Properly use of scrollTo( const QModelIndex &index , ScrollHint hint=EnsureVisible ) when we hidden columns.

    If your index refers to column which is hidden then scrollTo doesn’t work. If you want to use this method you have to set visible for suitable column.

    1. // index refer for example to column 0(which is primary key) and row to which we want to scroll
    2. view->setColumnHidden( 0, false );
    3. view->scrollTo( myModelIndex );
    4. view->setColumnHidden( 0, true );

    [Revisions]