Qt 4.8
qlistwidget.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QLISTWIDGET_H
43 #define QLISTWIDGET_H
44 
45 #include <QtGui/qlistview.h>
46 #include <QtCore/qvariant.h>
47 #include <QtCore/qvector.h>
48 #include <QtGui/qitemselectionmodel.h>
49 
51 
53 
54 QT_MODULE(Gui)
55 
56 #ifndef QT_NO_LISTWIDGET
57 
58 class QListWidget;
59 class QListModel;
60 class QWidgetItemData;
62 
64 {
65  friend class QListModel;
66  friend class QListWidget;
67 public:
68  enum ItemType { Type = 0, UserType = 1000 };
69  explicit QListWidgetItem(QListWidget *view = 0, int type = Type);
70  explicit QListWidgetItem(const QString &text, QListWidget *view = 0, int type = Type);
71  explicit QListWidgetItem(const QIcon &icon, const QString &text,
72  QListWidget *view = 0, int type = Type);
73  QListWidgetItem(const QListWidgetItem &other);
74  virtual ~QListWidgetItem();
75 
76  virtual QListWidgetItem *clone() const;
77 
78  inline QListWidget *listWidget() const { return view; }
79 
80  inline void setSelected(bool select);
81  inline bool isSelected() const;
82 
83  inline void setHidden(bool hide);
84  inline bool isHidden() const;
85 
86  inline Qt::ItemFlags flags() const { return itemFlags; }
87  void setFlags(Qt::ItemFlags flags);
88 
89  inline QString text() const
90  { return data(Qt::DisplayRole).toString(); }
91  inline void setText(const QString &text);
92 
93  inline QIcon icon() const
95  inline void setIcon(const QIcon &icon);
96 
97  inline QString statusTip() const
98  { return data(Qt::StatusTipRole).toString(); }
99  inline void setStatusTip(const QString &statusTip);
100 
101 #ifndef QT_NO_TOOLTIP
102  inline QString toolTip() const
103  { return data(Qt::ToolTipRole).toString(); }
104  inline void setToolTip(const QString &toolTip);
105 #endif
106 
107 #ifndef QT_NO_WHATSTHIS
108  inline QString whatsThis() const
109  { return data(Qt::WhatsThisRole).toString(); }
110  inline void setWhatsThis(const QString &whatsThis);
111 #endif
112 
113  inline QFont font() const
114  { return qvariant_cast<QFont>(data(Qt::FontRole)); }
115  inline void setFont(const QFont &font);
116 
117  inline int textAlignment() const
118  { return data(Qt::TextAlignmentRole).toInt(); }
119  inline void setTextAlignment(int alignment)
120  { setData(Qt::TextAlignmentRole, alignment); }
121 
122  inline QColor backgroundColor() const
124  virtual void setBackgroundColor(const QColor &color)
125  { setData(Qt::BackgroundColorRole, color); }
126 
127  inline QBrush background() const
129  inline void setBackground(const QBrush &brush)
130  { setData(Qt::BackgroundRole, brush); }
131 
132  inline QColor textColor() const
134  inline void setTextColor(const QColor &color)
135  { setData(Qt::TextColorRole, color); }
136 
137  inline QBrush foreground() const
139  inline void setForeground(const QBrush &brush)
140  { setData(Qt::ForegroundRole, brush); }
141 
142  inline Qt::CheckState checkState() const
143  { return static_cast<Qt::CheckState>(data(Qt::CheckStateRole).toInt()); }
145  { setData(Qt::CheckStateRole, static_cast<int>(state)); }
146 
147  inline QSize sizeHint() const
149  inline void setSizeHint(const QSize &size)
150  { setData(Qt::SizeHintRole, size); }
151 
152  virtual QVariant data(int role) const;
153  virtual void setData(int role, const QVariant &value);
154 
155  virtual bool operator<(const QListWidgetItem &other) const;
156 
157 #ifndef QT_NO_DATASTREAM
158  virtual void read(QDataStream &in);
159  virtual void write(QDataStream &out) const;
160 #endif
161  QListWidgetItem &operator=(const QListWidgetItem &other);
162 
163  inline int type() const { return rtti; }
164 
165 private:
166  int rtti;
170  Qt::ItemFlags itemFlags;
171 };
172 
173 inline void QListWidgetItem::setText(const QString &atext)
174 { setData(Qt::DisplayRole, atext); }
175 
176 inline void QListWidgetItem::setIcon(const QIcon &aicon)
177 { setData(Qt::DecorationRole, aicon); }
178 
179 inline void QListWidgetItem::setStatusTip(const QString &astatusTip)
180 { setData(Qt::StatusTipRole, astatusTip); }
181 
182 #ifndef QT_NO_TOOLTIP
183 inline void QListWidgetItem::setToolTip(const QString &atoolTip)
184 { setData(Qt::ToolTipRole, atoolTip); }
185 #endif
186 
187 #ifndef QT_NO_WHATSTHIS
188 inline void QListWidgetItem::setWhatsThis(const QString &awhatsThis)
189 { setData(Qt::WhatsThisRole, awhatsThis); }
190 #endif
191 
192 inline void QListWidgetItem::setFont(const QFont &afont)
193 { setData(Qt::FontRole, afont); }
194 
195 #ifndef QT_NO_DATASTREAM
198 #endif
199 
200 class QListWidgetPrivate;
201 
203 {
204  Q_OBJECT
205  int count;
208 
209  friend class QListWidgetItem;
210  friend class QListModel;
211 public:
212  explicit QListWidget(QWidget *parent = 0);
213  ~QListWidget();
214 
215  QListWidgetItem *item(int row) const;
216  int row(const QListWidgetItem *item) const;
217  void insertItem(int row, QListWidgetItem *item);
218  void insertItem(int row, const QString &label);
219  void insertItems(int row, const QStringList &labels);
220  inline void addItem(const QString &label) { insertItem(count(), label); }
221  inline void addItem(QListWidgetItem *item);
222  inline void addItems(const QStringList &labels) { insertItems(count(), labels); }
223  QListWidgetItem *takeItem(int row);
224  int count() const;
225 
226  QListWidgetItem *currentItem() const;
227  void setCurrentItem(QListWidgetItem *item);
228  void setCurrentItem(QListWidgetItem *item, QItemSelectionModel::SelectionFlags command);
229 
230  int currentRow() const;
231  void setCurrentRow(int row);
232  void setCurrentRow(int row, QItemSelectionModel::SelectionFlags command);
233 
234  QListWidgetItem *itemAt(const QPoint &p) const;
235  inline QListWidgetItem *itemAt(int x, int y) const;
236  QRect visualItemRect(const QListWidgetItem *item) const;
237 
238  void sortItems(Qt::SortOrder order = Qt::AscendingOrder);
239  void setSortingEnabled(bool enable);
240  bool isSortingEnabled() const;
241 
242  void editItem(QListWidgetItem *item);
245 
246  QWidget *itemWidget(QListWidgetItem *item) const;
247  void setItemWidget(QListWidgetItem *item, QWidget *widget);
248  inline void removeItemWidget(QListWidgetItem *item);
249 
250  bool isItemSelected(const QListWidgetItem *item) const;
251  void setItemSelected(const QListWidgetItem *item, bool select);
252  QList<QListWidgetItem*> selectedItems() const;
253  QList<QListWidgetItem*> findItems(const QString &text, Qt::MatchFlags flags) const;
254 
255  bool isItemHidden(const QListWidgetItem *item) const;
256  void setItemHidden(const QListWidgetItem *item, bool hide);
257  void dropEvent(QDropEvent *event);
258 
259 public Q_SLOTS:
260  void scrollToItem(const QListWidgetItem *item, QAbstractItemView::ScrollHint hint = EnsureVisible);
261  void clear();
262 
263 Q_SIGNALS:
264  void itemPressed(QListWidgetItem *item);
265  void itemClicked(QListWidgetItem *item);
266  void itemDoubleClicked(QListWidgetItem *item);
267  void itemActivated(QListWidgetItem *item);
268  void itemEntered(QListWidgetItem *item);
269  void itemChanged(QListWidgetItem *item);
270 
271  void currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous);
272  void currentTextChanged(const QString &currentText);
273  void currentRowChanged(int currentRow);
274 
275  void itemSelectionChanged();
276 
277 protected:
278  bool event(QEvent *e);
279  virtual QStringList mimeTypes() const;
280  virtual QMimeData *mimeData(const QList<QListWidgetItem*> items) const;
281 #ifndef QT_NO_DRAGANDDROP
282  virtual bool dropMimeData(int index, const QMimeData *data, Qt::DropAction action);
283  virtual Qt::DropActions supportedDropActions() const;
284 #endif
285  QList<QListWidgetItem*> items(const QMimeData *data) const;
286 
287  QModelIndex indexFromItem(QListWidgetItem *item) const;
288  QListWidgetItem *itemFromIndex(const QModelIndex &index) const;
289 
290 private:
291  void setModel(QAbstractItemModel *model);
292  Qt::SortOrder sortOrder() const;
293 
296 
297  Q_PRIVATE_SLOT(d_func(), void _q_emitItemPressed(const QModelIndex &index))
298  Q_PRIVATE_SLOT(d_func(), void _q_emitItemClicked(const QModelIndex &index))
299  Q_PRIVATE_SLOT(d_func(), void _q_emitItemDoubleClicked(const QModelIndex &index))
300  Q_PRIVATE_SLOT(d_func(), void _q_emitItemActivated(const QModelIndex &index))
301  Q_PRIVATE_SLOT(d_func(), void _q_emitItemEntered(const QModelIndex &index))
302  Q_PRIVATE_SLOT(d_func(), void _q_emitItemChanged(const QModelIndex &index))
303  Q_PRIVATE_SLOT(d_func(), void _q_emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &current))
304  Q_PRIVATE_SLOT(d_func(), void _q_sort())
305  Q_PRIVATE_SLOT(d_func(), void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight))
306 };
307 
309 { setItemWidget(aItem, 0); }
310 
312 { insertItem(count(), aitem); }
313 
314 inline QListWidgetItem *QListWidget::itemAt(int ax, int ay) const
315 { return itemAt(QPoint(ax, ay)); }
316 
317 inline void QListWidgetItem::setSelected(bool aselect)
318 { if (view) view->setItemSelected(this, aselect); }
319 
320 inline bool QListWidgetItem::isSelected() const
321 { return (view ? view->isItemSelected(this) : false); }
322 
323 inline void QListWidgetItem::setHidden(bool ahide)
324 { if (view) view->setItemHidden(this, ahide); }
325 
326 inline bool QListWidgetItem::isHidden() const
327 { return (view ? view->isItemHidden(this) : false); }
328 
329 #endif // QT_NO_LISTWIDGET
330 
332 
334 
335 #endif // QLISTWIDGET_H
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
CheckState
Definition: qnamespace.h:1607
void setFont(const QFont &)
Use the single-argument overload instead.
Definition: qwidget.cpp:4996
void openPersistentEditor(const QModelIndex &index)
Opens a persistent editor on the item at the given index.
int currentRow
the row of the current item.
Definition: qlistwidget.h:206
int type
Definition: qmetatype.cpp:239
ItemType
This enum describes the types that are used to describe list widget items.
Definition: qlistwidget.h:68
void setTextAlignment(int alignment)
Sets the list item&#39;s text alignment to alignment.
Definition: qlistwidget.h:119
int textAlignment() const
Returns the text alignment for the list item.
Definition: qlistwidget.h:117
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
EventRef event
QSize size() const
QPointer< QWidget > widget
#define QT_MODULE(x)
Definition: qglobal.h:2783
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
#define Q_GUI_EXPORT
Definition: qglobal.h:1450
void setStatusTip(const QString &statusTip)
Sets the status tip for the list item to the text specified by statusTip.
Definition: qlistwidget.h:179
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
QString whatsThis() const
Returns the list item&#39;s "What&#39;s This?" help text.
Definition: qlistwidget.h:108
static void clear(QVariant::Private *d)
Definition: qvariant.cpp:197
int select(int, fd_set *, fd_set *, fd_set *, struct timeval *)
QListWidgetItemPrivate * d
Definition: qlistwidget.h:169
void setSizeHint(const QSize &size)
Sets the size hint for the list item to be size.
Definition: qlistwidget.h:149
void removeItemWidget(QListWidgetItem *item)
Definition: qlistwidget.h:308
The QListWidget class provides an item-based list widget.
Definition: qlistwidget.h:202
#define Q_DISABLE_COPY(Class)
Disables the use of copy constructors and assignment operators for the given Class.
Definition: qglobal.h:2523
#define Q_PRIVATE_SLOT(d, signature)
Definition: qobjectdefs.h:73
void setIcon(const QIcon &icon)
Sets the icon for the list item to the given icon.
Definition: qlistwidget.h:176
QListWidgetItem * item(int row) const
Returns the item that occupies the given row in the list if one has been set; otherwise returns 0...
void setCheckState(Qt::CheckState state)
Sets the check state of the list item to state.
Definition: qlistwidget.h:144
void itemChanged(QListWidgetItem *item)
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
Reimplemented Function
QListWidgetItem * itemAt(const QPoint &p) const
Returns a pointer to the item at the coordinates p.
#define Q_SLOTS
Definition: qobjectdefs.h:71
void setWhatsThis(const QString &whatsThis)
Sets the "What&#39;s This?" help for the list item to the text specified by whatsThis.
Definition: qlistwidget.h:188
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
#define Q_SIGNALS
Definition: qobjectdefs.h:72
bool operator<(int priority, const QPair< QRunnable *, int > &p)
Definition: qthreadpool.cpp:50
QSize sizeHint() const
Returns the size hint set for the list item.
Definition: qlistwidget.h:147
QBrush background() const
Returns the brush used to display the list item&#39;s background.
Definition: qlistwidget.h:127
QFont font() const
Returns the font used to display this list item&#39;s text.
Definition: qlistwidget.h:113
QString toolTip() const
Returns the list item&#39;s tooltip.
Definition: qlistwidget.h:102
bool event(QEvent *e)
Reimplemented Function
Definition: qlistview.cpp:1693
bool isHidden() const
Returns true if the widget is hidden, otherwise returns false.
Definition: qwidget.h:1008
void setWhatsThis(const QString &)
Definition: qwidget.cpp:11655
virtual void setModel(QAbstractItemModel *model)
Sets the model for the view to present.
void setBackground(const QBrush &brush)
Sets the background brush of the list item to the given brush.
Definition: qlistwidget.h:129
void addItem(const QString &label)
Inserts an item with the text label at the end of the list widget.
Definition: qlistwidget.h:220
SortOrder
Definition: qnamespace.h:189
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QStringList mimeTypes() const
Returns a list of MIME types that can be used to describe a list of model indexes.
bool isHidden() const
Returns true if the item is hidden; otherwise returns false.
Definition: qlistwidget.h:326
QVector< void * > dummy
Definition: qlistwidget.h:167
static bool setData(const QByteArray &data, STGMEDIUM *pmedium)
Definition: qmime_win.cpp:141
void setFont(const QFont &font)
Sets the font used when painting the item to the given font.
Definition: qlistwidget.h:192
bool isSelected() const
Returns true if the item is selected; otherwise returns false.
Definition: qlistwidget.h:320
QList< QListWidgetItem * > items
void setStatusTip(const QString &)
Definition: qwidget.cpp:11629
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
void setHidden(bool hidden)
Convenience function, equivalent to setVisible(!hidden).
Definition: qwidget.h:495
QColor backgroundColor() const
This function is deprecated.
Definition: qlistwidget.h:122
static const char * data(const QByteArray &arr)
QString statusTip() const
DropAction
Definition: qnamespace.h:1597
Qt::DropActions supportedDropActions() const
Returns the drop actions supported by this model.
The QMimeData class provides a container for data that records information about its MIME type...
Definition: qmimedata.h:57
QString statusTip() const
Returns the list item&#39;s status tip.
Definition: qlistwidget.h:97
#define Q_OBJECT
Definition: qobjectdefs.h:157
void hide()
Hides the widget.
Definition: qwidget.h:501
The QListWidgetItem class provides an item for use with the QListWidget item view class...
Definition: qlistwidget.h:63
The QAbstractItemModel class provides the abstract interface for item model classes.
void setToolTip(const QString &)
Definition: qwidget.cpp:11600
Qt::CheckState checkState() const
Returns the checked state of the list item (see Qt::CheckState).
Definition: qlistwidget.h:142
The QBrush class defines the fill pattern of shapes drawn by QPainter.
Definition: qbrush.h:76
Qt::ItemFlags flags() const
Returns the item flags for this item (see Qt::ItemFlags).
Definition: qlistwidget.h:86
friend class QListWidgetItem
Definition: qlistwidget.h:209
QColor textColor() const
Returns the color used to display the list item&#39;s text.
Definition: qlistwidget.h:132
QString toolTip() const
The QDropEvent class provides an event which is sent when a drag and drop action is completed...
Definition: qevent.h:476
The QListView class provides a list or icon view onto a model.
Definition: qlistview.h:57
QListWidget * listWidget() const
Returns the list widget containing the item.
Definition: qlistwidget.h:78
void setHidden(bool hide)
Hides the item if hide is true; otherwise shows the item.
Definition: qlistwidget.h:323
The QFont class specifies a font used for drawing text.
Definition: qfont.h:64
Q_GUI_EXPORT QDataStream & operator<<(QDataStream &out, const QListWidgetItem &item)
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
void setSelected(bool select)
Sets the selected state of the item to select.
Definition: qlistwidget.h:317
The QModelIndex class is used to locate data in a data model.
void setForeground(const QBrush &brush)
Sets the foreground brush of the list item to the given brush.
Definition: qlistwidget.h:139
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
Qt::ItemFlags itemFlags
Definition: qlistwidget.h:170
T qvariant_cast(const QVariant &)
Definition: qvariant.h:571
State state() const
Returns the item view&#39;s state.
QListWidget * view
Definition: qlistwidget.h:168
QMimeData * mimeData(const QModelIndexList &indexes) const
Returns an object that contains serialized items of data corresponding to the list of indexes specifi...
quint16 index
Q_GUI_EXPORT QDataStream & operator>>(QDataStream &in, QListWidgetItem &item)
const QFont & font() const
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
QIcon icon() const
Returns the list item&#39;s icon.
Definition: qlistwidget.h:93
#define Q_DECLARE_PRIVATE(Class)
Definition: qglobal.h:2467
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
void closePersistentEditor(const QModelIndex &index)
Closes the persistent editor for the item at the given index.
void setToolTip(const QString &toolTip)
Sets the tooltip for the list item to the text specified by toolTip.
Definition: qlistwidget.h:183
int type() const
Returns the type passed to the QListWidgetItem constructor.
Definition: qlistwidget.h:163
Qt::ItemFlags flags(const QModelIndex &index) const
Returns the item flags for the given index.
void setTextColor(const QColor &color)
This function is deprecated.
Definition: qlistwidget.h:134
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
QString whatsThis() const
#define QT_END_HEADER
Definition: qglobal.h:137
QBrush foreground() const
Returns the brush used to display the list item&#39;s foreground (e.
Definition: qlistwidget.h:137
int count
the number of items in the list including any hidden items.
Definition: qlistwidget.h:205
void setText(const QString &text)
Sets the text for the list widget item&#39;s to the given text.
Definition: qlistwidget.h:173
QString text() const
Returns the list item&#39;s text.
Definition: qlistwidget.h:89
void addItems(const QStringList &labels)
Inserts items with the text labels at the end of the list widget.
Definition: qlistwidget.h:222
void dropEvent(QDropEvent *e)
Reimplemented Function
Definition: qlistview.cpp:917
#define text
Definition: qobjectdefs.h:80
virtual void setBackgroundColor(const QColor &color)
This function is deprecated.
Definition: qlistwidget.h:124
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
static void clone(QMetaObjectBuilder &builder, const QMetaObject *mo, const QMetaObject *ignoreStart, const QMetaObject *ignoreEnd)
bool sortingEnabled
whether sorting is enabled
Definition: qlistwidget.h:207
The QIcon class provides scalable icons in different modes and states.
Definition: qicon.h:60