Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QTableWidgetItem Class Reference

The QTableWidgetItem class provides an item for use with the QTableWidget class. More...

#include <qtablewidget.h>

+ Collaboration diagram for QTableWidgetItem:

Public Types

enum  ItemType { Type = 0 , UserType = 1000 }
 This enum describes the types that are used to describe table widget items. More...
 

Public Member Functions

 QTableWidgetItem (int type=Type)
 Constructs a table item of the specified type that does not belong to any table.
 
 QTableWidgetItem (const QString &text, int type=Type)
 Constructs a table item with the given text.
 
 QTableWidgetItem (const QIcon &icon, const QString &text, int type=Type)
 Constructs a table item with the given icon and text.
 
 QTableWidgetItem (const QTableWidgetItem &other)
 
virtual ~QTableWidgetItem ()
 Destroys the table item.
 
virtual QTableWidgetItemclone () const
 Creates a copy of the item.
 
QTableWidgettableWidget () const
 Returns the table widget that contains the item.
 
int row () const
 
int column () const
 
void setSelected (bool select)
 
bool isSelected () const
 
Qt::ItemFlags flags () const
 Returns the flags used to describe the item.
 
void setFlags (Qt::ItemFlags flags)
 Sets the flags for the item to the given flags.
 
QString text () const
 Returns the item's text.
 
void setText (const QString &text)
 Sets the item's text to the text specified.
 
QIcon icon () const
 Returns the item's icon.
 
void setIcon (const QIcon &icon)
 Sets the item's icon to the icon specified.
 
QString statusTip () const
 Returns the item's status tip.
 
void setStatusTip (const QString &statusTip)
 Sets the status tip for the table item to the text specified by statusTip.
 
QFont font () const
 Returns the font used to render the item's text.
 
void setFont (const QFont &font)
 Sets the font used to display the item's text to the given font.
 
Qt::Alignment textAlignment () const
 
void setTextAlignment (Qt::Alignment alignment)
 \obsolete [6.4] Use the overload that takes a Qt::Alignment argument.
 
QBrush background () const
 
void setBackground (const QBrush &brush)
 
QBrush foreground () const
 
void setForeground (const QBrush &brush)
 
Qt::CheckState checkState () const
 Returns the checked state of the table item.
 
void setCheckState (Qt::CheckState state)
 Sets the check state of the table item to be state.
 
QSize sizeHint () const
 
void setSizeHint (const QSize &size)
 
virtual QVariant data (int role) const
 Returns the item's data for the given role.
 
virtual void setData (int role, const QVariant &value)
 Sets the item's data for the given role to the specified value.
 
virtual bool operator< (const QTableWidgetItem &other) const
 Returns true if the item is less than the other item; otherwise returns false.
 
virtual void read (QDataStream &in)
 Reads the item from stream in.
 
virtual void write (QDataStream &out) const
 Writes the item to stream out.
 
QTableWidgetItemoperator= (const QTableWidgetItem &other)
 Assigns other's data and flags to this item.
 
int type () const
 Returns the type passed to the QTableWidgetItem constructor.
 

Friends

class QTableWidget
 
class QTableModel
 

Related Symbols

(Note that these are not member symbols.)

QDataStreamoperator>> (QDataStream &in, QTableWidgetItem &item)
 Reads a table widget item from stream in into item.
 
QDataStreamoperator<< (QDataStream &out, const QTableWidgetItem &item)
 Writes the table widget item item to stream out.
 

Detailed Description

The QTableWidgetItem class provides an item for use with the QTableWidget class.

\inmodule QtWidgets

Table items are used to hold pieces of information for table widgets. Items usually contain text, icons, or checkboxes

The QTableWidgetItem class is a convenience class that replaces the QTableItem class in Qt 3. It provides an item for use with the QTableWidget class.

Top-level items are constructed without a parent then inserted at the position specified by a pair of row and column numbers:

QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(
pow(row, column+1)));
tableWidget->setItem(row, column, newItem);

Each item can have its own background brush which is set with the setBackground() function. The current background brush can be found with background(). The text label for each item can be rendered with its own font and brush. These are specified with the setFont() and setForeground() functions, and read with font() and foreground().

By default, items are enabled, editable, selectable, checkable, and can be used both as the source of a drag and drop operation and as a drop target. Each item's flags can be changed by calling setFlags() with the appropriate value (see \l{Qt::ItemFlags}). Checkable items can be checked and unchecked with the setCheckState() function. The corresponding checkState() function indicates whether the item is currently checked.

