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
qlistwidget.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QLISTWIDGET_H
5#define QLISTWIDGET_H
6
7#include <QtWidgets/qlistview.h>
8#include <QtWidgets/qtwidgetsglobal.h>
9#include <QtCore/qlist.h>
10#include <QtCore/qitemselectionmodel.h>
11#include <QtCore/qvariant.h>
12
14
16
17class QListWidget;
18class QListModel;
19class QWidgetItemData;
21
22class Q_WIDGETS_EXPORT QListWidgetItem
23{
24 friend class QListModel;
25 friend class QListWidget;
26public:
27 enum ItemType { Type = 0, UserType = 1000 };
28 explicit QListWidgetItem(QListWidget *listview = nullptr, int type = Type);
29 explicit QListWidgetItem(const QString &text, QListWidget *listview = nullptr, int type = Type);
30 explicit QListWidgetItem(const QIcon &icon, const QString &text,
31 QListWidget *listview = nullptr, int type = Type);
33 virtual ~QListWidgetItem();
34
35 virtual QListWidgetItem *clone() const;
36
37 inline QListWidget *listWidget() const { return view; }
38
39 void setSelected(bool select);
40 bool isSelected() const;
41
42 inline void setHidden(bool hide);
43 inline bool isHidden() const;
44
45 inline Qt::ItemFlags flags() const { return itemFlags; }
46 void setFlags(Qt::ItemFlags flags);
47
48 inline QString text() const
49 { return data(Qt::DisplayRole).toString(); }
50 inline void setText(const QString &text);
51
52 inline QIcon icon() const
53 { return qvariant_cast<QIcon>(data(Qt::DecorationRole)); }
54 inline void setIcon(const QIcon &icon);
55
56 inline QString statusTip() const
57 { return data(Qt::StatusTipRole).toString(); }
58 inline void setStatusTip(const QString &statusTip);
59
60#if QT_CONFIG(tooltip)
61 inline QString toolTip() const
62 { return data(Qt::ToolTipRole).toString(); }
63 inline void setToolTip(const QString &toolTip);
64#endif
65
66#if QT_CONFIG(whatsthis)
67 inline QString whatsThis() const
68 { return data(Qt::WhatsThisRole).toString(); }
69 inline void setWhatsThis(const QString &whatsThis);
70#endif
71
72 inline QFont font() const
73 { return qvariant_cast<QFont>(data(Qt::FontRole)); }
74 inline void setFont(const QFont &font);
75
76#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
77 inline int textAlignment() const
78 { return data(Qt::TextAlignmentRole).toInt(); }
79#else
80 inline Qt::Alignment textAlignment() const
81 { return qvariant_cast<Qt::Alignment>(data(Qt::TextAlignmentRole)); }
82#endif
83#if QT_DEPRECATED_SINCE(6, 4)
84 QT_DEPRECATED_VERSION_X_6_4("Use the overload taking Qt::Alignment")
85 inline void setTextAlignment(int alignment)
89#endif
92
93 inline QBrush background() const
94 { return qvariant_cast<QBrush>(data(Qt::BackgroundRole)); }
97
98 inline QBrush foreground() const
99 { return qvariant_cast<QBrush>(data(Qt::ForegroundRole)); }
100 inline void setForeground(const QBrush &brush)
102
104 { return qvariant_cast<Qt::CheckState>(data(Qt::CheckStateRole)); }
106 { setData(Qt::CheckStateRole, static_cast<int>(state)); }
107
108 inline QSize sizeHint() const
109 { return qvariant_cast<QSize>(data(Qt::SizeHintRole)); }
110 inline void setSizeHint(const QSize &size)
111 { setData(Qt::SizeHintRole, size.isValid() ? QVariant(size) : QVariant()); }
112
113 virtual QVariant data(int role) const;
114 virtual void setData(int role, const QVariant &value);
115
116 virtual bool operator<(const QListWidgetItem &other) const;
117
118#ifndef QT_NO_DATASTREAM
119 virtual void read(QDataStream &in);
120 virtual void write(QDataStream &out) const;
121#endif
122 QListWidgetItem &operator=(const QListWidgetItem &other);
123
124 inline int type() const { return rtti; }
125
126private:
127 QListModel *listModel() const;
128 int rtti;
131 Qt::ItemFlags itemFlags;
132};
133
134inline void QListWidgetItem::setText(const QString &atext)
135{ setData(Qt::DisplayRole, atext); }
136
137inline void QListWidgetItem::setIcon(const QIcon &aicon)
138{ setData(Qt::DecorationRole, aicon); }
139
140inline void QListWidgetItem::setStatusTip(const QString &astatusTip)
141{ setData(Qt::StatusTipRole, astatusTip); }
142
143#if QT_CONFIG(tooltip)
144inline void QListWidgetItem::setToolTip(const QString &atoolTip)
145{ setData(Qt::ToolTipRole, atoolTip); }
146#endif
147
148#if QT_CONFIG(whatsthis)
149inline void QListWidgetItem::setWhatsThis(const QString &awhatsThis)
150{ setData(Qt::WhatsThisRole, awhatsThis); }
151#endif
152
153inline void QListWidgetItem::setFont(const QFont &afont)
154{ setData(Qt::FontRole, afont); }
155
156#ifndef QT_NO_DATASTREAM
159#endif
160
162
163class Q_WIDGETS_EXPORT QListWidget : public QListView
164{
167 Q_PROPERTY(int currentRow READ currentRow WRITE setCurrentRow NOTIFY currentRowChanged
168 USER true)
169 Q_PROPERTY(bool sortingEnabled READ isSortingEnabled WRITE setSortingEnabled)
170
171 friend class QListWidgetItem;
172 friend class QListModel;
173public:
174 explicit QListWidget(QWidget *parent = nullptr);
175 ~QListWidget();
176
177 void setSelectionModel(QItemSelectionModel *selectionModel) override;
178
179 QListWidgetItem *item(int row) const;
180 int row(const QListWidgetItem *item) const;
181 void insertItem(int row, QListWidgetItem *item);
182 void insertItem(int row, const QString &label);
183 void insertItems(int row, const QStringList &labels);
184 inline void addItem(const QString &label) { insertItem(count(), label); }
185 inline void addItem(QListWidgetItem *item);
186 inline void addItems(const QStringList &labels) { insertItems(count(), labels); }
187 QListWidgetItem *takeItem(int row);
188 int count() const;
189
190 QListWidgetItem *currentItem() const;
191 void setCurrentItem(QListWidgetItem *item);
192 void setCurrentItem(QListWidgetItem *item, QItemSelectionModel::SelectionFlags command);
193
194 int currentRow() const;
195 void setCurrentRow(int row);
196 void setCurrentRow(int row, QItemSelectionModel::SelectionFlags command);
197
198 QListWidgetItem *itemAt(const QPoint &p) const;
199 inline QListWidgetItem *itemAt(int x, int y) const;
200 QRect visualItemRect(const QListWidgetItem *item) const;
201
202 void sortItems(Qt::SortOrder order = Qt::AscendingOrder);
203 void setSortingEnabled(bool enable);
204 bool isSortingEnabled() const;
205
206 void editItem(QListWidgetItem *item);
207 void openPersistentEditor(QListWidgetItem *item);
208 void closePersistentEditor(QListWidgetItem *item);
210 bool isPersistentEditorOpen(QListWidgetItem *item) const;
211
212 QWidget *itemWidget(QListWidgetItem *item) const;
213 void setItemWidget(QListWidgetItem *item, QWidget *widget);
214 inline void removeItemWidget(QListWidgetItem *item);
215
216 QList<QListWidgetItem*> selectedItems() const;
217 QList<QListWidgetItem*> findItems(const QString &text, Qt::MatchFlags flags) const;
218
219 QList<QListWidgetItem*> items(const QMimeData *data) const;
220
221 QModelIndex indexFromItem(const QListWidgetItem *item) const;
222 QListWidgetItem *itemFromIndex(const QModelIndex &index) const;
223
224protected:
225#if QT_CONFIG(draganddrop)
226 void dropEvent(QDropEvent *event) override;
227#endif
228public Q_SLOTS:
229 void scrollToItem(const QListWidgetItem *item, QAbstractItemView::ScrollHint hint = EnsureVisible);
230 void clear();
231
239
241 void currentTextChanged(const QString &currentText);
242 void currentRowChanged(int currentRow);
243
245
246protected:
247 bool event(QEvent *e) override;
248 virtual QStringList mimeTypes() const;
249 virtual QMimeData *mimeData(const QList<QListWidgetItem *> &items) const;
250#if QT_CONFIG(draganddrop)
251 virtual bool dropMimeData(int index, const QMimeData *data, Qt::DropAction action);
252 virtual Qt::DropActions supportedDropActions() const;
253#endif
254
255private:
256 void setModel(QAbstractItemModel *model) override;
257 Qt::SortOrder sortOrder() const;
258
259 Q_DECLARE_PRIVATE(QListWidget)
260 Q_DISABLE_COPY(QListWidget)
261};
262
264{ setItemWidget(aItem, nullptr); }
265
267{ insertItem(count(), aitem); }
268
269inline QListWidgetItem *QListWidget::itemAt(int ax, int ay) const
270{ return itemAt(QPoint(ax, ay)); }
271
272inline void QListWidgetItem::setHidden(bool ahide)
273{ if (view) view->setRowHidden(view->row(this), ahide); }
274
275inline bool QListWidgetItem::isHidden() const
276{ return (view ? view->isRowHidden(view->row(this)) : false); }
277
279
280#endif // QLISTWIDGET_H
bool isPersistentEditorOpen(const QModelIndex &index) const
ScrollHint
\value EnsureVisible Scroll to ensure that the item is visible.
\inmodule QtGui
Definition qbrush.h:30
\inmodule QtCore\reentrant
Definition qdatastream.h:46
\inmodule QtCore
Definition qcoreevent.h:45
\reentrant
Definition qfont.h:22
The QIcon class provides scalable icons in different modes and states.
Definition qicon.h:20
The QListView class provides a list or icon view onto a model.
Definition qlistview.h:17
void setRowHidden(int row, bool hide)
If hide is true, the given row will be hidden; otherwise the row will be shown.
bool isRowHidden(int row) const
Returns true if the row is hidden; otherwise returns false.
The QListWidgetItem class provides an item for use with the QListWidget item view class.
Definition qlistwidget.h:23
void setHidden(bool hide)
QBrush background() const
Definition qlistwidget.h:93
void setCheckState(Qt::CheckState state)
Sets the check state of the list item to state.
void setText(const QString &text)
Sets the text for the list widget item's to the given text.
Qt::CheckState checkState() const
Returns the checked state of the list item (see \l{Qt::CheckState}).
QString text() const
Returns the list item's text.
Definition qlistwidget.h:48
QBrush foreground() const
Definition qlistwidget.h:98
QSize sizeHint() const
QIcon icon() const
Returns the list item's icon.
Definition qlistwidget.h:52
void setBackground(const QBrush &brush)
Definition qlistwidget.h:95
bool isHidden() const
ItemType
This enum describes the types that are used to describe list widget items.
Definition qlistwidget.h:27
void setFont(const QFont &font)
Sets the font used when painting the item to the given font.
void setStatusTip(const QString &statusTip)
Sets the status tip for the list item to the text specified by statusTip.
int type() const
Returns the type passed to the QListWidgetItem constructor.
virtual void setData(int role, const QVariant &value)
Sets the data for a given role to the given value.
void setForeground(const QBrush &brush)
void setIcon(const QIcon &icon)
Sets the icon for the list item to the given icon.
Qt::Alignment textAlignment() const
Definition qlistwidget.h:80
void setTextAlignment(Qt::Alignment alignment)
\obsolete [6.4] Use the overload that takes a Qt::Alignment argument.
Definition qlistwidget.h:90
QFont font() const
Returns the font used to display this list item's text.
Definition qlistwidget.h:72
void setSizeHint(const QSize &size)
QListWidget * listWidget() const
Returns the list widget containing the item.
Definition qlistwidget.h:37
Qt::ItemFlags flags() const
Returns the item flags for this item (see \l{Qt::ItemFlags}).
Definition qlistwidget.h:45
QString statusTip() const
Returns the list item's status tip.
Definition qlistwidget.h:56
The QListWidget class provides an item-based list widget.
void addItems(const QStringList &labels)
Inserts items with the text labels at the end of the list widget.
void itemClicked(QListWidgetItem *item)
This signal is emitted with the specified item when a mouse button is clicked on an item in the widge...
void removeItemWidget(QListWidgetItem *item)
void itemChanged(QListWidgetItem *item)
This signal is emitted whenever the data of item has changed.
void currentTextChanged(const QString &currentText)
This signal is emitted whenever the current item changes.
void insertItem(int row, QListWidgetItem *item)
Inserts the item at the position in the list given by row.
void currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
This signal is emitted whenever the current item changes.
void itemPressed(QListWidgetItem *item)
This signal is emitted with the specified item when a mouse button is pressed on an item in the widge...
QListWidgetItem * itemAt(const QPoint &p) const
Returns a pointer to the item at the coordinates p.
void setItemWidget(QListWidgetItem *item, QWidget *widget)
void itemEntered(QListWidgetItem *item)
This signal is emitted when the mouse cursor enters an item.
void addItem(const QString &label)
Inserts an item with the text label at the end of the list widget.
int row(const QListWidgetItem *item) const
Returns the row containing the given item.
void currentRowChanged(int currentRow)
This signal is emitted whenever the current item changes.
void itemSelectionChanged()
This signal is emitted whenever the selection changes.
int count
the number of items in the list including any hidden items.
void itemDoubleClicked(QListWidgetItem *item)
This signal is emitted with the specified item when a mouse button is double clicked on an item in th...
void itemActivated(QListWidgetItem *item)
This signal is emitted when the item is activated.
\inmodule QtCore
Definition qmimedata.h:16
\inmodule QtCore
\inmodule QtCore\reentrant
Definition qpoint.h:25
\inmodule QtCore\reentrant
Definition qrect.h:30
\inmodule QtCore
Definition qsize.h:25
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
Definition qvariant.h:65
static auto fromValue(T &&value) noexcept(std::is_nothrow_copy_constructible_v< T > &&Private::CanUseInternalSpace< T >) -> std::enable_if_t< std::conjunction_v< std::is_copy_constructible< T >, std::is_destructible< T > >, QVariant >
Definition qvariant.h:536
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
QOpenGLWidget * widget
[1]
b clear()
QString text
uint alignment
else opt state
[0]
Combined button and popup list for selecting options.
CheckState
AlignmentFlag
Definition qnamespace.h:143
@ WhatsThisRole
@ FontRole
@ TextAlignmentRole
@ ForegroundRole
@ DecorationRole
@ BackgroundRole
@ CheckStateRole
@ StatusTipRole
@ ToolTipRole
@ DisplayRole
@ SizeHintRole
SortOrder
Definition qnamespace.h:121
@ AscendingOrder
Definition qnamespace.h:122
@ NoBrush
DropAction
Definition brush.cpp:5
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
Q_WIDGETS_EXPORT QDataStream & operator>>(QDataStream &in, QListWidgetItem &item)
Q_WIDGETS_EXPORT QDataStream & operator<<(QDataStream &out, const QListWidgetItem &item)
GLint GLint GLint GLint GLint x
[0]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint index
[2]
GLenum GLenum GLsizei count
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum type
GLuint GLsizei const GLchar * label
[43]
GLbitfield flags
GLboolean enable
GLint y
struct _cl_event * event
GLuint in
GLenum GLenum GLsizei void * row
GLfloat GLfloat p
[1]
GLfixed GLfixed GLint GLint order
decorationRoleName toolTipRoleName whatsThisRoleName setTextAlignment
decorationRoleName toolTipRoleName setWhatsThis
decorationRoleName setToolTip
static bool operator<(const QSettingsIniKey &k1, const QSettingsIniKey &k2)
static QT_BEGIN_NAMESPACE QVariant hint(QPlatformIntegration::StyleHint h)
#define QT_REQUIRE_CONFIG(feature)
#define QT_DEPRECATED_VERSION_X_6_4(text)
#define Q_PROPERTY(...)
#define Q_OBJECT
#define Q_SLOTS
#define Q_SIGNALS
ReturnedValue read(const char *data)
#define explicit
view setModel(model)
[17] //! [18]
QSqlQueryModel * model
[16]
gzip write("uncompressed data")
QTextStream out(stdout)
[7]
mimeData setData("text/csv", csvData)
QMimeData * mimeData
QSharedPointer< T > other(t)
[5]
scene addItem(form)
QGraphicsItem * item
edit hide()
selection select(topLeft, bottomRight)
QList< QTreeWidgetItem * > items
myAction setIcon(SomeIcon)
insertRed setText("insert red text")
QQuickView * view
[0]
Definition moc.h:23