Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QTableWidget Class Reference

The QTableWidget class provides an item-based table view with a default model. More...

#include <qtablewidget.h>

Inheritance diagram for QTableWidget:
Collaboration diagram for QTableWidget:

Public Slots

void scrollToItem (const QTableWidgetItem *item, QAbstractItemView::ScrollHint hint=EnsureVisible)
 Scrolls the view if necessary to ensure that the item is visible.
void insertRow (int row)
 Inserts an empty row into the table at row.
void insertColumn (int column)
 Inserts an empty column into the table at column.
void removeRow (int row)
 Removes the row row and all its items from the table.
void removeColumn (int column)
 Removes the column column and all its items from the table.
void clear ()
 Removes all items in the view.
void clearContents ()
 Removes all items not in the headers from the view.
Public Slots inherited from QTableView
void selectRow (int row)
 Selects the given row in the table view if the current SelectionMode and SelectionBehavior allows rows to be selected.
void selectColumn (int column)
 Selects the given column in the table view if the current SelectionMode and SelectionBehavior allows columns to be selected.
void hideRow (int row)
 Hide the given row.
void hideColumn (int column)
 Hide the given column.
void showRow (int row)
 Show the given row.
void showColumn (int column)
 Show the given column.
void resizeRowToContents (int row)
 Resizes the given row based on the size hints of the delegate used to render each item in the row.
void resizeRowsToContents ()
 Resizes all rows based on the size hints of the delegate used to render each item in the rows.
void resizeColumnToContents (int column)
 Resizes the given column based on the size hints of the delegate used to render each item in the column.
void resizeColumnsToContents ()
 Resizes all columns based on the size hints of the delegate used to render each item in the columns.
void sortByColumn (int column, Qt::SortOrder order)
 Sorts the model by the values in the given column and order.
void setShowGrid (bool show)
Public Slots inherited from QAbstractItemView
virtual void reset ()
 Reset the internal state of the view.
virtual void selectAll ()
 Selects all items in the view.
void edit (const QModelIndex &index)
 Starts editing the item corresponding to the given index if it is editable.
void clearSelection ()
 Deselects all selected items.
void setCurrentIndex (const QModelIndex &index)
 Sets the current item to be the item at index.
void scrollToTop ()
 Scrolls the view to the top.
void scrollToBottom ()
 Scrolls the view to the bottom.
void update (const QModelIndex &index)
 Updates the area occupied by the given index.

Signals

void itemPressed (QTableWidgetItem *item)
 This signal is emitted whenever an item in the table is pressed.
void itemClicked (QTableWidgetItem *item)
 This signal is emitted whenever an item in the table is clicked.
void itemDoubleClicked (QTableWidgetItem *item)
 This signal is emitted whenever an item in the table is double clicked.
void itemActivated (QTableWidgetItem *item)
 This signal is emitted when the specified item has been activated.
void itemEntered (QTableWidgetItem *item)
 This signal is emitted when the mouse cursor enters an item.
void itemChanged (QTableWidgetItem *item)
 This signal is emitted whenever the data of item has changed.
void currentItemChanged (QTableWidgetItem *current, QTableWidgetItem *previous)
 This signal is emitted whenever the current item changes.
void itemSelectionChanged ()
 This signal is emitted whenever the selection changes.
void cellPressed (int row, int column)
 This signal is emitted whenever a cell in the table is pressed.
void cellClicked (int row, int column)
 This signal is emitted whenever a cell in the table is clicked.
void cellDoubleClicked (int row, int column)
 This signal is emitted whenever a cell in the table is double clicked.
void cellActivated (int row, int column)
 This signal is emitted when the cell specified by row and column has been activated.
void cellEntered (int row, int column)
 This signal is emitted when the mouse cursor enters a cell.
void cellChanged (int row, int column)
 This signal is emitted whenever the data of the item in the cell specified by row and column has changed.
void currentCellChanged (int currentRow, int currentColumn, int previousRow, int previousColumn)
 This signal is emitted whenever the current cell changes.
Signals inherited from QAbstractItemView
void pressed (const QModelIndex &index)
 This signal is emitted when a mouse button is pressed.
void clicked (const QModelIndex &index)
 This signal is emitted when a mouse button is left-clicked.
void doubleClicked (const QModelIndex &index)
 This signal is emitted when a mouse button is double-clicked.
void activated (const QModelIndex &index)
 This signal is emitted when the item specified by index is activated by the user.
void entered (const QModelIndex &index)
 This signal is emitted when the mouse cursor enters the item specified by index.
void viewportEntered ()
 This signal is emitted when the mouse cursor enters the viewport.
void iconSizeChanged (const QSize &size)

Public Member Functions

 QTableWidget (QWidget *parent=nullptr)
 Creates a new table view with the given parent.
 QTableWidget (int rows, int columns, QWidget *parent=nullptr)
 Creates a new table view with the given rows and columns, and with the given parent.
 ~QTableWidget ()
 Destroys this QTableWidget.
void setRowCount (int rows)
 Sets the number of rows in this table's model to rows.
int rowCount () const
 Returns the number of rows.
void setColumnCount (int columns)
 Sets the number of columns in this table's model to columns.
int columnCount () const
 Returns the number of columns.
int row (const QTableWidgetItem *item) const
 Returns the row for the item.
int column (const QTableWidgetItem *item) const
 Returns the column for the item.
QTableWidgetItemitem (int row, int column) const
 Returns the item for the given row and column if one has been set; otherwise returns \nullptr.
void setItem (int row, int column, QTableWidgetItem *item)
 Sets the item for the given row and column to item.
QTableWidgetItemtakeItem (int row, int column)
 Removes the item at row and column from the table without deleting it.
QList< QTableWidgetItem * > items (const QMimeData *data) const
 Returns a list of pointers to the items contained in the data object.