Definition at line 49 of file qtablewidget.h.

Member Enumeration Documentation

◆ ItemType

This enum describes the types that are used to describe table widget items.

\value Type The default type for table widget items. \value UserType The minimum value for custom types. Values below UserType are reserved by Qt.

You can define new user types in QTableWidgetItem subclasses to ensure that custom items are treated specially.

See also
type()
Enumerator
Type 
UserType 

Definition at line 54 of file qtablewidget.h.

Constructor & Destructor Documentation

◆ QTableWidgetItem() [1/4]

QTableWidgetItem::QTableWidgetItem ( int type = Type)
explicit

Constructs a table item of the specified type that does not belong to any table.

See also
type()

Definition at line 1306 of file qtablewidget.cpp.

Referenced by clone().

+ Here is the caller graph for this function:

◆ QTableWidgetItem() [2/4]

QTableWidgetItem::QTableWidgetItem ( const QString & text,
int type = Type )
explicit

Constructs a table item with the given text.

See also
type()

Definition at line 1322 of file qtablewidget.cpp.

References Qt::DisplayRole, setData(), and text().

+ Here is the call graph for this function:

◆ QTableWidgetItem() [3/4]

QTableWidgetItem::QTableWidgetItem ( const QIcon & icon,
const QString & text,
int type = Type )
explicit

Constructs a table item with the given icon and text.

See also
type()

Definition at line 1339 of file qtablewidget.cpp.

References Qt::DecorationRole, Qt::DisplayRole, icon(), setData(), and text().

+ Here is the call graph for this function:

◆ QTableWidgetItem() [4/4]

QTableWidgetItem::QTableWidgetItem ( const QTableWidgetItem & other)
Since
4.1

Constructs a copy of other. Note that type() and tableWidget() are not copied.

This function is useful when reimplementing clone().

See also
data(), flags()

Definition at line 1500 of file qtablewidget.cpp.

◆ ~QTableWidgetItem()

QTableWidgetItem::~QTableWidgetItem ( )
virtual

Destroys the table item.

Definition at line 1355 of file qtablewidget.cpp.

References model.

Member Function Documentation

◆ background()

QBrush QTableWidgetItem::background ( ) const
inline
Since
4.2

Returns the brush used to render the item's background.

See also
foreground()

Definition at line 119 of file qtablewidget.h.

References Qt::BackgroundRole.

◆ checkState()

Qt::CheckState QTableWidgetItem::checkState ( ) const
inline

Returns the checked state of the table item.

See also
flags()

Definition at line 129 of file qtablewidget.h.

References Qt::CheckStateRole.

◆ clone()

QTableWidgetItem * QTableWidgetItem::clone ( ) const
virtual

Creates a copy of the item.

Definition at line 1365 of file qtablewidget.cpp.

References QTableWidgetItem().

Referenced by QTableModel::createItem().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ column()

int QTableWidgetItem::column ( ) const
inline
Since
4.2

Returns the column of the item in the table. If the item is not in a table, this function will return -1.

See also
row()

Definition at line 326 of file qtablewidget.h.

References QTableWidget::column().

+ Here is the call graph for this function:

◆ data()

QVariant QTableWidgetItem::data ( int role) const
virtual

Returns the item's data for the given role.

Definition at line 1406 of file qtablewidget.cpp.

References Qt::DisplayRole, and Qt::EditRole.

Referenced by QTableModel::data(), QTableModel::headerData(), and QTableModel::setItemData().

+ Here is the caller graph for this function:

◆ flags()

Qt::ItemFlags QTableWidgetItem::flags ( ) const
inline

Returns the flags used to describe the item.

These determine whether the item can be checked, edited, and selected.

See also
setFlags()

Definition at line 71 of file qtablewidget.h.

◆ font()

QFont QTableWidgetItem::font ( ) const
inline

Returns the font used to render the item's text.

See also
setFont()

Definition at line 98 of file qtablewidget.h.

References Qt::FontRole.

◆ foreground()

QBrush QTableWidgetItem::foreground ( ) const
inline
Since
4.2

Returns the brush used to render the item's foreground (e.g. text).

See also
background()

Definition at line 124 of file qtablewidget.h.

References Qt::ForegroundRole.

◆ icon()

QIcon QTableWidgetItem::icon ( ) const
inline

Returns the item's icon.

See also
setIcon(), {QAbstractItemView::iconSize}{iconSize}

Definition at line 78 of file qtablewidget.h.

References Qt::DecorationRole.

Referenced by QTableWidgetItem().

