QListWidgetItem Class

The QListWidgetItem class provides an item for use with the QListWidget item view class. 更多...

头文件: #include <QListWidgetItem>
qmake: QT += widgets

公有类型

enum ItemType { Type, UserType }

公有函数

QListWidgetItem(QListWidget *parent = Q_NULLPTR, int type = Type)
QListWidgetItem(const QString &text, QListWidget *parent = Q_NULLPTR, int type = Type)
QListWidgetItem(const QIcon &icon, const QString &text, QListWidget *parent = Q_NULLPTR, int type = Type)
QListWidgetItem(const QListWidgetItem &other)
virtual ~QListWidgetItem()
QBrush background() const
Qt::CheckState checkState() const
virtual QListWidgetItem *clone() const
virtual QVariant data(int role) const
Qt::ItemFlags flags() const
QFont font() const
QBrush foreground() const
QIcon icon() const
bool isHidden() const
bool isSelected() const
QListWidget *listWidget() const
virtual void read(QDataStream &in)
void setBackground(const QBrush &brush)
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 setHidden(bool hide)
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 setToolTip(const QString &toolTip)
void setWhatsThis(const QString &whatsThis)
QSize sizeHint() const
QString statusTip() const
QString text() const
int textAlignment() const
QString toolTip() const
int type() const
QString whatsThis() const
virtual void write(QDataStream &out) const
virtual bool operator<(const QListWidgetItem &other) const
QListWidgetItem &operator=(const QListWidgetItem &other)
QDataStream &operator<<(QDataStream &out, const QListWidgetItem &item)
QDataStream &operator>>(QDataStream &in, QListWidgetItem &item)

详细描述

The QListWidgetItem class provides an item for use with the QListWidget item view class.

A QListWidgetItem represents a single item in a QListWidget. Each item can hold several pieces of information, and will display them appropriately.

The item view convenience classes use a classic item-based interface rather than a pure model/view approach. For a more flexible list view widget, consider using the QListView class with a standard model.

List items can be inserted automatically into a list, when they are constructed, by specifying the list widget:


      new QListWidgetItem(tr("Hazel"), listWidget);

Alternatively, list items can also be created without a parent widget, and later inserted into a list using QListWidget::insertItem().

List items are typically used to display text() and an icon(). These are set with the setText() and setIcon() functions. The appearance of the text can be customized with setFont(), setForeground(), and setBackground(). Text in list items can be aligned using the setTextAlignment() function. Tooltips, status tips and "What's This?" help can be added to list items with setToolTip(), setStatusTip(), and setWhatsThis().

By default, items are enabled, selectable, checkable, and can be the source of drag and drop operations.

Each item's flags can be changed by calling setFlags() with the appropriate value (see Qt::ItemFlags). Checkable items can be checked, unchecked and partially checked with the setCheckState() function. The corresponding checkState() function indicates the item's current check state.

The isHidden() function can be used to determine whether the item is hidden. To hide an item, use setHidden().

Subclassing

When subclassing QListWidgetItem to provide custom items, it is possible to define new types for them enabling them to be distinguished from standard items. For subclasses that require this feature, ensure that you call the base class constructor with a new type value equal to or greater than UserType, within your constructor.

参见 QListWidget, Model/View Programming, QTreeWidgetItem, and QTableWidgetItem.

成员类型

enum QListWidgetItem::ItemType

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

ConstantValueDescription
QListWidgetItem::Type0The default type for list widget items.
QListWidgetItem::UserType1000The minimum value for custom types. Values below UserType are reserved by Qt.

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

参见 type().

成员函数

QListWidgetItem::QListWidgetItem(QListWidget *parent = Q_NULLPTR, int type = Type)

Constructs an empty list widget item of the specified type with the given parent. If parent is not specified, the item will need to be inserted into a list widget with QListWidget::insertItem().

This constructor inserts the item into the model of the parent that is passed to the constructor. If the model is sorted then the behavior of the insert is undetermined since the model will call the '<' operator method on the item which, at this point, is not yet constructed. To avoid the undetermined behavior, we recommend not to specify the parent and use QListWidget::insertItem() instead.

参见 type().

QListWidgetItem::QListWidgetItem(const QString &text, QListWidget *parent = Q_NULLPTR, int type = Type)

Constructs an empty list widget item of the specified type with the given text and parent. If the parent is not specified, the item will need to be inserted into a list widget with QListWidget::insertItem().

This constructor inserts the item into the model of the parent that is passed to the constructor. If the model is sorted then the behavior of the insert is undetermined since the model will call the '<' operator method on the item which, at this point, is not yet constructed. To avoid the undetermined behavior, we recommend not to specify the parent and use QListWidget::insertItem() instead.

参见 type().

QListWidgetItem::QListWidgetItem(const QIcon &icon, const QString &text, QListWidget *parent = Q_NULLPTR, int type = Type)

Constructs an empty list widget item of the specified type with the given icon, text and parent. If the parent is not specified, the item will need to be inserted into a list widget with QListWidget::insertItem().

This constructor inserts the item into the model of the parent that is passed to the constructor. If the model is sorted then the behavior of the insert is undetermined since the model will call the '<' operator method on the item which, at this point, is not yet constructed. To avoid the undetermined behavior, we recommend not to specify the parent and use QListWidget::insertItem() instead.

参见 type().

