Qt Toolkit - QTableView Class

| Classes - Annotated - Tree - Functions - Home - Structure

# QTableView Class Reference

This is an abstract base class for implementing tables More...

`#include <qtableview.h>`

Inherits QFrame.

Inherited by QMultiLineEdit.

List of all member functions.

## Public Members

- virtual void setBackgroundColor ( const QColor & ) (internal)
- void repaint ( bool erase=TRUE )
- void repaint ( int x, int y, int w, int h, bool erase=TRUE )
- void repaint ( const QRect &, bool erase=TRUE )

## Protected Members

- QTableView ( QWidget * parent=0, const char * name=0, WFlags f=0 )
- ~QTableView ()
- int numRows () const
- virtual void setNumRows ( int )
- int numCols () const
- virtual void setNumCols ( int )
- int topCell () const
- virtual void setTopCell ( int row )
- int leftCell () const
- virtual void setLeftCell ( int col )
- virtual void setTopLeftCell ( int row, int col )
- int xOffset () const
- virtual void setXOffset ( int )
- int yOffset () const
- virtual void setYOffset ( int )
- virtual void setOffset ( int x, int y, bool updateScrBars = TRUE )
- virtual int cellWidth ( int col )
- virtual int cellHeight ( int row )
- int cellWidth () const
- int cellHeight () const
- virtual void setCellWidth ( int )
- virtual void setCellHeight ( int )
- virtual int totalWidth ()
- virtual int totalHeight ()
- uint tableFlags () const
- bool testTableFlags ( uint f ) const
- virtual void setTableFlags ( uint f )
- void clearTableFlags ( uint f = ~0 )
- bool autoUpdate () const
- virtual void setAutoUpdate ( bool )
- void updateCell ( int row, int column, bool erase=TRUE )
- QRect cellUpdateRect () const
- QRect viewRect () const
- int lastRowVisible () const
- int lastColVisible () const
- bool rowIsVisible ( int row ) const
- bool colIsVisible ( int col ) const
- QScrollBar* verticalScrollBar () const
- QScrollBar* horizontalScrollBar () const
- virtual void paintCell ( QPainter *, int row, int col )
- virtual void setupPainter ( QPainter * )
- virtual void paintEvent ( QPaintEvent * )
- int findRow ( int yPos ) const
- int findCol ( int xPos ) const
- bool rowYPos ( int row, int * yPos ) const
- bool colXPos ( int col, int * xPos ) const
- int maxXOffset ()
- int maxYOffset ()
- int maxColOffset ()
- int maxRowOffset ()
- int minViewX () const
- int minViewY () const
- int maxViewX () const
- int maxViewY () const
- int viewWidth () const
- int viewHeight () const
- void scroll ( int xPixels, int yPixels )
- void updateScrollBars ()
- void updateTableSize ()

## Detailed Description

This is an abstract base class for implementing tables

A table view consists of a number of abstract cells organized in rows and columns and a visible part called a view. The cells are identified with a row index and a column index. The top left cell is in row 0, column 0.

The behavior of the widget can be finely tuned using setTableFlags(); a typical subclass will consist of little more than a call to setTableFlags(), some table content manipulation, and an implementation of paintCell(). Subclasses that need cells with variable width or height must reimplement cellHeight() and/or cellWidth(). Use updateTableSize() to tell QTableView when the width or height has changed.

When you read this documentation, it is important to understand the distinctions between the four pixel coordinate systems involved.

1. The cell coordinates. (0,0) is the top left corner of a cell. This is used by functions such as paintCell().
2. The table coordinates. (0,0) is the top left corner of the cell at row 0 and column 0. These coordinates are absolute; that is, they are independent of what part of the table is visible at the moment. This is used by functions such as setXOffset() or maxYOffset().
3. The widget coordinates. (0,0) is the top left corner of the widget, including the frame. This is used by functions such as repaint().
4. The view coordinates. (0,0) is the top left corner of the view, excluding the frame. This is the least-used coordinate system, used by functions such as viewWidth().