+ Here is the caller graph for this function:

◆ isSelected()

bool QTableWidgetItem::isSelected ( ) const
Since
4.2

Returns true if the item is selected, otherwise returns false.

See also
setSelected()

Definition at line 1061 of file qtablewidget.cpp.

References QAbstractTableModel::index(), QItemSelectionModel::isSelected(), model, QAbstractItemView::model(), and QAbstractItemView::selectionModel().

+ Here is the call graph for this function:

◆ operator<()

bool QTableWidgetItem::operator< ( const QTableWidgetItem & other) const
virtual

Returns true if the item is less than the other item; otherwise returns false.

Definition at line 1420 of file qtablewidget.cpp.

References Qt::DisplayRole, other(), and QAbstractItemModelPrivate::variantLessThan().

+ Here is the call graph for this function:

◆ operator=()

QTableWidgetItem & QTableWidgetItem::operator= ( const QTableWidgetItem & other)

Assigns other's data and flags to this item.

Note that type() and tableWidget() are not copied.

This function is useful when reimplementing clone().

See also
data(), flags()

Definition at line 1515 of file qtablewidget.cpp.

References other().

+ Here is the call graph for this function:

◆ read()

void QTableWidgetItem::read ( QDataStream & in)
virtual

Reads the item from stream in.

See also
write()

Definition at line 1433 of file qtablewidget.cpp.

◆ row()

int QTableWidgetItem::row ( ) const
inline
Since
4.2

Returns the row of the item in the table. If the item is not in a table, this function will return -1.

See also
column()

Definition at line 323 of file qtablewidget.h.

References QTableWidget::row().

+ Here is the call graph for this function:

◆ setBackground()

void QTableWidgetItem::setBackground ( const QBrush & brush)
inline
Since
4.2

Sets the item's background brush to the specified brush. Setting a default-constructed brush will let the view use the default color from the style.

See also
setForeground()

Definition at line 121 of file qtablewidget.h.

References Qt::BackgroundRole, Qt::NoBrush, and setData().

+ Here is the call graph for this function:

◆ setCheckState()

void QTableWidgetItem::setCheckState ( Qt::CheckState state)
inline

Sets the check state of the table item to be state.

Definition at line 131 of file qtablewidget.h.

References Qt::CheckStateRole, setData(), and state.

+ Here is the call graph for this function:

◆ setData()

void QTableWidgetItem::setData ( int role,
const QVariant & value )
virtual

Sets the item's data for the given role to the specified value.

Note
The default implementation treats Qt::EditRole and Qt::DisplayRole as referring to the same data.
See also
Qt::ItemDataRole, data()

Definition at line 1378 of file qtablewidget.cpp.

References Qt::DisplayRole, Qt::EditRole, i, and model.

Referenced by QTableWidgetItem(), QTableWidgetItem(), QTableModel::setData(), setFont(), QTableModel::setHeaderData(), setIcon(), QTableModel::setItemData(), setStatusTip(), and setText().

+ Here is the caller graph for this function:

◆ setFlags()

void QTableWidgetItem::setFlags ( Qt::ItemFlags flags)

Sets the flags for the item to the given flags.

These determine whether the item can be selected or modified.

See also
flags()

Definition at line 1108 of file qtablewidget.cpp.

References model.

◆ setFont()

void QTableWidgetItem::setFont ( const QFont & font)
inline

Sets the font used to display the item's text to the given font.

See also
font(), setText(), setForeground()

Definition at line 182 of file qtablewidget.h.

References Qt::FontRole, and setData().

+ Here is the call graph for this function:

◆ setForeground()

void QTableWidgetItem::setForeground ( const QBrush & brush)
inline
Since
4.2

Sets the item's foreground brush to the specified brush. Setting a default-constructed brush will let the view use the default color from the style.

See also
setBackground()

Definition at line 126 of file qtablewidget.h.

References Qt::ForegroundRole, Qt::NoBrush, and setData().

+ Here is the call graph for this function:

◆ setIcon()

void QTableWidgetItem::setIcon ( const QIcon & icon)
inline

Sets the item's icon to the icon specified.

See also
icon(), setText(), {QAbstractItemView::iconSize}{iconSize}

Definition at line 166 of file qtablewidget.h.

References Qt::DecorationRole, and setData().

+ Here is the call graph for this function:

◆ setSelected()

void QTableWidgetItem::setSelected ( bool select)
Since
4.2

Sets the selected state of the item to select.

See also
isSelected()

Definition at line 1080 of file qtablewidget.cpp.