QModelIndex indexFromItem (const QTableWidgetItem *item) const
 Returns the QModelIndex associated with the given item.
QTableWidgetItemitemFromIndex (const QModelIndex &index) const
 Returns a pointer to the QTableWidgetItem associated with the given index.
QTableWidgetItemverticalHeaderItem (int row) const
 Returns the vertical header item for row row.
void setVerticalHeaderItem (int row, QTableWidgetItem *item)
 Sets the vertical header item for row row to item.
QTableWidgetItemtakeVerticalHeaderItem (int row)
 Removes the vertical header item at row from the header without deleting it.
QTableWidgetItemhorizontalHeaderItem (int column) const
 Returns the horizontal header item for column, column, if one has been set; otherwise returns \nullptr.
void setHorizontalHeaderItem (int column, QTableWidgetItem *item)
 Sets the horizontal header item for column column to item.
QTableWidgetItemtakeHorizontalHeaderItem (int column)
 Removes the horizontal header item at column from the header without deleting it.
void setVerticalHeaderLabels (const QStringList &labels)
 Sets the vertical header labels using labels.
void setHorizontalHeaderLabels (const QStringList &labels)
 Sets the horizontal header labels using labels.
int currentRow () const
 Returns the row of the current item.
int currentColumn () const
 Returns the column of the current item.
QTableWidgetItemcurrentItem () const
 Returns the current item.
void setCurrentItem (QTableWidgetItem *item)
 Sets the current item to item.
void setCurrentItem (QTableWidgetItem *item, QItemSelectionModel::SelectionFlags command)
 Sets the current item to be item, using the given command.
void setCurrentCell (int row, int column)
 Sets the current cell to be the cell at position (row, column).
void setCurrentCell (int row, int column, QItemSelectionModel::SelectionFlags command)
 Sets the current cell to be the cell at position (row, column), using the given command.
void sortItems (int column, Qt::SortOrder order=Qt::AscendingOrder)
 Sorts all the rows in the table widget based on column and order.
void setSortingEnabled (bool enable)
bool isSortingEnabled () const
void editItem (QTableWidgetItem *item)
 Starts editing the item if it is editable.
void openPersistentEditor (QTableWidgetItem *item)
 Opens an editor for the give item.
void closePersistentEditor (QTableWidgetItem *item)
 Closes the persistent editor for item.
bool isPersistentEditorOpen (QTableWidgetItem *item) const
QWidgetcellWidget (int row, int column) const
 Returns the widget displayed in the cell in the given row and column.
void setCellWidget (int row, int column, QWidget *widget)
 Sets the given widget to be displayed in the cell in the given row and column, passing the ownership of the widget to the table.
void removeCellWidget (int row, int column)
 Removes the widget set on the cell indicated by row and column.
void setRangeSelected (const QTableWidgetSelectionRange &range, bool select)
 Selects or deselects the range depending on select.
QList< QTableWidgetSelectionRangeselectedRanges () const
 Returns a list of all selected ranges.
QList< QTableWidgetItem * > selectedItems () const
 Returns a list of all selected items.
QList< QTableWidgetItem * > findItems (const QString &text, Qt::MatchFlags flags) const
 Finds items that matches the text using the given flags.
int visualRow (int logicalRow) const
 Returns the visual row of the given logicalRow.
int visualColumn (int logicalColumn) const
 Returns the visual column of the given logicalColumn.
QTableWidgetItemitemAt (const QPoint &p) const
 Returns a pointer to the item at the given point, or returns \nullptr if point is not covered by an item in the table widget.
QTableWidgetItemitemAt (int x, int y) const
 Returns the item at the position equivalent to QPoint({ax}, {ay}) in the table widget's coordinate system, or returns \nullptr if the specified point is not covered by an item in the table widget.
QRect visualItemRect (const QTableWidgetItem *item) const
 Returns the rectangle on the viewport occupied by the item at item.
const QTableWidgetItemitemPrototype () const
 Returns the item prototype used by the table.
void setItemPrototype (const QTableWidgetItem *item)
 Sets the item prototype for the table to the specified item.
bool isPersistentEditorOpen (const QModelIndex &index) const
Public Member Functions inherited from QTableView
 QTableView (QWidget *parent=nullptr)
 Constructs a table view with a parent to represent the data.
 ~QTableView ()
 Destroys the table view.
void setRootIndex (const QModelIndex &index) override
 \reimp
void setSelectionModel (QItemSelectionModel *selectionModel) override
 \reimp
void doItemsLayout () override
QHeaderViewhorizontalHeader () const
 Returns the table view's horizontal header.
QHeaderViewverticalHeader () const
 Returns the table view's vertical header.
void setHorizontalHeader (QHeaderView *header)
 Sets the widget to use for the horizontal header to header.
void setVerticalHeader (QHeaderView *header)
 Sets the widget to use for the vertical header to header.
int rowViewportPosition (int row) const
 Returns the y-coordinate in contents coordinates of the given row.
int rowAt (int y) const
 Returns the row in which the given y-coordinate, y, in contents coordinates is located.
void setRowHeight (int row, int height)
 Sets the height of the given row to be height.
int rowHeight (int row) const
 Returns the height of the given row.
int columnViewportPosition (int column) const
 Returns the x-coordinate in contents coordinates of the given column.
int columnAt (int x) const
 Returns the column in which the given x-coordinate, x, in contents coordinates is located.
void setColumnWidth (int column, int width)
 Sets the width of the given column to be width.
int columnWidth (int column) const
 Returns the width of the given column.
bool isRowHidden (int row) const
 Returns true if the given row is hidden; otherwise returns false.
void setRowHidden (int row, bool hide)
 If hide is true row will be hidden, otherwise it will be shown.