It is rather unfortunate that we have to use four different coordinate systems, but if we were to provide a flexible and powerful base class, there wasn't any way around it.

Note: The row,column indices are always given in that order, i.e. first the vertical (row), then the horizontal (column). This is the opposite order of all pixel operations, which take first the horizontal (x), then the vertical (y).

Warning: the functions setNumRows(), setNumCols(), setCellHeight(), setCellWidth(), setTableFlags() and clearTableFlags() may cause virtual functions like cellWidth() and cellHeight() to be called, even if autoUpdate() is FALSE. This may cause errors if relevant state variables are not initialized.

Warning: Experience has shown that use of this widget tends to bring more bugs than expected, and our analysis indicates that widget's very flexibility is the problem. If QScrollView or QListBox can easily be made to do the job you need, we recommend subclassing those widgets rather than QTableView. In addition, QScrollView makes it easy to have child widgets inside tables, something QTableView doesn't support at all.

See also QScrollView and GUI Design Handbook: Table

## Member Function Documentation

### QTableView::QTableView ( QWidget * parent=0, const char * name=0, WFlags f=0 ) `[protected]`

Constructs a table view. All the arguments are passed to the QFrame constructor.

The table flags are all cleared (set to zero). Set `Tbl_autoVScrollBar` or `Tbl_autoHScrollBar` to get automatic scroll bars and `Tbl_clipCellPainting` to get safe clipping.

The cell height and cell width are set to 0.

Frame line shapes (QFrame::HLink and QFrame::VLine) are disallowed, see QFrame::setFrameStyle().

Note that the f argument is not table flags but rather widget flags.

### QTableView::~QTableView () `[protected]`

Destructs the table view.

### bool QTableView::autoUpdate () const `[protected]`

Returns TRUE if the view updates itself automatically whenever it is changed in some way.

See also setAutoUpdate().

### int QTableView::cellHeight () const `[protected]`

Returns the row height, in pixels. Returns 0 if the rows have variable heights.

See also setCellHeight() and cellWidth().

### int QTableView::cellHeight ( int ) `[virtual protected]`

Returns the height of row row, in pixels.

This function is virtual and must be reimplemented by subclasses that have variable cell heights. Note that if the total table height changes, updateTableSize() must be called.

See also setCellHeight(), cellWidth() and totalHeight().

### QRect QTableView::cellUpdateRect () const `[protected]`

This function should only be called from the paintCell() function in subclasses. It returns the portion of a cell that actually needs to be updated, in cell coordinates. This is only useful for non-trivial paintCell().

### int QTableView::cellWidth () const `[protected]`

Returns the column width, in pixels. Returns 0 if the columns have variable widths.

See also setCellWidth() and cellHeight().

### int QTableView::cellWidth ( int ) `[virtual protected]`

Returns the width of column col, in pixels.

This function is virtual and must be reimplemented by subclasses that have variable cell widths. Note that if the total table width changes, updateTableSize() must be called.

See also setCellWidth(), cellHeight(), totalWidth() and updateTableSize().

### void QTableView::clearTableFlags ( uint f = ~0 ) `[protected]`

Clears the table flags that are set in f.

Example (clears a single flag):

        clearTableFlags( Tbl_snapToGrid );

The default argument clears all flags.

See also setTableFlags(), testTableFlags() and tableFlags().

### bool QTableView::colIsVisible ( int col ) const `[protected]`

Returns TRUE if col is at least partially visible.

See also rowIsVisible().

### bool QTableView::colXPos ( int col, int * xPos ) const `[protected]`

Computes the position in the widget of column column.

Returns TRUE and stores the result in *xPos (in widget coordinates) if the column is visible. Returns FALSE and does not modify *xPos if col is invisible or invalid.

See also rowYPos() and findCol().

### int QTableView::findCol ( int xPos ) const `[protected]`

Returns the index of the column at position xPos, where xPos is in widget coordinates. Returns -1 if xPos is outside the valid range.

See also findRow() and colXPos().

