QTableWidget Class Reference
The QTableWidget class provides an item-based table view with a default model. More...
#include <QTableWidget>
Inherits QTableView.
Writable Properties
- 2 writable properties inherited from QTableView
- 12 writable properties inherited from QAbstractItemView
- 2 writable properties inherited from QViewport
- 5 writable properties inherited from QFrame
- 33 writable properties inherited from QWidget
- 1 writable property inherited from QObject
Read-Only Properties
- 1 read-only property inherited from QFrame
- 20 read-only properties inherited from QWidget
Public Functions
- QTableWidget ( QWidget * parent = 0 )
- QTableWidget ( int rows, int columns, QWidget * parent = 0 )
- ~QTableWidget ()
- void closePersistentEditor ( QTableWidgetItem * item )
- int column ( const QTableWidgetItem * item ) const
- int currentColumn () const
- QTableWidgetItem * currentItem () const
- int currentRow () const
- QList<QTableWidgetItem *> findItems ( const QString & text, QAbstractItemModel::MatchFlags flags = QAbstractItemModel::MatchDefault ) const
- QTableWidgetItem * horizontalHeaderItem ( int column ) const
- bool isItemVisible ( const QTableWidgetItem * item ) const
- bool isSelected ( const QTableWidgetItem * item ) const
- QTableWidgetItem * item ( int row, int column ) const
- QTableWidgetItemCreatorBase * itemCreator () const
- void openPersistentEditor ( QTableWidgetItem * item )
- void removeItem ( QTableWidgetItem * item )
- int row ( const QTableWidgetItem * item ) const
- QList<QTableWidgetItem *> selectedItems ( bool fillEmptyCells = false )
- QList<QTableWidgetSelectionRange> selectedRanges () const
- void setCurrentItem ( QTableWidgetItem * item )
- void setHorizontalHeaderItem ( int column, QTableWidgetItem * item )
- void setHorizontalHeaderLabels ( const QStringList & labels )
- void setItem ( int row, int column, QTableWidgetItem * item )
- void setItemCreator ( QTableWidgetItemCreatorBase * factory )
- void setSelected ( const QTableWidgetItem * item, bool select )
- void setSelected ( const QTableWidgetSelectionRange & range, bool select )
- void setVerticalHeaderItem ( int row, QTableWidgetItem * item )
- void setVerticalHeaderLabels ( const QStringList & labels )
- void sortItems ( int column, Qt::SortOrder order = Qt::AscendingOrder )
- QTableWidgetItem * takeItem ( int row, int column )
- QTableWidgetItem * verticalHeaderItem ( int row ) const
- int visualColumn ( const QTableWidgetItem * item ) const
- QTableWidgetItem * visualItem ( int visualRow, int visualColumn ) const
- int visualRow ( const QTableWidgetItem * item ) const
Public Slots
Signals
- void aboutToShowContextMenu ( QMenu * menu, QTableWidgetItem * item )
- void clicked ( QTableWidgetItem * item, Qt::MouseButton button, Qt::KeyboardModifiers modifiers )
- void currentChanged ( QTableWidgetItem * current, QTableWidgetItem * previous )
- void doubleClicked ( QTableWidgetItem * item, Qt::MouseButton button, Qt::KeyboardModifiers modifiers )
- void itemChanged ( QTableWidgetItem * item )
- void itemEntered ( QTableWidgetItem * item, Qt::MouseButton button, Qt::KeyboardModifiers modifiers )
- void keyPressed ( QTableWidgetItem * item, Qt::Key key, Qt::KeyboardModifiers modifiers )
- void pressed ( QTableWidgetItem * item, Qt::MouseButton button, Qt::KeyboardModifiers modifiers )
- void returnPressed ( QTableWidgetItem * item )
- void selectionChanged ()
Static Public Members
- 4 static public members inherited from QWidget
- 4 static public members inherited from QObject
Protected Functions
Protected Slots
Detailed Description
The QTableWidget class provides an item-based table view with a default model.
Table widgets provide standard table display facilities for applications. The items in a QTableWidget are provided by QTableWidgetItem.
If you want a table that uses your own data model you should use QTableView rather than this class.
Table widgets can be constructed with the required numbers of rows and columns:
tableWidget = new QTableWidget(12, 3, this);
Alternatively, tables can be constructed without a given size and resized later:
tableWidget = new QTableWidget(this);
tableWidget->setRowCount(10);
tableWidget->setColumnCount(5);
Items are created ouside the table (with no parent widget) and inserted into the table with setItem():
QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(
(row+1)*(column+1)));
tableWidget->setItem(row, column, newItem);
Tables can be given both horizontal and vertical headers. The simplest way to create the headers is to supply a list of strings to the setHorizontalHeaderLabels() and setVerticalHeaderLabels() functions. These will provide simple textual headers for the table's columns and rows. More sophisticated headers can be created from existing table items that are usually constructed outside the table. For example, we can construct a table item with an icon and aligned text, and use it as the header for a particular column:
QTableWidgetItem *cubesHeaderItem = new QTableWidgetItem(tr("Cubes"));
cubesHeaderItem->setIcon(QIcon(QPixmap(":/Images/cubed.png")));
cubesHeaderItem->setTextAlignment(Qt::AlignVCenter);
The number of rows in the table can be found with rowCount(), and the number of columns with columnCount(). The table can be cleared with the clear() function.
See also QTableWidgetItem and Model/View Programming.
Property Documentation
columnCount : int
This property holds the number of columns in the table.
Access functions:
- int columnCount () const
- void setColumnCount ( int columns )
rowCount : int
This property holds the number of rows in the table.
Access functions:
- int rowCount () const
- void setRowCount ( int rows )
sortingEnabled : bool
This property holds whether the items in the table can be sorted by clicking on the horizontal header.
Access functions:
- bool isSortingEnabled () const
- void setSortingEnabled ( bool enable )
Member Function Documentation
QTableWidget::QTableWidget ( QWidget * parent = 0 )
Creates a new table view with the given parent.
QTableWidget::QTableWidget ( int rows, int columns, QWidget * parent = 0 )
Creates a new table view with the given rows and columns, and with the given parent.
QTableWidget::~QTableWidget ()
Destroys this QTableWidget.
void QTableWidget::aboutToShowContextMenu ( QMenu * menu, QTableWidgetItem * item ) [signal]
This signal is emitted when the widget is about to show a context menu. The menu is the menu about to be shown, and the item is the clicked item the context menu was called for.
See also QMenu::addAction().
void QTableWidget::clear () [slot]
Removes all items and selections in the view.
This signal is emitted when the specified item is clicked. The state of the mouse buttons is described by button; the modifiers reflect the state of the keyboard's modifier keys.
The item may be 0 if the mouse was not clicked on an item.
void QTableWidget::closePersistentEditor ( QTableWidgetItem * item )
Closes the persistent editor for item.
See also openPersistentEditor().
int QTableWidget::column ( const QTableWidgetItem * item ) const
Returns the column for the item.
void QTableWidget::currentChanged ( QTableWidgetItem * current, QTableWidgetItem * previous ) [signal]
This signal is emitted whenever the current item changes. The previous item is the item that previously had the focus, current is the new current item.
int QTableWidget::currentColumn () const
Returns the column of the current item.
QTableWidgetItem * QTableWidget::currentItem () const
Returns the current item.
int QTableWidget::currentRow () const
Returns the row of the current item.
This signal is emitted when the specified item is double clicked. The state of the mouse buttons is described by button; the modifiers reflect the state of the keyboard's modifier keys.
The item may be 0 if the mouse was not clicked on an item.
void QTableWidget::ensureVisible ( const QTableWidgetItem * item ) [slot]
Scrolls the view if necessary to ensure that the item is visible.
Finds items that matches the text, using the criteria given in the flags (see QAbstractItemModel::MatchFlags).
QTableWidgetItem * QTableWidget::horizontalHeaderItem ( int column ) const
Returns the horizontal header item for column column.
void QTableWidget::insertColumn ( int column ) [slot]
Inserts an empty column into the table at column.
void QTableWidget::insertRow ( int row ) [slot]
Inserts an empty row into the table at row.
bool QTableWidget::isItemVisible ( const QTableWidgetItem * item ) const
Returns true if the item is in the viewport, otherwise returns false.
bool QTableWidget::isSelected ( const QTableWidgetItem * item ) const
Returns true if the item is selected, otherwise returns false.
QTableWidgetItem * QTableWidget::item ( int row, int column ) const
Returns the item for the given row and column.
See also setItem().
void QTableWidget::itemChanged ( QTableWidgetItem * item ) [signal]
This signal is emitted whenever the data of item has changed.
This signal is emitted when the mouse cursor enters an item. The item is the item entered. The state of the mouse buttons is specified by button; the modifiers reflect the state of the keyboard's modifier keys.
This signal is only emitted when mouseTracking is turned on, or when a mouse button is pressed while moving into an item.
void QTableWidget::openPersistentEditor ( QTableWidgetItem * item )
Opens an editor for the give item. The editor remains open after editing.
See also closePersistentEditor().
void QTableWidget::removeColumn ( int column ) [slot]
Removes the column column and all its items from the table.
void QTableWidget::removeItem ( QTableWidgetItem * item )
Removes item item from the table.
void QTableWidget::removeRow ( int row ) [slot]
Removes the row row and all its items from the table.
void QTableWidget::returnPressed ( QTableWidgetItem * item ) [signal]
This signal is emitted when return has been pressed on an item.
int QTableWidget::row ( const QTableWidgetItem * item ) const
Returns the row for the item.
QList<QTableWidgetItem *> QTableWidget::selectedItems ( bool fillEmptyCells = false )
Returns a list of all selected items. If a cell is empty and fillEmptyCells is true, the item is created and set in that cell.
Returns a list of all selected ranges.
See also QTableWidgetSelectionRange.
void QTableWidget::selectionChanged () [signal]
This signal is emitted whenever the selection changes.
See also selectedItems() and isSelected().
void QTableWidget::setCurrentItem ( QTableWidgetItem * item )
Sets the current item to item.
void QTableWidget::setHorizontalHeaderItem ( int column, QTableWidgetItem * item )
Sets the horizontal header item for column column to item.
void QTableWidget::setHorizontalHeaderLabels ( const QStringList & labels )
Sets the horizontal header labels using labels.
void QTableWidget::setItem ( int row, int column, QTableWidgetItem * item )
Sets the item for the given row and column to item.
See also item().
void QTableWidget::setSelected ( const QTableWidgetItem * item, bool select )
Selects or deselects item depending on select.
void QTableWidget::setSelected ( const QTableWidgetSelectionRange & range, bool select )
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Selects or deselects the range depending on select.
void QTableWidget::setVerticalHeaderItem ( int row, QTableWidgetItem * item )
Sets the vertical header item for row row to item.
void QTableWidget::setVerticalHeaderLabels ( const QStringList & labels )
Sets the vertical header labels using labels.
void QTableWidget::sortItems ( int column, Qt::SortOrder order = Qt::AscendingOrder )
Sorts all the rows in the table widget based on column and order.
QTableWidgetItem * QTableWidget::takeItem ( int row, int column )
Removes the item at row and column from the table without deleting it.
QTableWidgetItem * QTableWidget::verticalHeaderItem ( int row ) const
Returns the vertical header item for row row.
int QTableWidget::visualColumn ( const QTableWidgetItem * item ) const
Returns the visual column of the given item.
QTableWidgetItem * QTableWidget::visualItem ( int visualRow, int visualColumn ) const
Returns a pointer to the item at the visualRow and visualColumn in the view.
int QTableWidget::visualRow ( const QTableWidgetItem * item ) const
Returns the visual row of the given item.