bool isColumnHidden (int column) const
 Returns true if the given column is hidden; otherwise returns false.
void setColumnHidden (int column, bool hide)
 If hide is true the given column will be hidden; otherwise it will be shown.
void setSortingEnabled (bool enable)
 If enable is true, enables sorting for the table and immediately trigger a call to sortByColumn() with the current sort section and order.
bool isSortingEnabled () const
bool showGrid () const
Qt::PenStyle gridStyle () const
void setGridStyle (Qt::PenStyle style)
void setWordWrap (bool on)
bool wordWrap () const
QRect visualRect (const QModelIndex &index) const override
 \reimp
void scrollTo (const QModelIndex &index, ScrollHint hint=EnsureVisible) override
 \reimp
QModelIndex indexAt (const QPoint &p) const override
 Returns the index position of the model item corresponding to the table item at position pos in contents coordinates.
void setSpan (int row, int column, int rowSpan, int columnSpan)
 Sets the span of the table element at (row, column) to the number of rows and columns specified by (rowSpanCount, columnSpanCount).
int rowSpan (int row, int column) const
 Returns the row span of the table element at (row, column).
int columnSpan (int row, int column) const
 Returns the column span of the table element at (row, column).
void clearSpans ()
 Removes all row and column spans in the table view.
Public Member Functions inherited from QAbstractItemView
 QAbstractItemView (QWidget *parent=nullptr)
 Constructs an abstract item view with the given parent.
 ~QAbstractItemView ()
 Destroys the view.
QAbstractItemModelmodel () const
 Returns the model that this view is presenting.
QItemSelectionModelselectionModel () const
 Returns the current selection model.
void setItemDelegate (QAbstractItemDelegate *delegate)
 Sets the item delegate for this view and its model to delegate.
QAbstractItemDelegateitemDelegate () const
 Returns the item delegate used by this view and model.
void setSelectionMode (QAbstractItemView::SelectionMode mode)
QAbstractItemView::SelectionMode selectionMode () const
void setSelectionBehavior (QAbstractItemView::SelectionBehavior behavior)
QAbstractItemView::SelectionBehavior selectionBehavior () const
QModelIndex currentIndex () const
 Returns the model index of the current item.
QModelIndex rootIndex () const
 Returns the model index of the model's root item.
void setEditTriggers (EditTriggers triggers)
EditTriggers editTriggers () const
void setVerticalScrollMode (ScrollMode mode)
ScrollMode verticalScrollMode () const
void resetVerticalScrollMode ()
void setHorizontalScrollMode (ScrollMode mode)
ScrollMode horizontalScrollMode () const
void resetHorizontalScrollMode ()
void setAutoScroll (bool enable)
bool hasAutoScroll () const
void setAutoScrollMargin (int margin)
int autoScrollMargin () const
void setTabKeyNavigation (bool enable)
bool tabKeyNavigation () const
void setAlternatingRowColors (bool enable)
bool alternatingRowColors () const
void setIconSize (const QSize &size)
QSize iconSize () const
void setTextElideMode (Qt::TextElideMode mode)
Qt::TextElideMode textElideMode () const
virtual void keyboardSearch (const QString &search)
 Moves to and selects the item best matching the string search.
QSize sizeHintForIndex (const QModelIndex &index) const
 Returns the size hint for the item with the specified index or an invalid size for invalid indexes.
int updateThreshold () const
void setUpdateThreshold (int threshold)
void openPersistentEditor (const QModelIndex &index)
 Opens a persistent editor on the item at the given index.
void closePersistentEditor (const QModelIndex &index)
 Closes the persistent editor for the item at the given index.
bool isPersistentEditorOpen (const QModelIndex &index) const
void setIndexWidget (const QModelIndex &index, QWidget *widget)
 Sets the given widget on the item at the given index, passing the ownership of the widget to the viewport.
QWidgetindexWidget (const QModelIndex &index) const
 Returns the widget for the item at the given index.
void setItemDelegateForRow (int row, QAbstractItemDelegate *delegate)
 Sets the given item delegate used by this view and model for the given row.
QAbstractItemDelegateitemDelegateForRow (int row) const
 Returns the item delegate used by this view and model for the given row, or \nullptr if no delegate has been assigned.
void setItemDelegateForColumn (int column, QAbstractItemDelegate *delegate)
 Sets the given item delegate used by this view and model for the given column.
QAbstractItemDelegateitemDelegateForColumn (int column) const
 Returns the item delegate used by this view and model for the given column.
virtual QAbstractItemDelegateitemDelegateForIndex (const QModelIndex &index) const
virtual QVariant inputMethodQuery (Qt::InputMethodQuery query) const override
 \reimp

Protected Member Functions

bool event (QEvent *e) override
 \reimp
virtual QStringList mimeTypes () const
 Returns a list of MIME types that can be used to describe a list of tablewidget items.
virtual QMimeDatamimeData (const QList< QTableWidgetItem * > &items) const
 Returns an object that contains a serialized description of the specified items.
virtual bool dropMimeData (int row, int column, const QMimeData *data, Qt::DropAction action)
 Handles the data supplied by a drag and drop operation that ended with the given action in the given row and column.
virtual Qt::DropActions supportedDropActions () const
 Returns the drop actions supported by this view.
Protected Member Functions inherited from QTableView
 QTableView (QTableViewPrivate &, QWidget *parent)
void scrollContentsBy (int dx, int dy) override
 \reimp
void initViewItemOption (QStyleOptionViewItem *option) const override
 \reimp
void paintEvent (QPaintEvent *e) override
 Paints the table on receipt of the given paint event event.
void timerEvent (QTimerEvent *event) override
 \reimp
int horizontalOffset () const override
 Returns the horizontal offset of the items in the table view.
int verticalOffset () const override
 Returns the vertical offset of the items in the table view.