References QItemSelectionModel::Deselect, QAbstractTableModel::index(), model, QAbstractItemView::model(), QItemSelectionModel::Select, QItemSelectionModel::select(), select(), and QAbstractItemView::selectionModel().

+ Here is the call graph for this function:

◆ setSizeHint()

void QTableWidgetItem::setSizeHint ( const QSize & size)
inline
Since
4.1

Sets the size hint for the table item to be size. If no size hint is set or size is invalid, the item delegate will compute the size hint based on the item data.

Definition at line 136 of file qtablewidget.h.

References setData(), and Qt::SizeHintRole.

+ Here is the call graph for this function:

◆ setStatusTip()

void QTableWidgetItem::setStatusTip ( const QString & statusTip)
inline

Sets the status tip for the table item to the text specified by statusTip.

QTableWidget mouse tracking needs to be enabled for this feature to work.

See also
statusTip(), setToolTip(), setWhatsThis()

Definition at line 169 of file qtablewidget.h.

References setData(), and Qt::StatusTipRole.

+ Here is the call graph for this function:

◆ setText()

void QTableWidgetItem::setText ( const QString & text)
inline

Sets the item's text to the text specified.

See also
text(), setFont(), setForeground()

Definition at line 163 of file qtablewidget.h.

References Qt::DisplayRole, and setData().

Referenced by MainWindow::averageItems(), and MainWindow::sumItems().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setTextAlignment()

void QTableWidgetItem::setTextAlignment ( Qt::Alignment alignment)
inline

\obsolete [6.4] Use the overload that takes a Qt::Alignment argument.

Sets the text alignment for the item's text to the alignment specified.

See also
Qt::Alignment
Since
6.4

Sets the text alignment for the item's text to the alignment specified.

Definition at line 116 of file qtablewidget.h.

References alignment, QVariant::fromValue(), setData(), and Qt::TextAlignmentRole.

+ Here is the call graph for this function:

◆ sizeHint()

QSize QTableWidgetItem::sizeHint ( ) const
inline
Since
4.1

Returns the size hint set for the table item.

Definition at line 134 of file qtablewidget.h.

References Qt::SizeHintRole.

◆ statusTip()

QString QTableWidgetItem::statusTip ( ) const
inline

Returns the item's status tip.

See also
setStatusTip()

Definition at line 82 of file qtablewidget.h.

References Qt::StatusTipRole.

◆ tableWidget()

QTableWidget * QTableWidgetItem::tableWidget ( ) const
inline

Returns the table widget that contains the item.

Definition at line 63 of file qtablewidget.h.

References view.

◆ text()

QString QTableWidgetItem::text ( ) const
inline

Returns the item's text.

See also
setText()

Definition at line 74 of file qtablewidget.h.

References Qt::DisplayRole.

Referenced by QTableWidgetItem(), and QTableWidgetItem().

+ Here is the caller graph for this function:

◆ textAlignment()

int QTableWidgetItem::textAlignment ( ) const
inline

Returns the text alignment for the item's text.

Note
This function returns an int for historical reasons. It will be corrected to return Qt::Alignment in Qt 7.
See also
Qt::Alignment

Definition at line 106 of file qtablewidget.h.

References Qt::TextAlignmentRole.

◆ type()

int QTableWidgetItem::type ( ) const
inline

Returns the type passed to the QTableWidgetItem constructor.

Definition at line 150 of file qtablewidget.h.

◆ write()

void QTableWidgetItem::write ( QDataStream & out) const
virtual

Writes the item to stream out.

See also
read()

Definition at line 1443 of file qtablewidget.cpp.

References out.

Friends And Related Symbol Documentation

◆ operator<<()

QDataStream & operator<< ( QDataStream & out,
const QTableWidgetItem & item )
related

Writes the table widget item item to stream out.

This operator uses QTableWidgetItem::write().

See also
{Serializing Qt Data Types}

Definition at line 1482 of file qtablewidget.cpp.

References item, and out.

◆ operator>>()

QDataStream & operator>> ( QDataStream & in,
QTableWidgetItem & item )
related

Reads a table widget item from stream in into item.

This operator uses QTableWidgetItem::read().

See also
{Serializing Qt Data Types}

Definition at line 1467 of file qtablewidget.cpp.

References item.

◆ QTableModel

friend class QTableModel
friend

Definition at line 52 of file qtablewidget.h.

◆ QTableWidget

friend class QTableWidget
friend

Definition at line 51 of file qtablewidget.h.


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