### int QTableView::findRow ( int yPos ) const `[protected]`

Returns the index of the row at position yPos, where yPos is in widget coordinates. Returns -1 if yPos is outside the valid range.

See also findCol() and rowYPos().

### QScrollBar * QTableView::horizontalScrollBar () const `[protected]`

Returns a pointer to the horizontal scroll bar, mainly so you can connect() to its signals. Note that the scroll bar works in pixel values, use findCol() to translate to cell numbers.

### int QTableView::lastColVisible () const `[protected]`

Returns the index of the last (right) column in the view. The index of the first column is 0.

If no columns are visible it returns -1. This can happen if the view is too narrow for the first column and Tbl_cutCellsH is set.

See also lastRowVisible().

### int QTableView::lastRowVisible () const `[protected]`

Returns the index of the last (bottom) row in the view. The index of the first row is 0.

If no rows are visible it returns -1. This can happen if the view is too small for the first row and Tbl_cutCellsV is set.

See also lastColVisible().

### int QTableView::leftCell () const `[protected]`

Returns the index of the first column in the table that is visible in the view. The index of the very leftmost column is 0.

See also topCell() and setLeftCell().

### int QTableView::maxColOffset () `[protected]`

Returns the index of the last column which may be at the left edge of the view.

Depending on the Tbl_scrollLastHCell flag, this may or may not be the last column.

See also maxXOffset() and maxRowOffset().

### int QTableView::maxRowOffset () `[protected]`

Returns the index of the last row which may be at the top edge of the view.

Depending on the Tbl_scrollLastVCell flag, this may or may not be the last row.

See also maxYOffset() and maxColOffset().

### int QTableView::maxViewX () const `[protected]`

Returns the rightmost pixel of the table view in view coordinates. This excludes the frame and any scroll bar, but includes blank pixels to the right of the visible table data.

See also maxViewY(), viewWidth() and contentsRect().

### int QTableView::maxViewY () const `[protected]`

Returns the bottom pixel of the table view in view coordinates. This excludes the frame and any scroll bar, but includes blank pixels below the visible table data.

See also maxViewX(), viewHeight() and contentsRect().

### int QTableView::maxXOffset () `[protected]`

Returns the maximum horizontal offset within the table of the view's left edge, in table coordinates.

This is used mainly to set the horizontal scroll bar's range.

See also maxColOffset(), maxYOffset() and totalWidth().

### int QTableView::maxYOffset () `[protected]`

Returns the maximum vertical offset within the table of the view's top edge, in table coordinates.

This is used mainly to set the vertical scroll bar's range.

See also maxRowOffset(), maxXOffset() and totalHeight().

### int QTableView::minViewX () const `[protected]`

Returns the leftmost pixel of the table view in view coordinates. This excludes the frame and any header.

See also maxViewY(), viewWidth() and contentsRect().

### int QTableView::minViewY () const `[protected]`

Returns the top pixel of the table view in view coordinates. This excludes the frame and any header.

See also maxViewX(), viewHeight() and contentsRect().

### int QTableView::numCols () const `[protected]`

Returns the number of columns in the table

See also numRows() and setNumCols().

### int QTableView::numRows () const `[protected]`

Returns the number of rows in the table.

See also numCols() and setNumRows().

### void QTableView::paintCell ( QPainter * p, int row, int col ) `[virtual protected]`

This pure virtual function is called to paint the single cell at (row,col) using p, which is open when paintCell() is called and must remain open.

The coordinate system is translated such that the origin is at the top left corner of the cell to be painted; i.e. cell coordinates. Do not scale or shear the coordinate system (or if you do, restore the transformation matrix before you return).

By default, the painter is not clipped, for maximum efficiency. For safety, call setTableFlags(Tbl_clipCellPainting) to enable clipping.

See also paintEvent(), QPainter() and setTableFlags().

Reimplemented in QMultiLineEdit.

### void QTableView::paintEvent ( QPaintEvent * e ) `[virtual protected]`

Handles paint events for the table view.

Calls paintCell() for the cells that needs to be repainted.