QModelIndex moveCursor (CursorAction cursorAction, Qt::KeyboardModifiers modifiers) override
 Moves the cursor in accordance with the given cursorAction, using the information provided by the modifiers.
void setSelection (const QRect &rect, QItemSelectionModel::SelectionFlags command) override
 Selects the items within the given rect and in accordance with the specified selection flags.
QRegion visualRegionForSelection (const QItemSelection &selection) const override
 \reimp
QModelIndexList selectedIndexes () const override
 \reimp
void updateGeometries () override
 \reimp
QSize viewportSizeHint () const override
 \reimp
int sizeHintForRow (int row) const override
 Returns the size hint for the given row's height or -1 if there is no model.
int sizeHintForColumn (int column) const override
 Returns the size hint for the given column's width or -1 if there is no model.
void verticalScrollbarAction (int action) override
void horizontalScrollbarAction (int action) override
bool isIndexHidden (const QModelIndex &index) const override
 \reimp
void selectionChanged (const QItemSelection &selected, const QItemSelection &deselected) override
 \reimp
void currentChanged (const QModelIndex &current, const QModelIndex &previous) override
 \reimp
Protected Member Functions inherited from QAbstractItemView
 QAbstractItemView (QAbstractItemViewPrivate &, QWidget *parent=nullptr)
virtual bool edit (const QModelIndex &index, EditTrigger trigger, QEvent *event)
 Starts editing the item at index, creating an editor if necessary, and returns true if the view's \l{State} is now EditingState; otherwise returns false.
virtual QItemSelectionModel::SelectionFlags selectionCommand (const QModelIndex &index, const QEvent *event=nullptr) const
 Returns the SelectionFlags to be used when updating a selection model for the specified index.
State state () const
 Returns the item view's state.
void setState (State state)
 Sets the item view's state to the given state.
void scheduleDelayedItemsLayout ()
 Schedules a layout of the items in the view to be executed when the event processing starts.
void executeDelayedItemsLayout ()
 Executes the scheduled layouts without waiting for the event processing to begin.
void setDirtyRegion (const QRegion &region)
 Marks the given region as dirty and schedules it to be updated.
void scrollDirtyRegion (int dx, int dy)
 Prepares the view for scrolling by ({dx},{dy}) pixels by moving the dirty regions in the opposite direction.
QPoint dirtyRegionOffset () const
 Returns the offset of the dirty regions in the view.
void startAutoScroll ()
void stopAutoScroll ()
void doAutoScroll ()
bool focusNextPrevChild (bool next) override
 \reimp
bool event (QEvent *event) override
 \reimp
bool viewportEvent (QEvent *event) override
 This function is used to handle tool tips, and What's This?
void mousePressEvent (QMouseEvent *event) override
 This function is called with the given event when a mouse button is pressed while the cursor is inside the widget.
void mouseMoveEvent (QMouseEvent *event) override
 This function is called with the given event when a mouse move event is sent to the widget.
void mouseReleaseEvent (QMouseEvent *event) override
 This function is called with the given event when a mouse button is released, after a mouse press event on the widget.
void mouseDoubleClickEvent (QMouseEvent *event) override
 This function is called with the given event when a mouse button is double clicked inside the widget.
void focusInEvent (QFocusEvent *event) override
 This function is called with the given event when the widget obtains the focus.
void focusOutEvent (QFocusEvent *event) override
 This function is called with the given event when the widget loses the focus.
void keyPressEvent (QKeyEvent *event) override
 This function is called with the given event when a key event is sent to the widget.
void resizeEvent (QResizeEvent *event) override
 This function is called with the given event when a resize event is sent to the widget.
void timerEvent (QTimerEvent *event) override
 This function is called with the given event when a timer event is sent to the widget.
void inputMethodEvent (QInputMethodEvent *event) override
 \reimp
bool eventFilter (QObject *object, QEvent *event) override
 \reimp
QSize viewportSizeHint () const override

Properties

int rowCount
 the number of rows in the table
int columnCount
 the number of columns in the table
Properties inherited from QTableView
bool showGrid
 whether the grid is shown
Qt::PenStyle gridStyle
 the pen style used to draw the grid.
bool sortingEnabled
 whether sorting is enabled
bool wordWrap
 the item text word-wrapping policy
Properties inherited from QAbstractItemView
bool autoScroll
 whether autoscrolling in drag move events is enabled
int autoScrollMargin
 the size of the area when auto scrolling is triggered
EditTriggers editTriggers
 which actions will initiate item editing
bool tabKeyNavigation
 whether item navigation with tab and backtab is enabled.
bool alternatingRowColors
 whether to draw the background using alternating colors
SelectionMode selectionMode
 which selection mode the view operates in
SelectionBehavior selectionBehavior
 which selection behavior the view uses
QSize iconSize
 the size of items' icons
Qt::TextElideMode textElideMode
 the position of the "..." in elided text.
ScrollMode verticalScrollMode
 how the view scrolls its contents in the vertical direction
ScrollMode horizontalScrollMode
 how the view scrolls its contents in the horizontal direction
int updateThreshold

Private Member Functions

void setModel (QAbstractItemModel *model) override

Friends

class QTableModel

Additional Inherited Members

Public Types inherited from QAbstractItemView
enum  SelectionMode {
  NoSelection , SingleSelection , MultiSelection , ExtendedSelection ,
  ContiguousSelection
}
 This enum indicates how the view responds to user selections: More...
enum  SelectionBehavior { SelectItems , SelectRows , SelectColumns }
 \value SelectItems Selecting single items. More...
enum  ScrollHint { EnsureVisible , PositionAtTop , PositionAtBottom , PositionAtCenter }
 \value EnsureVisible Scroll to ensure that the item is visible. More...
