QTableWidgetItem Class

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

Header: #include <QTableWidgetItem>
qmake: QT += widgets

Public Types

enum ItemType { Type, UserType }

Public Functions

QTableWidgetItem(int type = Type)
QTableWidgetItem(const QString &text, int type = Type)
QTableWidgetItem(const QIcon &icon, const QString &text, int type = Type)
QTableWidgetItem(const QTableWidgetItem &other)
virtual ~QTableWidgetItem()
QBrush background() const
QColor backgroundColor() const
Qt::CheckState checkState() const
virtual QTableWidgetItem *clone() const
int column() const
virtual QVariant data(int role) const
Qt::ItemFlags flags() const
QFont font() const
QBrush foreground() const
QIcon icon() const
bool isSelected() const
virtual void read(QDataStream &in)
int row() const
void setBackground(const QBrush &brush)
void setBackgroundColor(const QColor &color)
void setCheckState(Qt::CheckState state)
virtual void setData(int role, const QVariant &value)
void setFlags(Qt::ItemFlags flags)
void setFont(const QFont &font)
void setForeground(const QBrush &brush)
void setIcon(const QIcon &icon)
void setSelected(bool select)
void setSizeHint(const QSize &size)
void setStatusTip(const QString &statusTip)
void setText(const QString &text)
void setTextAlignment(int alignment)
void setTextColor(const QColor &color)
void setToolTip(const QString &toolTip)
void setWhatsThis(const QString &whatsThis)
QSize sizeHint() const
QString statusTip() const
QTableWidget *tableWidget() const
QString text() const
int textAlignment() const
QColor textColor() const
QString toolTip() const
int type() const
QString whatsThis() const
virtual void write(QDataStream &out) const
virtual bool operator<(const QTableWidgetItem &other) const
QTableWidgetItem &operator=(const QTableWidgetItem &other)
QDataStream &operator<<(QDataStream &out, const QTableWidgetItem &item)
QDataStream &operator>>(QDataStream &in, QTableWidgetItem &item)

Detailed Description

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

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 Qt::ItemFlags). Checkable items can be checked and unchecked with the setCheckState() function. The corresponding checkState() function indicates whether the item is currently checked.

Subclassing

When subclassing QTableWidgetItem to provide custom items, it is possible to define new types for them so that they can be distinguished from standard items. The constructors for subclasses that require this feature need to call the base class constructor with a new type value equal to or greater than UserType.

See also QTableWidget, Model/View Programming, QListWidgetItem, and QTreeWidgetItem.

Member Type Documentation

enum QTableWidgetItem::ItemType

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

ConstantValueDescription
QTableWidgetItem::Type0The default type for table widget items.
QTableWidgetItem::UserType1000The 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().

Member Function Documentation

QTableWidgetItem::QTableWidgetItem(int type = Type)

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

See also type().

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

Constructs a table item with the given text.

See also type().

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

Constructs a table item with the given icon and text.

See also type().

QTableWidgetItem::QTableWidgetItem(const QTableWidgetItem &other)

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

This function is useful when reimplementing clone().

This function was introduced in Qt 4.1.

See also data() and flags().

[virtual] QTableWidgetItem::~QTableWidgetItem()

Destroys the table item.

QBrush QTableWidgetItem::background() const

See also setBackground().

QColor QTableWidgetItem::backgroundColor() const

See also setBackgroundColor().

Qt::CheckState QTableWidgetItem::checkState() const

See also setCheckState().

[virtual] QTableWidgetItem *QTableWidgetItem::clone() const

Creates a copy of the item.

int QTableWidgetItem::column() const

[virtual] QVariant QTableWidgetItem::data(int role) const

Returns the item's data for the given role.

See also setData().

Qt::ItemFlags QTableWidgetItem::flags() const

See also setFlags().

QFont QTableWidgetItem::font() const

See also setFont().

QBrush QTableWidgetItem::foreground() const

See also setForeground().

QIcon QTableWidgetItem::icon() const

See also setIcon().

bool QTableWidgetItem::isSelected() const

[virtual] void QTableWidgetItem::read(QDataStream &in)

Reads the item from stream in.

See also write().

int QTableWidgetItem::row() const

void QTableWidgetItem::setBackground(const QBrush &brush)

See also background().

void QTableWidgetItem::setBackgroundColor(const QColor &color)

See also backgroundColor().

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

See also checkState().

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

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 and data().

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

See also flags().

void QTableWidgetItem::setFont(const QFont &font)

See also font().

void QTableWidgetItem::setForeground(const QBrush &brush)

See also foreground().

void QTableWidgetItem::setIcon(const QIcon &icon)

See also icon().

void QTableWidgetItem::setSelected(bool select)

See also isSelected().

void QTableWidgetItem::setSizeHint(const QSize &size)

See also sizeHint().

void QTableWidgetItem::setStatusTip(const QString &statusTip)

See also statusTip().

void QTableWidgetItem::setText(const QString &text)

See also text().

void QTableWidgetItem::setTextAlignment(int alignment)

See also textAlignment().

void QTableWidgetItem::setTextColor(const QColor &color)

See also textColor().

void QTableWidgetItem::setToolTip(const QString &toolTip)

See also toolTip().

void QTableWidgetItem::setWhatsThis(const QString &whatsThis)

See also whatsThis().

QSize QTableWidgetItem::sizeHint() const

See also setSizeHint().

QString QTableWidgetItem::statusTip() const

See also setStatusTip().

QTableWidget *QTableWidgetItem::tableWidget() const

QString QTableWidgetItem::text() const

See also setText().

int QTableWidgetItem::textAlignment() const

See also setTextAlignment().

QColor QTableWidgetItem::textColor() const

See also setTextColor().

QString QTableWidgetItem::toolTip() const

See also setToolTip().

int QTableWidgetItem::type() const

QString QTableWidgetItem::whatsThis() const

See also setWhatsThis().

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

Writes the item to stream out.

See also read().

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

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

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() and flags().

Related Non-Members

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

Writes the table widget item item to stream out.

This operator uses QTableWidgetItem::write().

See also Serializing Qt Data Types.

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

Reads a table widget item from stream in into item.

This operator uses QTableWidgetItem::read().

See also Serializing Qt Data Types.