Reimplemented from QWidget.

### void QTableView::repaint ( int x, int y, int w, int h, bool erase=TRUE )

Repaints the table view directly by calling paintEvent() directly, unless updates are disabled.

Erases the view area (x,y,w,h) if erase is TRUE. Parameters (x,y) are in widget coordinates.

If w is negative, it is replaced with `width() - x`. If h is negative, it is replaced width `height() - y`.

Doing a repaint() usually is faster than doing an update(), but calling update() many times in a row will generate a single paint event.

At present, QTableView is the only widget that reimplements repaint(). It does this because by clearing and then repainting one cell at at time, it can make the screen flicker less than it would otherwise.

### void QTableView::repaint ( bool erase=TRUE )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Repaints the entire view.

### void QTableView::repaint ( const QRect & r, bool erase=TRUE )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

### void QTableView::resizeEvent ( QResizeEvent * ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### bool QTableView::rowIsVisible ( int row ) const `[protected]`

Returns TRUE if row is at least partially visible.

See also colIsVisible().

### bool QTableView::rowYPos ( int row, int * yPos ) const `[protected]`

Computes the position in the widget of row row.

Returns TRUE and stores the result in *yPos (in widget coordinates) if the row is visible. Returns FALSE and does not modify *yPos if row is invisible or invalid.

See also colXPos() and findRow().

### void QTableView::scroll ( int xPixels, int yPixels ) `[protected]`

Moves the visible area of the table rightwards by xPixels and downwards by yPixels pixels. Both may be negative.

Warning: You might find that QScrollView offers a higher-level of functionality than using QTableView and this function.

This function is not the same as QWidget::scroll(), in particular, the signs of xPixels and yPixels have the reverse semantics.

See also setXOffset(), setYOffset(), setOffset(), setTopCell(), setLeftCell() and setTopLeftOffset().

### void QTableView::setAutoUpdate ( bool enable ) `[virtual protected]`

Sets the auto-update option of the table view to enable.

If enable is TRUE (this is the default) then the view updates itself automatically whenever it has changed in some way (for example when a flag is changed).

If enable is FALSE, the view does NOT repaint itself, or update its internal state variables itself when it is changed. This can be useful to avoid flicker during large changes, and is singularly useless otherwise: Disable auto-update, do the changes, re-enable auto-update, and call repaint().

Warning: Do not leave the view in this state for a long time (i.e. between events ). If, for example, the user interacts with the view when auto-update is off, strange things can happen.

Setting auto-update to TRUE does not repaint the view, you must call repaint() to do this.

See also autoUpdate() and repaint().

### void QTableView::setCellHeight ( int cellHeight ) `[virtual protected]`

Sets the height in pixels of the table cells to cellHeight.

Setting it to zero means that the row height is variable. When set to 0 (this is the default) QTableView calls the virtual function cellHeight() to get the height.

See also cellHeight(), setCellWidth(), totalHeight() and numRows().

### void QTableView::setCellWidth ( int cellWidth ) `[virtual protected]`

Sets the width in pixels of the table cells to cellWidth.

Setting it to zero means that the column width is variable. When set to 0 (this is the default) QTableView calls the virtual function cellWidth() to get the width.

See also cellWidth(), setCellHeight(), totalWidth() and numCols().

### void QTableView::setLeftCell ( int col ) `[virtual protected]`

Scrolls the table such that col becomes the leftmost column. The index of the very leftmost column is 0.

See also setXOffset(), setTopLeftCell() and setTopCell().

### void QTableView::setNumCols ( int cols ) `[virtual protected]`

Sets the number of columns of the table to cols (must be non-negative). Does not change leftCell().

The table repaints itself automatically if autoUpdate() is set.

See also numCols(), numRows() and setNumRows().

### void QTableView::setNumRows ( int rows ) `[virtual protected]`

Sets the number of rows of the table to rows (must be non-negative). Does not change topCell().

The table repaints itself automatically if autoUpdate() is set.

See also numCols(), setNumCols() and numRows().