enum  EditTrigger {
  NoEditTriggers = 0 , CurrentChanged = 1 , DoubleClicked = 2 , SelectedClicked = 4 ,
  EditKeyPressed = 8 , AnyKeyPressed = 16 , AllEditTriggers = 31
}
 This enum describes actions which will initiate item editing. More...
enum  ScrollMode { ScrollPerItem , ScrollPerPixel }
 Describes how the scrollbar should behave. More...
Protected Types inherited from QAbstractItemView
enum  CursorAction {
  MoveUp , MoveDown , MoveLeft , MoveRight ,
  MoveHome , MoveEnd , MovePageUp , MovePageDown ,
  MoveNext , MovePrevious
}
 This enum describes the different ways to navigate between items,. More...
enum  State {
  NoState , DraggingState , DragSelectingState , EditingState ,
  ExpandingState , CollapsingState , AnimatingState
}
 Describes the different states the view can be in. More...
Protected Slots inherited from QTableView
void rowMoved (int row, int oldIndex, int newIndex)
 This slot is called to change the index of the given row in the table view.
void columnMoved (int column, int oldIndex, int newIndex)
 This slot is called to change the index of the given column in the table view.
void rowResized (int row, int oldHeight, int newHeight)
 This slot is called to change the height of the given row.
void columnResized (int column, int oldWidth, int newWidth)
 This slot is called to change the width of the given column.
void rowCountChanged (int oldCount, int newCount)
 This slot is called whenever rows are added or deleted.
void columnCountChanged (int oldCount, int newCount)
 This slot is called whenever columns are added or deleted.
Protected Slots inherited from QAbstractItemView
virtual void dataChanged (const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles=QList< int >())
 This slot is called when items with the given roles are changed in the model.
virtual void rowsInserted (const QModelIndex &parent, int start, int end)
 This slot is called when rows are inserted.
virtual void rowsAboutToBeRemoved (const QModelIndex &parent, int start, int end)
 This slot is called when rows are about to be removed.
virtual void updateEditorData ()
virtual void updateEditorGeometries ()
virtual void verticalScrollbarValueChanged (int value)
virtual void horizontalScrollbarValueChanged (int value)
virtual void closeEditor (QWidget *editor, QAbstractItemDelegate::EndEditHint hint)
 Closes the given editor, and releases it.
virtual void commitData (QWidget *editor)
 Commit the data in the editor to the model.
virtual void editorDestroyed (QObject *editor)
 This function is called when the given editor has been destroyed.

Detailed Description

The QTableWidget class provides an item-based table view with a default model.

\inmodule QtWidgets

{Table of months and amounts}

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 outside 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);

If you want to enable sorting in your table widget, do so after you have populated it with items, otherwise sorting may interfere with the insertion order (see setItem() for details).

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")));

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, QTableView, {Model/View Programming}

Definition at line 193 of file qtablewidget.h.

Constructor & Destructor Documentation

◆ QTableWidget() [1/2]

QTableWidget::QTableWidget ( QWidget * parent = nullptr)
explicit

Creates a new table view with the given parent.

Definition at line 1886 of file qtablewidget.cpp.

◆ QTableWidget() [2/2]

QTableWidget::QTableWidget ( int rows,
int columns,
QWidget * parent = nullptr )

Creates a new table view with the given rows and columns, and with the given parent.

Definition at line 1897 of file qtablewidget.cpp.

◆ ~QTableWidget()

QTableWidget::~QTableWidget ( )

Destroys this QTableWidget.

Definition at line 1908 of file qtablewidget.cpp.

Member Function Documentation

◆ cellActivated

void QTableWidget::cellActivated ( int row,
int column )
signal

This signal is emitted when the cell specified by row and column has been activated.

◆ cellChanged

void QTableWidget::cellChanged ( int row,
int column )
signal

This signal is emitted whenever the data of the item in the cell specified by row and column has changed.

◆ cellClicked

void QTableWidget::cellClicked ( int row,
int column )
signal

This signal is emitted whenever a cell in the table is clicked.

The row and column specified is the cell that was clicked.

◆ cellDoubleClicked

void QTableWidget::cellDoubleClicked ( int row,
int column )
signal

This signal is emitted whenever a cell in the table is double clicked.

The row and column specified is the cell that was double clicked.

◆ cellEntered

void QTableWidget::cellEntered ( int row,
int column )
signal

This signal is emitted when the mouse cursor enters a cell.

The cell is specified by row and column.

This signal is only emitted when mouseTracking is turned on, or when a mouse button is pressed while moving into an item.

◆ cellPressed

void QTableWidget::cellPressed ( int row,
int column )
signal

This signal is emitted whenever a cell in the table is pressed.

The row and column specified is the cell that was pressed.

◆ cellWidget()

QWidget * QTableWidget::cellWidget ( int row,
int column ) const

Returns the widget displayed in the cell in the given row and column.

Note
The table takes ownership of the widget.
See also
setCellWidget()

Definition at line 2314 of file qtablewidget.cpp.

◆ clear

void QTableWidget::clear ( )
slot

Removes all items in the view.

This will also remove all selections and headers. If you don't want to remove the headers, use QTableWidget::clearContents(). The table dimensions stay the same.

Definition at line 2554 of file qtablewidget.cpp.

◆ clearContents

void QTableWidget::clearContents ( )
slot

Removes all items not in the headers from the view.

This will also remove all selections. The table dimensions stay the same.

Definition at line 2566 of file qtablewidget.cpp.

◆ closePersistentEditor()

void QTableWidget::closePersistentEditor ( QTableWidgetItem * item)

Closes the persistent editor for item.

See also
openPersistentEditor(), isPersistentEditorOpen()

Definition at line 2284 of file qtablewidget.cpp.

◆ column()

int QTableWidget::column ( const QTableWidgetItem * item) const

Returns the column for the item.