QListWidgetItem::QListWidgetItem(const QListWidgetItem &other)

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

This function is useful when reimplementing clone().

This function was introduced in Qt 4.1.

参见 data() and flags().

[virtual] QListWidgetItem::~QListWidgetItem()

Destroys the list item.

QBrush QListWidgetItem::background() const

Returns the brush used to display the list item's background.

This function was introduced in Qt 4.2.

参见 setBackground() and foreground().

Qt::CheckState QListWidgetItem::checkState() const

Returns the checked state of the list item (see Qt::CheckState).

参见 setCheckState() and flags().

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

Creates an exact copy of the item.

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

Returns the item's data for a given role. Reimplement this function if you need extra roles or special behavior for certain roles.

参见 Qt::ItemDataRole and setData().

Qt::ItemFlags QListWidgetItem::flags() const

Returns the item flags for this item (see Qt::ItemFlags).

参见 setFlags().

QFont QListWidgetItem::font() const

Returns the font used to display this list item's text.

参见 setFont().

QBrush QListWidgetItem::foreground() const

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

This function was introduced in Qt 4.2.

参见 setForeground() and background().

QIcon QListWidgetItem::icon() const

Returns the list item's icon.

参见 setIcon() and iconSize.

bool QListWidgetItem::isHidden() const

Returns true if the item is hidden; otherwise returns false.

This function was introduced in Qt 4.2.

参见 setHidden().

bool QListWidgetItem::isSelected() const

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

This function was introduced in Qt 4.2.

参见 setSelected().

QListWidget *QListWidgetItem::listWidget() const

Returns the list widget containing the item.

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

Reads the item from stream in.

参见 write().

void QListWidgetItem::setBackground(const QBrush &brush)

Sets the background brush of the list item to the given brush.

This function was introduced in Qt 4.2.

参见 background() and setForeground().

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

Sets the check state of the list item to state.

参见 checkState().

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

Sets the data for a given role to the given value. Reimplement this function if you need extra roles or special behavior for certain roles.

参见 Qt::ItemDataRole and data().

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

Sets the item flags for the list item to flags.

参见 flags() and Qt::ItemFlags.

void QListWidgetItem::setFont(const QFont &font)

Sets the font used when painting the item to the given font.

参见 font().

void QListWidgetItem::setForeground(const QBrush &brush)

Sets the foreground brush of the list item to the given brush.

This function was introduced in Qt 4.2.

参见 foreground() and setBackground().

void QListWidgetItem::setHidden(bool hide)

Hides the item if hide is true; otherwise shows the item.

This function was introduced in Qt 4.2.

参见 isHidden().

void QListWidgetItem::setIcon(const QIcon &icon)

Sets the icon for the list item to the given icon.

参见 icon(), text(), and iconSize.

void QListWidgetItem::setSelected(bool select)

Sets the selected state of the item to select.

This function was introduced in Qt 4.2.

参见 isSelected().

void QListWidgetItem::setSizeHint(const QSize &size)

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

This function was introduced in Qt 4.1.

参见 sizeHint().

void QListWidgetItem::setStatusTip(const QString &statusTip)

Sets the status tip for the list item to the text specified by statusTip. QListWidget mouseTracking needs to be enabled for this feature to work.

参见 statusTip(), setToolTip(), setWhatsThis(), and QWidget::setMouseTracking().

void QListWidgetItem::setText(const QString &text)

Sets the text for the list widget item's to the given text.

参见 text().

void QListWidgetItem::setTextAlignment(int alignment)

Sets the list item's text alignment to alignment.

参见 textAlignment() and Qt::AlignmentFlag.

void QListWidgetItem::setToolTip(const QString &toolTip)

Sets the tooltip for the list item to the text specified by toolTip.

参见 toolTip(), setStatusTip(), and setWhatsThis().

void QListWidgetItem::setWhatsThis(const QString &whatsThis)

Sets the "What's This?" help for the list item to the text specified by whatsThis.

参见 whatsThis(), setStatusTip(), and setToolTip().

QSize QListWidgetItem::sizeHint() const

Returns the size hint set for the list item.

This function was introduced in Qt 4.1.

参见 setSizeHint().

QString QListWidgetItem::statusTip() const

Returns the list item's status tip.

参见 setStatusTip().

QString QListWidgetItem::text() const

Returns the list item's text.

参见 setText().

int QListWidgetItem::textAlignment() const

Returns the text alignment for the list item.

参见 setTextAlignment() and Qt::AlignmentFlag.

QString QListWidgetItem::toolTip() const

Returns the list item's tooltip.

参见 setToolTip(), statusTip(), and whatsThis().

int QListWidgetItem::type() const

Returns the type passed to the QListWidgetItem constructor.

QString QListWidgetItem::whatsThis() const

Returns the list item's "What's This?" help text.

参见 setWhatsThis(), statusTip(), and toolTip().

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

Writes the item to stream out.

参见 read().

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

Returns true if this item's text is less then other item's text; otherwise returns false.

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

Assigns other's data and flags to this item. Note that type() and listWidget() are not copied.

This function is useful when reimplementing clone().

参见 data() and flags().

相关非成员

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

Writes the list widget item item to stream out.

This operator uses QListWidgetItem::write().

参见 Serializing Qt Data Types.

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

Reads a list widget item from stream in into item.

This operator uses QListWidgetItem::read().

参见 Serializing Qt Data Types.