### void QTableView::setOffset ( int x, int y, bool updateScrBars = TRUE ) `[virtual protected]`

Scrolls the table such that (x,y) becomes the top left pixel in the view. Parameters (x,y) are in table coordinates.

The interaction with Tbl_snapTo*Grid is tricky. If updateScrBars is TRUE, the scroll bars are updated.

See also xOffset(), yOffset(), setXOffset(), setYOffset() and setTopLeftCell().

### void QTableView::setPalette ( const QPalette & p ) `[virtual]`

Reimplemented for internal reasons; the API is not affected.

### void QTableView::setTableFlags ( uint f ) `[virtual protected]`

Sets the table flags to f.

If a flag setting changes the appearance of the table the table is repainted if and only if autoUpdate() is TRUE.

The table flags are mostly single bits, though there are some multibit flags for convenience. Here is a complete list:

Tbl_vScrollBar
  The table has a vertical scroll bar.
Tbl_hScrollBar
  The table has a horizontal scroll bar.
Tbl_autoVScrollBar
  The table has a vertical scroll bar if and only if the table is taller than the view.
Tbl_autoHScrollBar
  The table has a horizontal scroll bar if and only if the table is wider than the view.
Tbl_autoScrollBars
  The union of the previous two flags.
Tbl_clipCellPainting
  The table uses QPainter::setClipRect() to make sure that paintCell() will not draw outside the cell boundaries.
Tbl_cutCellsV
  The table will never show part of a cell at the bottom of the table; if there is not space for all of a cell the space is left blank.
Tbl_cutCellsH
  The table will never show part of a cell at the right side of the table; if there is not space for all of a cell the space is left blank.
Tbl_cutCells
  The union of the previous two flags.
Tbl_scrollLastHCell
  When the user scrolls horizontally, let him/her scroll the last cell leftward until it is at the left edge of the view. If this flag is not set, the user can only scroll to the point where last cell is completely visible.
Tbl_scrollLastVCell
  When the user scrolls vertically, let him/her scroll the last cell upward until it is at the top edge of the view. If this flag is not set, the user can only scroll to the point where last cell is completely visible.
Tbl_scrollLastCell
  The union of the previous two flags.
Tbl_smoothHScrolling
  The table scrolls as smoothly as possible when the user scrolls horizontally. When this flag is not set scrolling is done one cell at a time.
Tbl_smoothVScrolling
  The table scrolls as smoothly as possible when scrolling vertically. When this flag is not set scrolling is done one cell at a time.
Tbl_smoothScrolling
  The union of of previous two flags.
Tbl_snapToHGrid
  Except when the user is actually scrolling, the leftmost column shown snaps to the leftmost edge of the view.
Tbl_snapToVGrid
  Except when the user is actually scrolling, the top row snaps to the top edge of the view.
Tbl_snapToGrid
  The union of the previous two flags.

You can specify more than one flag at a time using bitwise OR.

Example:

        setTableFlags( Tbl_smoothScrolling | Tbl_autoScrollBars );

Warning: The cutCells options (`Tbl_cutCells, Tbl_cutCellsH` and Tbl_cutCellsV) may cause painting problems when scrollbars are enabled. Do not combine cutCells and scrollbars.

See also clearTableFlags(), testTableFlags() and tableFlags().

### void QTableView::setTopCell ( int row ) `[virtual protected]`

Scrolls the table such that row becomes the top row. The index of the very first row is 0.

See also setYOffset(), setTopLeftCell() and setLeftCell().

### void QTableView::setTopLeftCell ( int row, int col ) `[virtual protected]`

Scrolls the table such that the cell at row row and colum col becomes the top left cell in the view. The cell at the extreme top left of the table is at position (0,0).

See also setLeftCell(), setTopCell() and setOffset().

### void QTableView::setXOffset ( int x ) `[virtual protected]`

Scrolls the table such that x becomes the leftmost pixel in the view. The x parameter is in table coordinates.

The interaction with Tbl_snapToHGrid is tricky.

See also xOffset(), setYOffset(), setOffset() and setLeftCell().

### void QTableView::setYOffset ( int y ) `[virtual protected]`

Scrolls the table such that y becomes the top pixel in the view. The y parameter is in table coordinates.

The interaction with Tbl_snapToVGrid is tricky.

See also yOffset(), setXOffset(), setOffset() and setTopCell().

### void QTableView::setupPainter ( QPainter * ) `[virtual protected]`

This virtual function is called before painting of table cells is started. It can be reimplemented by subclasses that want to to set up the painter in a special way and that do not want to do so for each cell.

### void QTableView::show () `[virtual]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### uint QTableView::tableFlags () const `[protected]`

Returns the union of the table flags that are currently set.

See also setTableFlags(), clearTableFlags() and testTableFlags().

### bool QTableView::testTableFlags ( uint f ) const `[protected]`

Returns TRUE if any of the table flags in f are currently set, otherwise FALSE.

See also setTableFlags(), clearTableFlags() and tableFlags().

### int QTableView::topCell () const `[protected]`

Returns the index of the first row in the table that is visible in the view. The index of the very first row is 0.

See also leftCell() and setTopCell().

### int QTableView::totalHeight () `[virtual protected]`

Returns the total height of the table in pixels.

This function is virtual and should be reimplemented by subclasses that have variable cell heights and a non-trivial cellHeight() function, or a large number of rows in the table.

The default implementation may be slow for very tall tables.

See also cellHeight() and totalWidth().

### int QTableView::totalWidth () `[virtual protected]`

Returns the total width of the table in pixels.

This function is virtual and should be reimplemented by subclasses that have variable cell widths and a non-trivial cellWidth() function, or a large number of columns in the table.

The default implementation may be slow for very wide tables.

See also cellWidth() and totalHeight().

### void QTableView::updateCell ( int row, int col, bool erase=TRUE ) `[protected]`

Repaints the cell at row row, column col if it is inside the view.

If erase is TRUE, the relevant part of the view is cleared to the background color/pixmap before the contents are repainted.

See also isVisible().

### void QTableView::updateScrollBars () `[protected]`

Updates the scroll bars' contents and presence to match the table's state. Generally you should not need to call this.

See also setTableFlags().

### void QTableView::updateTableSize () `[protected]`

Updates the scroll bars and internal state.

Call this function when the table view's total size is changed; typically because the result of cellHeight() or cellWidth() have changed.

This function does not repaint the widget.

### QScrollBar * QTableView::verticalScrollBar () const `[protected]`

Returns a pointer to the vertical scroll bar, mainly so you can connect() to its signals. Note that the scroll bar works in pixel values, use findRow() to translate to cell numbers.

### int QTableView::viewHeight () const `[protected]`

Returns the height of the table view, as such, in view coordinates. This does not include any header, scroll bar or frame, but does include background pixels below the table data.

See also minViewY(), maxViewY(), viewWidth(), contentsRect() and viewRect().

### QRect QTableView::viewRect () const `[protected]`

Returns the rectangle which is the actual table, excluding any frame, in widget coordinates.

### int QTableView::viewWidth () const `[protected]`

Returns the width of the table view, as such, in view coordinates. This does not include any header, scroll bar or frame, but does include background pixels to the right of the table data.

See also minViewX(), maxViewX(), viewHeight(), contentsRect() and viewRect().

### int QTableView::xOffset () const `[protected]`

Returns the x coordinate in table coordinates of the pixel which is currently on the left edge of the view.

See also setXOffset(), yOffset() and leftCell().

### int QTableView::yOffset () const `[protected]`

Returns the y coordinate in table coordinates of the pixel which is currently on the top edge of the view.

See also setYOffset(), xOffset() and topCell().

### void QTableView::setBackgroundColor ( const QColor & c ) `[virtual]`

For internal use only.

Search the documentation, FAQ, qt-interest archive and more (uses www.trolltech.com):

This file is part of the Qt toolkit, copyright © 1995-2000 Trolltech, all rights reserved.