Definition at line 1972 of file qtablewidget.cpp.

◆ columnCount()

int QTableWidget::columnCount ( ) const

Returns the number of columns.

Definition at line 1954 of file qtablewidget.cpp.

◆ currentCellChanged

void QTableWidget::currentCellChanged ( int currentRow,
int currentColumn,
int previousRow,
int previousColumn )
signal

This signal is emitted whenever the current cell changes.

The cell specified by previousRow and previousColumn is the cell that previously had the focus, the cell specified by currentRow and currentColumn is the new current cell.

◆ currentColumn()

int QTableWidget::currentColumn ( ) const

Returns the column of the current item.

See also
currentRow(), setCurrentCell()

Definition at line 2160 of file qtablewidget.cpp.

◆ currentItem()

QTableWidgetItem * QTableWidget::currentItem ( ) const

Returns the current item.

See also
setCurrentItem()

Definition at line 2170 of file qtablewidget.cpp.

◆ currentItemChanged

void QTableWidget::currentItemChanged ( 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.

◆ currentRow()

int QTableWidget::currentRow ( ) const

Returns the row of the current item.

See also
currentColumn(), setCurrentCell()

Definition at line 2150 of file qtablewidget.cpp.

◆ dropMimeData()

bool QTableWidget::dropMimeData ( int row,
int column,
const QMimeData * data,
Qt::DropAction action )
protectedvirtual

Handles the data supplied by a drag and drop operation that ended with the given action in the given row and column.

Returns true if the data and action can be handled by the model; otherwise returns false.

See also
supportedDropActions(), supportedDragActions

Definition at line 2621 of file qtablewidget.cpp.

◆ editItem()

void QTableWidget::editItem ( QTableWidgetItem * item)

Starts editing the item if it is editable.

Definition at line 2257 of file qtablewidget.cpp.

◆ event()

bool QTableWidget::event ( QEvent * e)
overrideprotected

\reimp

Definition at line 2711 of file qtablewidget.cpp.

◆ findItems()

QList< QTableWidgetItem * > QTableWidget::findItems ( const QString & text,
Qt::MatchFlags flags ) const

Finds items that matches the text using the given flags.

Definition at line 2403 of file qtablewidget.cpp.

◆ horizontalHeaderItem()

QTableWidgetItem * QTableWidget::horizontalHeaderItem ( int column) const

Returns the horizontal header item for column, column, if one has been set; otherwise returns \nullptr.

Definition at line 2075 of file qtablewidget.cpp.

◆ indexFromItem()

QModelIndex QTableWidget::indexFromItem ( const QTableWidgetItem * item) const

Returns the QModelIndex associated with the given item.

Note
In Qt versions prior to 5.10, this function took a non-{const} item.

Definition at line 2686 of file qtablewidget.cpp.

◆ insertColumn

void QTableWidget::insertColumn ( int column)
slot

Inserts an empty column into the table at column.

Definition at line 2522 of file qtablewidget.cpp.

◆ insertRow

void QTableWidget::insertRow ( int row)
slot

Inserts an empty row into the table at row.

Definition at line 2513 of file qtablewidget.cpp.

◆ isPersistentEditorOpen() [1/2]

bool QAbstractItemView::isPersistentEditorOpen ( const QModelIndex & index) const
Since
5.10

Returns whether a persistent editor is open for the item at index index.

See also
openPersistentEditor(), closePersistentEditor()

Definition at line 187 of file qabstractitemview.cpp.

◆ isPersistentEditorOpen() [2/2]

bool QTableWidget::isPersistentEditorOpen ( QTableWidgetItem * item) const
Since
5.10

Returns whether a persistent editor is open for item item.

See also
openPersistentEditor(), closePersistentEditor()

Definition at line 2300 of file qtablewidget.cpp.

◆ isSortingEnabled()

bool QTableWidget::isSortingEnabled ( ) const

Definition at line 2248 of file qtablewidget.cpp.

◆ item()

QTableWidgetItem * QTableWidget::item ( int row,
int column ) const

Returns the item for the given row and column if one has been set; otherwise returns \nullptr.

See also
setItem()

Definition at line 1985 of file qtablewidget.cpp.

◆ itemActivated

void QTableWidget::itemActivated ( QTableWidgetItem * item)
signal

This signal is emitted when the specified item has been activated.

◆ itemAt() [1/2]

QTableWidgetItem * QTableWidget::itemAt ( const QPoint & point) const

Returns a pointer to the item at the given point, or returns \nullptr if point is not covered by an item in the table widget.

See also
item()

Definition at line 2445 of file qtablewidget.cpp.

◆ itemAt() [2/2]

QTableWidgetItem * QTableWidget::itemAt ( int ax,
int ay ) const
inline

Returns the item at the position equivalent to QPoint({ax}, {ay}) in the table widget's coordinate system, or returns \nullptr if the specified point is not covered by an item in the table widget.

See also
item()

Definition at line 329 of file qtablewidget.h.

◆ itemChanged

void QTableWidget::itemChanged ( QTableWidgetItem * item)
signal

This signal is emitted whenever the data of item has changed.

◆ itemClicked

void QTableWidget::itemClicked ( QTableWidgetItem * item)
signal

This signal is emitted whenever an item in the table is clicked.

The item specified is the item that was clicked.

◆ itemDoubleClicked

void QTableWidget::itemDoubleClicked ( QTableWidgetItem * item)
signal

This signal is emitted whenever an item in the table is double clicked.

The item specified is the item that was double clicked.

◆ itemEntered

void QTableWidget::itemEntered ( QTableWidgetItem * item)
signal

This signal is emitted when the mouse cursor enters an item.

The item is the item entered.

This signal is only emitted when mouseTracking is turned on, or when a mouse button is pressed while moving into an item.

◆ itemFromIndex()

QTableWidgetItem * QTableWidget::itemFromIndex ( const QModelIndex & index) const

Returns a pointer to the QTableWidgetItem associated with the given index.

Definition at line 2696 of file qtablewidget.cpp.

◆ itemPressed

void QTableWidget::itemPressed ( QTableWidgetItem * item)
signal

This signal is emitted whenever an item in the table is pressed.

The item specified is the item that was pressed.

◆ itemPrototype()

const QTableWidgetItem * QTableWidget::itemPrototype ( ) const

Returns the item prototype used by the table.

See also
setItemPrototype()

Definition at line 2485 of file qtablewidget.cpp.

◆ items()

QList< QTableWidgetItem * > QTableWidget::items ( const QMimeData * data) const

Returns a list of pointers to the items contained in the data object.

If the object was not created by a QTreeWidget in the same process, the list is empty.

Definition at line 2672 of file qtablewidget.cpp.

◆ itemSelectionChanged

void QTableWidget::itemSelectionChanged ( )
signal

This signal is emitted whenever the selection changes.

See also
selectedItems(), QTableWidgetItem::isSelected()

◆ mimeData()

QMimeData * QTableWidget::mimeData ( const QList< QTableWidgetItem * > & items) const
protectedvirtual

Returns an object that contains a serialized description of the specified items.

The format used to describe the items is obtained from the mimeTypes() function.

If the list of items is empty, \nullptr is returned rather than a serialized empty list.

Definition at line 2592 of file qtablewidget.cpp.

◆ mimeTypes()

QStringList QTableWidget::mimeTypes ( ) const
protectedvirtual

Returns a list of MIME types that can be used to describe a list of tablewidget items.

See also
mimeData()

Definition at line 2579 of file qtablewidget.cpp.

◆ openPersistentEditor()

void QTableWidget::openPersistentEditor ( QTableWidgetItem * item)

Opens an editor for the give item.

The editor remains open after editing.

See also
closePersistentEditor(), isPersistentEditorOpen()

Definition at line 2270 of file qtablewidget.cpp.

◆ removeCellWidget()

void QTableWidget::removeCellWidget ( int row,
int column )
inline

Removes the widget set on the cell indicated by row and column.

Definition at line 326 of file qtablewidget.h.

◆ removeColumn

void QTableWidget::removeColumn ( int column)
slot

Removes the column column and all its items from the table.

Definition at line 2540 of file qtablewidget.cpp.

◆ removeRow

void QTableWidget::removeRow ( int row)
slot

Removes the row row and all its items from the table.

Definition at line 2531 of file qtablewidget.cpp.

◆ row()

int QTableWidget::row ( const QTableWidgetItem * item) const

Returns the row for the item.

Definition at line 1963 of file qtablewidget.cpp.

◆ rowCount()

int QTableWidget::rowCount ( ) const

Returns the number of rows.

Definition at line 1931 of file qtablewidget.cpp.

◆ scrollToItem

void QTableWidget::scrollToItem ( const QTableWidgetItem * item,
QAbstractItemView::ScrollHint hint = EnsureVisible )
slot

Scrolls the view if necessary to ensure that the item is visible.

The hint parameter specifies more precisely where the item should be located after the operation.

Definition at line 2470 of file qtablewidget.cpp.

◆ selectedItems()

QList< QTableWidgetItem * > QTableWidget::selectedItems ( ) const

Returns a list of all selected items.

This function returns a list of pointers to the contents of the selected cells. Use the selectedIndexes() function to retrieve the complete selection including empty cells.

See also
selectedIndexes()

Definition at line 2384 of file qtablewidget.cpp.

◆ selectedRanges()

QList< QTableWidgetSelectionRange > QTableWidget::selectedRanges ( ) const

Returns a list of all selected ranges.

See also
QTableWidgetSelectionRange

Definition at line 2360 of file qtablewidget.cpp.

◆ setCellWidget()

void QTableWidget::setCellWidget ( int row,
int column,
QWidget * widget )

Sets the given widget to be displayed in the cell in the given row and column, passing the ownership of the widget to the table.

If cell widget A is replaced with cell widget B, cell widget A will be deleted. For example, in the code snippet below, the QLineEdit object will be deleted.

...
setCellWidget(row, column, new QTextEdit);
See also
cellWidget()

Definition at line 2332 of file qtablewidget.cpp.

◆ setColumnCount()

void QTableWidget::setColumnCount ( int columns)

Sets the number of columns in this table's model to columns.

If this is less than columnCount(), the data in the unwanted columns is discarded.

See also
setRowCount()

Definition at line 1944 of file qtablewidget.cpp.

◆ setCurrentCell() [1/2]

void QTableWidget::setCurrentCell ( int row,
int column )

Sets the current cell to be the cell at position (row, column).

Depending on the current \l{QAbstractItemView::SelectionMode}{selection mode}, the cell may also be selected.

See also
setCurrentItem(), currentRow(), currentColumn()

Definition at line 2210 of file qtablewidget.cpp.

◆ setCurrentCell() [2/2]

void QTableWidget::setCurrentCell ( int row,
int column,
QItemSelectionModel::SelectionFlags command )

Sets the current cell to be the cell at position (row, column), using the given command.

See also
setCurrentItem(), currentRow(), currentColumn()

Definition at line 2221 of file qtablewidget.cpp.

◆ setCurrentItem() [1/2]

void QTableWidget::setCurrentItem ( QTableWidgetItem * item)

Sets the current item to item.

Unless the selection mode is \l{QAbstractItemView::}{NoSelection}, the item is also selected.

See also
currentItem(), setCurrentCell()

Definition at line 2184 of file qtablewidget.cpp.

◆ setCurrentItem() [2/2]

void QTableWidget::setCurrentItem ( QTableWidgetItem * item,
QItemSelectionModel::SelectionFlags command )

Sets the current item to be item, using the given command.

See also
currentItem(), setCurrentCell()

Definition at line 2195 of file qtablewidget.cpp.

◆ setHorizontalHeaderItem()

void QTableWidget::setHorizontalHeaderItem ( int column,
QTableWidgetItem * item )

Sets the horizontal header item for column column to item.

If necessary, the column count is increased to fit the item. The previous header item (if there was one) is deleted.

Definition at line 2086 of file qtablewidget.cpp.

◆ setHorizontalHeaderLabels()

void QTableWidget::setHorizontalHeaderLabels ( const QStringList & labels)

Sets the horizontal header labels using labels.

Definition at line 2130 of file qtablewidget.cpp.

◆ setItem()

void QTableWidget::setItem ( int row,
int column,
QTableWidgetItem * item )

Sets the item for the given row and column to item.

The table takes ownership of the item.

Note that if sorting is enabled (see \l{QTableView::sortingEnabled} {sortingEnabled}) and column is the current sort column, the row will be moved to the sorted position determined by item.

If you want to set several items of a particular row (say, by calling setItem() in a loop), you may want to turn off sorting before doing so, and turn it back on afterwards; this will allow you to use the same row argument for all items in the same row (i.e. setItem() will not move the row).

See also
item(), takeItem()

Definition at line 2009 of file qtablewidget.cpp.

◆ setItemPrototype()

void QTableWidget::setItemPrototype ( const QTableWidgetItem * item)

Sets the item prototype for the table to the specified item.

The table widget will use the item prototype clone function when it needs to create a new table item. For example when the user is editing in an empty cell. This is useful when you have a QTableWidgetItem subclass and want to make sure that QTableWidget creates instances of your subclass.

The table takes ownership of the prototype.

See also
itemPrototype()

Definition at line 2504 of file qtablewidget.cpp.

◆ setModel()

void QTableWidget::setModel ( QAbstractItemModel * model)
overrideprivatevirtual

Reimplemented from QTableView.

Definition at line 2705 of file qtablewidget.cpp.

◆ setRangeSelected()

void QTableWidget::setRangeSelected ( const QTableWidgetSelectionRange & range,
bool select )

Selects or deselects the range depending on select.

Definition at line 2341 of file qtablewidget.cpp.

◆ setRowCount()

void QTableWidget::setRowCount ( int rows)

Sets the number of rows in this table's model to rows.

If this is less than rowCount(), the data in the unwanted rows is discarded.

See also
setColumnCount()

Definition at line 1921 of file qtablewidget.cpp.

◆ setSortingEnabled()

void QTableWidget::setSortingEnabled ( bool enable)

Definition at line 2240 of file qtablewidget.cpp.

◆ setVerticalHeaderItem()

void QTableWidget::setVerticalHeaderItem ( int row,
QTableWidgetItem * item )

Sets the vertical header item for row row to item.

Definition at line 2048 of file qtablewidget.cpp.

◆ setVerticalHeaderLabels()

void QTableWidget::setVerticalHeaderLabels ( const QStringList & labels)

Sets the vertical header labels using labels.

Definition at line 2112 of file qtablewidget.cpp.

◆ sortItems()

void QTableWidget::sortItems ( int column,
Qt::SortOrder order = Qt::AscendingOrder )

Sorts all the rows in the table widget based on column and order.

Definition at line 2230 of file qtablewidget.cpp.

◆ supportedDropActions()

Qt::DropActions QTableWidget::supportedDropActions ( ) const
protectedvirtual

Returns the drop actions supported by this view.

See also
Qt::DropActions, supportedDragActions, dropMimeData()

Definition at line 2640 of file qtablewidget.cpp.

◆ takeHorizontalHeaderItem()

QTableWidgetItem * QTableWidget::takeHorizontalHeaderItem ( int column)

Removes the horizontal header item at column from the header without deleting it.

Definition at line 2100 of file qtablewidget.cpp.

◆ takeItem()

QTableWidgetItem * QTableWidget::takeItem ( int row,
int column )

Removes the item at row and column from the table without deleting it.

Definition at line 2027 of file qtablewidget.cpp.

◆ takeVerticalHeaderItem()

QTableWidgetItem * QTableWidget::takeVerticalHeaderItem ( int row)

Removes the vertical header item at row from the header without deleting it.

Definition at line 2062 of file qtablewidget.cpp.

◆ verticalHeaderItem()

QTableWidgetItem * QTableWidget::verticalHeaderItem ( int row) const

Returns the vertical header item for row row.

Definition at line 2039 of file qtablewidget.cpp.

◆ visualColumn()

int QTableWidget::visualColumn ( int logicalColumn) const

Returns the visual column of the given logicalColumn.

Definition at line 2431 of file qtablewidget.cpp.

◆ visualItemRect()

QRect QTableWidget::visualItemRect ( const QTableWidgetItem * item) const

Returns the rectangle on the viewport occupied by the item at item.

Definition at line 2454 of file qtablewidget.cpp.

◆ visualRow()

int QTableWidget::visualRow ( int logicalRow) const

Returns the visual row of the given logicalRow.

Definition at line 2422 of file qtablewidget.cpp.

◆ QTableModel

friend class QTableModel
friend

Definition at line 202 of file qtablewidget.h.

Property Documentation

◆ columnCount

int QTableWidget::columnCount
readwrite

the number of columns in the table

By default, for a table constructed without row and column counts, this property contains a value of 0.

Definition at line 197 of file qtablewidget.h.

◆ rowCount

int QTableWidget::rowCount
readwrite

the number of rows in the table

By default, for a table constructed without row and column counts, this property contains a value of 0.

Definition at line 196 of file qtablewidget.h.


The documentation for this class was generated from the following files: