|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtGui module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "qlistwidget.h" |
|
43 |
|
44 #ifndef QT_NO_LISTWIDGET |
|
45 #include <qitemdelegate.h> |
|
46 #include <private/qlistview_p.h> |
|
47 #include <private/qwidgetitemdata_p.h> |
|
48 #include <private/qlistwidget_p.h> |
|
49 |
|
50 QT_BEGIN_NAMESPACE |
|
51 |
|
52 // workaround for VC++ 6.0 linker bug (?) |
|
53 typedef bool(*LessThan)(const QPair<QListWidgetItem*,int>&,const QPair<QListWidgetItem*,int>&); |
|
54 |
|
55 class QListWidgetMimeData : public QMimeData |
|
56 { |
|
57 Q_OBJECT |
|
58 public: |
|
59 QList<QListWidgetItem*> items; |
|
60 }; |
|
61 |
|
62 QT_BEGIN_INCLUDE_NAMESPACE |
|
63 #include "qlistwidget.moc" |
|
64 QT_END_INCLUDE_NAMESPACE |
|
65 |
|
66 QListModel::QListModel(QListWidget *parent) |
|
67 : QAbstractListModel(parent) |
|
68 { |
|
69 } |
|
70 |
|
71 QListModel::~QListModel() |
|
72 { |
|
73 clear(); |
|
74 } |
|
75 |
|
76 void QListModel::clear() |
|
77 { |
|
78 for (int i = 0; i < items.count(); ++i) { |
|
79 if (items.at(i)) { |
|
80 items.at(i)->d->theid = -1; |
|
81 items.at(i)->view = 0; |
|
82 delete items.at(i); |
|
83 } |
|
84 } |
|
85 items.clear(); |
|
86 reset(); |
|
87 } |
|
88 |
|
89 QListWidgetItem *QListModel::at(int row) const |
|
90 { |
|
91 return items.value(row); |
|
92 } |
|
93 |
|
94 void QListModel::remove(QListWidgetItem *item) |
|
95 { |
|
96 if (!item) |
|
97 return; |
|
98 int row = items.indexOf(item); // ### use index(item) - it's faster |
|
99 Q_ASSERT(row != -1); |
|
100 beginRemoveRows(QModelIndex(), row, row); |
|
101 items.at(row)->d->theid = -1; |
|
102 items.at(row)->view = 0; |
|
103 items.removeAt(row); |
|
104 endRemoveRows(); |
|
105 } |
|
106 |
|
107 void QListModel::insert(int row, QListWidgetItem *item) |
|
108 { |
|
109 if (!item) |
|
110 return; |
|
111 |
|
112 item->view = qobject_cast<QListWidget*>(QObject::parent()); |
|
113 if (item->view && item->view->isSortingEnabled()) { |
|
114 // sorted insertion |
|
115 QList<QListWidgetItem*>::iterator it; |
|
116 it = sortedInsertionIterator(items.begin(), items.end(), |
|
117 item->view->sortOrder(), item); |
|
118 row = qMax(it - items.begin(), 0); |
|
119 } else { |
|
120 if (row < 0) |
|
121 row = 0; |
|
122 else if (row > items.count()) |
|
123 row = items.count(); |
|
124 } |
|
125 beginInsertRows(QModelIndex(), row, row); |
|
126 items.insert(row, item); |
|
127 item->d->theid = row; |
|
128 endInsertRows(); |
|
129 } |
|
130 |
|
131 void QListModel::insert(int row, const QStringList &labels) |
|
132 { |
|
133 const int count = labels.count(); |
|
134 if (count <= 0) |
|
135 return; |
|
136 QListWidget *view = qobject_cast<QListWidget*>(QObject::parent()); |
|
137 if (view && view->isSortingEnabled()) { |
|
138 // sorted insertion |
|
139 for (int i = 0; i < count; ++i) { |
|
140 QListWidgetItem *item = new QListWidgetItem(labels.at(i)); |
|
141 insert(row, item); |
|
142 } |
|
143 } else { |
|
144 if (row < 0) |
|
145 row = 0; |
|
146 else if (row > items.count()) |
|
147 row = items.count(); |
|
148 beginInsertRows(QModelIndex(), row, row + count - 1); |
|
149 for (int i = 0; i < count; ++i) { |
|
150 QListWidgetItem *item = new QListWidgetItem(labels.at(i)); |
|
151 item->d->theid = row; |
|
152 item->view = qobject_cast<QListWidget*>(QObject::parent()); |
|
153 items.insert(row++, item); |
|
154 } |
|
155 endInsertRows(); |
|
156 } |
|
157 } |
|
158 |
|
159 QListWidgetItem *QListModel::take(int row) |
|
160 { |
|
161 if (row < 0 || row >= items.count()) |
|
162 return 0; |
|
163 |
|
164 beginRemoveRows(QModelIndex(), row, row); |
|
165 items.at(row)->d->theid = -1; |
|
166 items.at(row)->view = 0; |
|
167 QListWidgetItem *item = items.takeAt(row); |
|
168 endRemoveRows(); |
|
169 return item; |
|
170 } |
|
171 |
|
172 int QListModel::rowCount(const QModelIndex &parent) const |
|
173 { |
|
174 return parent.isValid() ? 0 : items.count(); |
|
175 } |
|
176 |
|
177 QModelIndex QListModel::index(QListWidgetItem *item) const |
|
178 { |
|
179 if (!item || !item->view || static_cast<const QListModel *>(item->view->model()) != this |
|
180 || items.isEmpty()) |
|
181 return QModelIndex(); |
|
182 int row; |
|
183 const int theid = item->d->theid; |
|
184 if (theid >= 0 && theid < items.count() && items.at(theid) == item) { |
|
185 row = theid; |
|
186 } else { // we need to search for the item |
|
187 row = items.lastIndexOf(item); // lastIndexOf is an optimization in favor of indexOf |
|
188 if (row == -1) // not found |
|
189 return QModelIndex(); |
|
190 item->d->theid = row; |
|
191 } |
|
192 return createIndex(row, 0, item); |
|
193 } |
|
194 |
|
195 QModelIndex QListModel::index(int row, int column, const QModelIndex &parent) const |
|
196 { |
|
197 if (hasIndex(row, column, parent)) |
|
198 return createIndex(row, column, items.at(row)); |
|
199 return QModelIndex(); |
|
200 } |
|
201 |
|
202 QVariant QListModel::data(const QModelIndex &index, int role) const |
|
203 { |
|
204 if (!index.isValid() || index.row() >= items.count()) |
|
205 return QVariant(); |
|
206 return items.at(index.row())->data(role); |
|
207 } |
|
208 |
|
209 bool QListModel::setData(const QModelIndex &index, const QVariant &value, int role) |
|
210 { |
|
211 if (!index.isValid() || index.row() >= items.count()) |
|
212 return false; |
|
213 items.at(index.row())->setData(role, value); |
|
214 return true; |
|
215 } |
|
216 |
|
217 QMap<int, QVariant> QListModel::itemData(const QModelIndex &index) const |
|
218 { |
|
219 QMap<int, QVariant> roles; |
|
220 if (!index.isValid() || index.row() >= items.count()) |
|
221 return roles; |
|
222 QListWidgetItem *itm = items.at(index.row()); |
|
223 for (int i = 0; i < itm->d->values.count(); ++i) { |
|
224 roles.insert(itm->d->values.at(i).role, |
|
225 itm->d->values.at(i).value); |
|
226 } |
|
227 return roles; |
|
228 } |
|
229 |
|
230 bool QListModel::insertRows(int row, int count, const QModelIndex &parent) |
|
231 { |
|
232 if (count < 1 || row < 0 || row > rowCount() || parent.isValid()) |
|
233 return false; |
|
234 |
|
235 beginInsertRows(QModelIndex(), row, row + count - 1); |
|
236 QListWidget *view = qobject_cast<QListWidget*>(QObject::parent()); |
|
237 QListWidgetItem *itm = 0; |
|
238 |
|
239 for (int r = row; r < row + count; ++r) { |
|
240 itm = new QListWidgetItem; |
|
241 itm->view = view; |
|
242 itm->d->theid = r; |
|
243 items.insert(r, itm); |
|
244 } |
|
245 |
|
246 endInsertRows(); |
|
247 return true; |
|
248 } |
|
249 |
|
250 bool QListModel::removeRows(int row, int count, const QModelIndex &parent) |
|
251 { |
|
252 if (count < 1 || row < 0 || (row + count) > rowCount() || parent.isValid()) |
|
253 return false; |
|
254 |
|
255 beginRemoveRows(QModelIndex(), row, row + count - 1); |
|
256 QListWidgetItem *itm = 0; |
|
257 for (int r = row; r < row + count; ++r) { |
|
258 itm = items.takeAt(row); |
|
259 itm->view = 0; |
|
260 itm->d->theid = -1; |
|
261 delete itm; |
|
262 } |
|
263 endRemoveRows(); |
|
264 return true; |
|
265 } |
|
266 |
|
267 Qt::ItemFlags QListModel::flags(const QModelIndex &index) const |
|
268 { |
|
269 if (!index.isValid() || index.row() >= items.count() || index.model() != this) |
|
270 return Qt::ItemIsDropEnabled; // we allow drops outside the items |
|
271 return items.at(index.row())->flags(); |
|
272 } |
|
273 |
|
274 void QListModel::sort(int column, Qt::SortOrder order) |
|
275 { |
|
276 if (column != 0) |
|
277 return; |
|
278 |
|
279 emit layoutAboutToBeChanged(); |
|
280 |
|
281 QVector < QPair<QListWidgetItem*,int> > sorting(items.count()); |
|
282 for (int i = 0; i < items.count(); ++i) { |
|
283 QListWidgetItem *item = items.at(i); |
|
284 sorting[i].first = item; |
|
285 sorting[i].second = i; |
|
286 } |
|
287 |
|
288 LessThan compare = (order == Qt::AscendingOrder ? &itemLessThan : &itemGreaterThan); |
|
289 qSort(sorting.begin(), sorting.end(), compare); |
|
290 QModelIndexList fromIndexes; |
|
291 QModelIndexList toIndexes; |
|
292 for (int r = 0; r < sorting.count(); ++r) { |
|
293 QListWidgetItem *item = sorting.at(r).first; |
|
294 toIndexes.append(createIndex(r, 0, item)); |
|
295 fromIndexes.append(createIndex(sorting.at(r).second, 0, sorting.at(r).first)); |
|
296 items[r] = sorting.at(r).first; |
|
297 } |
|
298 changePersistentIndexList(fromIndexes, toIndexes); |
|
299 |
|
300 emit layoutChanged(); |
|
301 } |
|
302 |
|
303 /** |
|
304 * This function assumes that all items in the model except the items that are between |
|
305 * (inclusive) start and end are sorted. |
|
306 * With these assumptions, this function can ensure that the model is sorted in a |
|
307 * much more efficient way than doing a naive 'sort everything'. |
|
308 * (provided that the range is relatively small compared to the total number of items) |
|
309 */ |
|
310 void QListModel::ensureSorted(int column, Qt::SortOrder order, int start, int end) |
|
311 { |
|
312 if (column != 0) |
|
313 return; |
|
314 |
|
315 int count = end - start + 1; |
|
316 QVector < QPair<QListWidgetItem*,int> > sorting(count); |
|
317 for (int i = 0; i < count; ++i) { |
|
318 sorting[i].first = items.at(start + i); |
|
319 sorting[i].second = start + i; |
|
320 } |
|
321 |
|
322 LessThan compare = (order == Qt::AscendingOrder ? &itemLessThan : &itemGreaterThan); |
|
323 qSort(sorting.begin(), sorting.end(), compare); |
|
324 |
|
325 QModelIndexList oldPersistentIndexes = persistentIndexList(); |
|
326 QModelIndexList newPersistentIndexes = oldPersistentIndexes; |
|
327 QList<QListWidgetItem*> tmp = items; |
|
328 QList<QListWidgetItem*>::iterator lit = tmp.begin(); |
|
329 bool changed = false; |
|
330 for (int i = 0; i < count; ++i) { |
|
331 int oldRow = sorting.at(i).second; |
|
332 QListWidgetItem *item = tmp.takeAt(oldRow); |
|
333 lit = sortedInsertionIterator(lit, tmp.end(), order, item); |
|
334 int newRow = qMax(lit - tmp.begin(), 0); |
|
335 lit = tmp.insert(lit, item); |
|
336 if (newRow != oldRow) { |
|
337 changed = true; |
|
338 for (int j = i + 1; j < count; ++j) { |
|
339 int otherRow = sorting.at(j).second; |
|
340 if (oldRow < otherRow && newRow >= otherRow) |
|
341 --sorting[j].second; |
|
342 else if (oldRow > otherRow && newRow <= otherRow) |
|
343 ++sorting[j].second; |
|
344 } |
|
345 for (int k = 0; k < newPersistentIndexes.count(); ++k) { |
|
346 QModelIndex pi = newPersistentIndexes.at(k); |
|
347 int oldPersistentRow = pi.row(); |
|
348 int newPersistentRow = oldPersistentRow; |
|
349 if (oldPersistentRow == oldRow) |
|
350 newPersistentRow = newRow; |
|
351 else if (oldRow < oldPersistentRow && newRow >= oldPersistentRow) |
|
352 newPersistentRow = oldPersistentRow - 1; |
|
353 else if (oldRow > oldPersistentRow && newRow <= oldPersistentRow) |
|
354 newPersistentRow = oldPersistentRow + 1; |
|
355 if (newPersistentRow != oldPersistentRow) |
|
356 newPersistentIndexes[k] = createIndex(newPersistentRow, |
|
357 pi.column(), pi.internalPointer()); |
|
358 } |
|
359 } |
|
360 } |
|
361 |
|
362 if (changed) { |
|
363 emit layoutAboutToBeChanged(); |
|
364 items = tmp; |
|
365 changePersistentIndexList(oldPersistentIndexes, newPersistentIndexes); |
|
366 emit layoutChanged(); |
|
367 } |
|
368 } |
|
369 |
|
370 bool QListModel::itemLessThan(const QPair<QListWidgetItem*,int> &left, |
|
371 const QPair<QListWidgetItem*,int> &right) |
|
372 { |
|
373 return (*left.first) < (*right.first); |
|
374 } |
|
375 |
|
376 bool QListModel::itemGreaterThan(const QPair<QListWidgetItem*,int> &left, |
|
377 const QPair<QListWidgetItem*,int> &right) |
|
378 { |
|
379 return (*right.first) < (*left.first); |
|
380 } |
|
381 |
|
382 QList<QListWidgetItem*>::iterator QListModel::sortedInsertionIterator( |
|
383 const QList<QListWidgetItem*>::iterator &begin, |
|
384 const QList<QListWidgetItem*>::iterator &end, |
|
385 Qt::SortOrder order, QListWidgetItem *item) |
|
386 { |
|
387 if (order == Qt::AscendingOrder) |
|
388 return qLowerBound(begin, end, item, QListModelLessThan()); |
|
389 return qLowerBound(begin, end, item, QListModelGreaterThan()); |
|
390 } |
|
391 |
|
392 void QListModel::itemChanged(QListWidgetItem *item) |
|
393 { |
|
394 QModelIndex idx = index(item); |
|
395 emit dataChanged(idx, idx); |
|
396 } |
|
397 |
|
398 QStringList QListModel::mimeTypes() const |
|
399 { |
|
400 const QListWidget *view = qobject_cast<const QListWidget*>(QObject::parent()); |
|
401 return view->mimeTypes(); |
|
402 } |
|
403 |
|
404 QMimeData *QListModel::internalMimeData() const |
|
405 { |
|
406 return QAbstractItemModel::mimeData(cachedIndexes); |
|
407 } |
|
408 |
|
409 QMimeData *QListModel::mimeData(const QModelIndexList &indexes) const |
|
410 { |
|
411 QList<QListWidgetItem*> itemlist; |
|
412 for (int i = 0; i < indexes.count(); ++i) |
|
413 itemlist << at(indexes.at(i).row()); |
|
414 const QListWidget *view = qobject_cast<const QListWidget*>(QObject::parent()); |
|
415 |
|
416 cachedIndexes = indexes; |
|
417 QMimeData *mimeData = view->mimeData(itemlist); |
|
418 cachedIndexes.clear(); |
|
419 return mimeData; |
|
420 } |
|
421 |
|
422 #ifndef QT_NO_DRAGANDDROP |
|
423 bool QListModel::dropMimeData(const QMimeData *data, Qt::DropAction action, |
|
424 int row, int column, const QModelIndex &index) |
|
425 { |
|
426 Q_UNUSED(column); |
|
427 QListWidget *view = qobject_cast<QListWidget*>(QObject::parent()); |
|
428 if (index.isValid()) |
|
429 row = index.row(); |
|
430 else if (row == -1) |
|
431 row = items.count(); |
|
432 |
|
433 return view->dropMimeData(row, data, action); |
|
434 } |
|
435 |
|
436 Qt::DropActions QListModel::supportedDropActions() const |
|
437 { |
|
438 const QListWidget *view = qobject_cast<const QListWidget*>(QObject::parent()); |
|
439 return view->supportedDropActions(); |
|
440 } |
|
441 #endif // QT_NO_DRAGANDDROP |
|
442 |
|
443 /*! |
|
444 \class QListWidgetItem |
|
445 \brief The QListWidgetItem class provides an item for use with the |
|
446 QListWidget item view class. |
|
447 |
|
448 \ingroup model-view |
|
449 |
|
450 A QListWidgetItem represents a single item in a QListWidget. Each item can |
|
451 hold several pieces of information, and will display them appropriately. |
|
452 |
|
453 The item view convenience classes use a classic item-based interface rather |
|
454 than a pure model/view approach. For a more flexible list view widget, |
|
455 consider using the QListView class with a standard model. |
|
456 |
|
457 List items can be inserted automatically into a list, when they are |
|
458 constructed, by specifying the list widget: |
|
459 |
|
460 \snippet doc/src/snippets/qlistwidget-using/mainwindow.cpp 2 |
|
461 |
|
462 Alternatively, list items can also be created without a parent widget, and |
|
463 later inserted into a list using QListWidget::insertItem(). |
|
464 |
|
465 List items are typically used to display text() and an icon(). These are |
|
466 set with the setText() and setIcon() functions. The appearance of the text |
|
467 can be customized with setFont(), setForeground(), and setBackground(). |
|
468 Text in list items can be aligned using the setTextAlignment() function. |
|
469 Tooltips, status tips and "What's This?" help can be added to list items |
|
470 with setToolTip(), setStatusTip(), and setWhatsThis(). |
|
471 |
|
472 By default, items are enabled, selectable, checkable, and can be the source |
|
473 of drag and drop operations. |
|
474 |
|
475 Each item's flags can be changed by calling setFlags() with the appropriate |
|
476 value (see Qt::ItemFlags). Checkable items can be checked, unchecked and |
|
477 partially checked with the setCheckState() function. The corresponding |
|
478 checkState() function indicates the item's current check state. |
|
479 |
|
480 The isHidden() function can be used to determine whether the item is |
|
481 hidden. To hide an item, use setHidden(). |
|
482 |
|
483 |
|
484 \section1 Subclassing |
|
485 |
|
486 When subclassing QListWidgetItem to provide custom items, it is possible to |
|
487 define new types for them enabling them to be distinguished from standard |
|
488 items. For subclasses that require this feature, ensure that you call the |
|
489 base class constructor with a new type value equal to or greater than |
|
490 \l UserType, within \e your constructor. |
|
491 |
|
492 \sa QListWidget, {Model/View Programming}, QTreeWidgetItem, QTableWidgetItem |
|
493 */ |
|
494 |
|
495 /*! |
|
496 \enum QListWidgetItem::ItemType |
|
497 |
|
498 This enum describes the types that are used to describe list widget items. |
|
499 |
|
500 \value Type The default type for list widget items. |
|
501 \value UserType The minimum value for custom types. Values below UserType are |
|
502 reserved by Qt. |
|
503 |
|
504 You can define new user types in QListWidgetItem subclasses to ensure that |
|
505 custom items are treated specially. |
|
506 |
|
507 \sa type() |
|
508 */ |
|
509 |
|
510 /*! |
|
511 \fn int QListWidgetItem::type() const |
|
512 |
|
513 Returns the type passed to the QListWidgetItem constructor. |
|
514 */ |
|
515 |
|
516 /*! |
|
517 \fn QListWidget *QListWidgetItem::listWidget() const |
|
518 |
|
519 Returns the list widget containing the item. |
|
520 */ |
|
521 |
|
522 /*! |
|
523 \fn void QListWidgetItem::setSelected(bool select) |
|
524 \since 4.2 |
|
525 |
|
526 Sets the selected state of the item to \a select. |
|
527 |
|
528 \sa isSelected() |
|
529 */ |
|
530 |
|
531 /*! |
|
532 \fn bool QListWidgetItem::isSelected() const |
|
533 \since 4.2 |
|
534 |
|
535 Returns true if the item is selected; otherwise returns false. |
|
536 |
|
537 \sa setSelected() |
|
538 */ |
|
539 |
|
540 /*! |
|
541 \fn void QListWidgetItem::setHidden(bool hide) |
|
542 \since 4.2 |
|
543 |
|
544 Hides the item if \a hide is true; otherwise shows the item. |
|
545 |
|
546 \sa isHidden() |
|
547 */ |
|
548 |
|
549 /*! |
|
550 \fn bool QListWidgetItem::isHidden() const |
|
551 \since 4.2 |
|
552 |
|
553 Returns true if the item is hidden; otherwise returns false. |
|
554 |
|
555 \sa setHidden() |
|
556 */ |
|
557 |
|
558 /*! |
|
559 \fn QListWidgetItem::QListWidgetItem(QListWidget *parent, int type) |
|
560 |
|
561 Constructs an empty list widget item of the specified \a type with the |
|
562 given \a parent. If \a parent is not specified, the item will need to be |
|
563 inserted into a list widget with QListWidget::insertItem(). |
|
564 |
|
565 This constructor inserts the item into the model of the parent that is |
|
566 passed to the constructor. If the model is sorted then the behavior of the |
|
567 insert is undetermined since the model will call the \c '<' operator method |
|
568 on the item which, at this point, is not yet constructed. To avoid the |
|
569 undetermined behavior, we recommend not to specify the parent and use |
|
570 QListWidget::insertItem() instead. |
|
571 |
|
572 \sa type() |
|
573 */ |
|
574 QListWidgetItem::QListWidgetItem(QListWidget *view, int type) |
|
575 : rtti(type), view(view), d(new QListWidgetItemPrivate(this)), |
|
576 itemFlags(Qt::ItemIsSelectable |
|
577 |Qt::ItemIsUserCheckable |
|
578 |Qt::ItemIsEnabled |
|
579 |Qt::ItemIsDragEnabled) |
|
580 { |
|
581 if (QListModel *model = (view ? qobject_cast<QListModel*>(view->model()) : 0)) |
|
582 model->insert(model->rowCount(), this); |
|
583 } |
|
584 |
|
585 /*! |
|
586 \fn QListWidgetItem::QListWidgetItem(const QString &text, QListWidget *parent, int type) |
|
587 |
|
588 Constructs an empty list widget item of the specified \a type with the |
|
589 given \a text and \a parent. If the parent is not specified, the item will |
|
590 need to be inserted into a list widget with QListWidget::insertItem(). |
|
591 |
|
592 This constructor inserts the item into the model of the parent that is |
|
593 passed to the constructor. If the model is sorted then the behavior of the |
|
594 insert is undetermined since the model will call the \c '<' operator method |
|
595 on the item which, at this point, is not yet constructed. To avoid the |
|
596 undetermined behavior, we recommend not to specify the parent and use |
|
597 QListWidget::insertItem() instead. |
|
598 |
|
599 \sa type() |
|
600 */ |
|
601 QListWidgetItem::QListWidgetItem(const QString &text, QListWidget *view, int type) |
|
602 : rtti(type), view(0), d(new QListWidgetItemPrivate(this)), |
|
603 itemFlags(Qt::ItemIsSelectable |
|
604 |Qt::ItemIsUserCheckable |
|
605 |Qt::ItemIsEnabled |
|
606 |Qt::ItemIsDragEnabled) |
|
607 { |
|
608 setData(Qt::DisplayRole, text); |
|
609 this->view = view; |
|
610 if (QListModel *model = (view ? qobject_cast<QListModel*>(view->model()) : 0)) |
|
611 model->insert(model->rowCount(), this); |
|
612 } |
|
613 |
|
614 /*! |
|
615 \fn QListWidgetItem::QListWidgetItem(const QIcon &icon, const QString &text, QListWidget *parent, int type) |
|
616 |
|
617 Constructs an empty list widget item of the specified \a type with the |
|
618 given \a icon, \a text and \a parent. If the parent is not specified, the |
|
619 item will need to be inserted into a list widget with |
|
620 QListWidget::insertItem(). |
|
621 |
|
622 This constructor inserts the item into the model of the parent that is |
|
623 passed to the constructor. If the model is sorted then the behavior of the |
|
624 insert is undetermined since the model will call the \c '<' operator method |
|
625 on the item which, at this point, is not yet constructed. To avoid the |
|
626 undetermined behavior, we recommend not to specify the parent and use |
|
627 QListWidget::insertItem() instead. |
|
628 |
|
629 \sa type() |
|
630 */ |
|
631 QListWidgetItem::QListWidgetItem(const QIcon &icon,const QString &text, |
|
632 QListWidget *view, int type) |
|
633 : rtti(type), view(0), d(new QListWidgetItemPrivate(this)), |
|
634 itemFlags(Qt::ItemIsSelectable |
|
635 |Qt::ItemIsUserCheckable |
|
636 |Qt::ItemIsEnabled |
|
637 |Qt::ItemIsDragEnabled) |
|
638 { |
|
639 setData(Qt::DisplayRole, text); |
|
640 setData(Qt::DecorationRole, icon); |
|
641 this->view = view; |
|
642 if (QListModel *model = (view ? qobject_cast<QListModel*>(view->model()) : 0)) |
|
643 model->insert(model->rowCount(), this); |
|
644 } |
|
645 |
|
646 /*! |
|
647 Destroys the list item. |
|
648 */ |
|
649 QListWidgetItem::~QListWidgetItem() |
|
650 { |
|
651 if (QListModel *model = (view ? qobject_cast<QListModel*>(view->model()) : 0)) |
|
652 model->remove(this); |
|
653 delete d; |
|
654 } |
|
655 |
|
656 /*! |
|
657 Creates an exact copy of the item. |
|
658 */ |
|
659 QListWidgetItem *QListWidgetItem::clone() const |
|
660 { |
|
661 return new QListWidgetItem(*this); |
|
662 } |
|
663 |
|
664 /*! |
|
665 Sets the data for a given \a role to the given \a value. Reimplement this |
|
666 function if you need extra roles or special behavior for certain roles. |
|
667 |
|
668 \sa Qt::ItemDataRole, data() |
|
669 */ |
|
670 void QListWidgetItem::setData(int role, const QVariant &value) |
|
671 { |
|
672 bool found = false; |
|
673 role = (role == Qt::EditRole ? Qt::DisplayRole : role); |
|
674 for (int i = 0; i < d->values.count(); ++i) { |
|
675 if (d->values.at(i).role == role) { |
|
676 if (d->values.at(i).value == value) |
|
677 return; |
|
678 d->values[i].value = value; |
|
679 found = true; |
|
680 break; |
|
681 } |
|
682 } |
|
683 if (!found) |
|
684 d->values.append(QWidgetItemData(role, value)); |
|
685 if (QListModel *model = (view ? qobject_cast<QListModel*>(view->model()) : 0)) |
|
686 model->itemChanged(this); |
|
687 } |
|
688 |
|
689 /*! |
|
690 Returns the item's data for a given \a role. Reimplement this function if |
|
691 you need extra roles or special behavior for certain roles. |
|
692 |
|
693 \sa Qt::ItemDataRole, setData() |
|
694 */ |
|
695 QVariant QListWidgetItem::data(int role) const |
|
696 { |
|
697 role = (role == Qt::EditRole ? Qt::DisplayRole : role); |
|
698 for (int i = 0; i < d->values.count(); ++i) |
|
699 if (d->values.at(i).role == role) |
|
700 return d->values.at(i).value; |
|
701 return QVariant(); |
|
702 } |
|
703 |
|
704 /*! |
|
705 Returns true if this item's text is less then \a other item's text; |
|
706 otherwise returns false. |
|
707 */ |
|
708 bool QListWidgetItem::operator<(const QListWidgetItem &other) const |
|
709 { |
|
710 const QVariant v1 = data(Qt::DisplayRole), v2 = other.data(Qt::DisplayRole); |
|
711 return QAbstractItemModelPrivate::variantLessThan(v1, v2); |
|
712 } |
|
713 |
|
714 #ifndef QT_NO_DATASTREAM |
|
715 |
|
716 /*! |
|
717 Reads the item from stream \a in. |
|
718 |
|
719 \sa write() |
|
720 */ |
|
721 void QListWidgetItem::read(QDataStream &in) |
|
722 { |
|
723 in >> d->values; |
|
724 } |
|
725 |
|
726 /*! |
|
727 Writes the item to stream \a out. |
|
728 |
|
729 \sa read() |
|
730 */ |
|
731 void QListWidgetItem::write(QDataStream &out) const |
|
732 { |
|
733 out << d->values; |
|
734 } |
|
735 #endif // QT_NO_DATASTREAM |
|
736 |
|
737 /*! |
|
738 \since 4.1 |
|
739 |
|
740 Constructs a copy of \a other. Note that type() and listWidget() are not |
|
741 copied. |
|
742 |
|
743 This function is useful when reimplementing clone(). |
|
744 |
|
745 \sa data(), flags() |
|
746 */ |
|
747 QListWidgetItem::QListWidgetItem(const QListWidgetItem &other) |
|
748 : rtti(Type), view(0), |
|
749 d(new QListWidgetItemPrivate(this)), |
|
750 itemFlags(other.itemFlags) |
|
751 { |
|
752 d->values = other.d->values; |
|
753 } |
|
754 |
|
755 /*! |
|
756 Assigns \a other's data and flags to this item. Note that type() and |
|
757 listWidget() are not copied. |
|
758 |
|
759 This function is useful when reimplementing clone(). |
|
760 |
|
761 \sa data(), flags() |
|
762 */ |
|
763 QListWidgetItem &QListWidgetItem::operator=(const QListWidgetItem &other) |
|
764 { |
|
765 d->values = other.d->values; |
|
766 itemFlags = other.itemFlags; |
|
767 return *this; |
|
768 } |
|
769 |
|
770 #ifndef QT_NO_DATASTREAM |
|
771 |
|
772 /*! |
|
773 \relates QListWidgetItem |
|
774 |
|
775 Writes the list widget item \a item to stream \a out. |
|
776 |
|
777 This operator uses QListWidgetItem::write(). |
|
778 |
|
779 \sa {Format of the QDataStream Operators} |
|
780 */ |
|
781 QDataStream &operator<<(QDataStream &out, const QListWidgetItem &item) |
|
782 { |
|
783 item.write(out); |
|
784 return out; |
|
785 } |
|
786 |
|
787 /*! |
|
788 \relates QListWidgetItem |
|
789 |
|
790 Reads a list widget item from stream \a in into \a item. |
|
791 |
|
792 This operator uses QListWidgetItem::read(). |
|
793 |
|
794 \sa {Format of the QDataStream Operators} |
|
795 */ |
|
796 QDataStream &operator>>(QDataStream &in, QListWidgetItem &item) |
|
797 { |
|
798 item.read(in); |
|
799 return in; |
|
800 } |
|
801 |
|
802 #endif // QT_NO_DATASTREAM |
|
803 |
|
804 /*! |
|
805 \fn Qt::ItemFlags QListWidgetItem::flags() const |
|
806 |
|
807 Returns the item flags for this item (see \l{Qt::ItemFlags}). |
|
808 */ |
|
809 |
|
810 /*! |
|
811 \fn QString QListWidgetItem::text() const |
|
812 |
|
813 Returns the list item's text. |
|
814 |
|
815 \sa setText() |
|
816 */ |
|
817 |
|
818 /*! |
|
819 \fn QIcon QListWidgetItem::icon() const |
|
820 |
|
821 Returns the list item's icon. |
|
822 |
|
823 \sa setIcon(), {QAbstractItemView::iconSize}{iconSize} |
|
824 */ |
|
825 |
|
826 /*! |
|
827 \fn QString QListWidgetItem::statusTip() const |
|
828 |
|
829 Returns the list item's status tip. |
|
830 |
|
831 \sa setStatusTip() |
|
832 */ |
|
833 |
|
834 /*! |
|
835 \fn QString QListWidgetItem::toolTip() const |
|
836 |
|
837 Returns the list item's tooltip. |
|
838 |
|
839 \sa setToolTip() statusTip() whatsThis() |
|
840 */ |
|
841 |
|
842 /*! |
|
843 \fn QString QListWidgetItem::whatsThis() const |
|
844 |
|
845 Returns the list item's "What's This?" help text. |
|
846 |
|
847 \sa setWhatsThis() statusTip() toolTip() |
|
848 */ |
|
849 |
|
850 /*! |
|
851 \fn QFont QListWidgetItem::font() const |
|
852 |
|
853 Returns the font used to display this list item's text. |
|
854 */ |
|
855 |
|
856 /*! |
|
857 \fn int QListWidgetItem::textAlignment() const |
|
858 |
|
859 Returns the text alignment for the list item. |
|
860 |
|
861 \sa Qt::AlignmentFlag |
|
862 */ |
|
863 |
|
864 /*! |
|
865 \fn QColor QListWidgetItem::backgroundColor() const |
|
866 \obsolete |
|
867 |
|
868 This function is deprecated. Use background() instead. |
|
869 */ |
|
870 |
|
871 /*! |
|
872 \fn QBrush QListWidgetItem::background() const |
|
873 \since 4.2 |
|
874 |
|
875 Returns the brush used to display the list item's background. |
|
876 |
|
877 \sa setBackground() foreground() |
|
878 */ |
|
879 |
|
880 /*! |
|
881 \fn QColor QListWidgetItem::textColor() const |
|
882 \obsolete |
|
883 |
|
884 Returns the color used to display the list item's text. |
|
885 |
|
886 This function is deprecated. Use foreground() instead. |
|
887 */ |
|
888 |
|
889 /*! |
|
890 \fn QBrush QListWidgetItem::foreground() const |
|
891 \since 4.2 |
|
892 |
|
893 Returns the brush used to display the list item's foreground (e.g. text). |
|
894 |
|
895 \sa setForeground() background() |
|
896 */ |
|
897 |
|
898 /*! |
|
899 \fn Qt::CheckState QListWidgetItem::checkState() const |
|
900 |
|
901 Returns the checked state of the list item (see \l{Qt::CheckState}). |
|
902 |
|
903 \sa flags() |
|
904 */ |
|
905 |
|
906 /*! |
|
907 \fn QSize QListWidgetItem::sizeHint() const |
|
908 \since 4.1 |
|
909 |
|
910 Returns the size hint set for the list item. |
|
911 */ |
|
912 |
|
913 /*! |
|
914 \fn void QListWidgetItem::setSizeHint(const QSize &size) |
|
915 \since 4.1 |
|
916 |
|
917 Sets the size hint for the list item to be \a size. If no size hint is set, |
|
918 the item delegate will compute the size hint based on the item data. |
|
919 */ |
|
920 |
|
921 /*! |
|
922 \fn void QListWidgetItem::setFlags(Qt::ItemFlags flags) |
|
923 |
|
924 Sets the item flags for the list item to \a flags. |
|
925 |
|
926 \sa Qt::ItemFlags |
|
927 */ |
|
928 void QListWidgetItem::setFlags(Qt::ItemFlags aflags) { |
|
929 itemFlags = aflags; |
|
930 if (QListModel *model = (view ? qobject_cast<QListModel*>(view->model()) : 0)) |
|
931 model->itemChanged(this); |
|
932 } |
|
933 |
|
934 |
|
935 /*! |
|
936 \fn void QListWidgetItem::setText(const QString &text) |
|
937 |
|
938 Sets the text for the list widget item's to the given \a text. |
|
939 |
|
940 \sa text() |
|
941 */ |
|
942 |
|
943 /*! |
|
944 \fn void QListWidgetItem::setIcon(const QIcon &icon) |
|
945 |
|
946 Sets the icon for the list item to the given \a icon. |
|
947 |
|
948 \sa icon(), text(), {QAbstractItemView::iconSize}{iconSize} |
|
949 */ |
|
950 |
|
951 /*! |
|
952 \fn void QListWidgetItem::setStatusTip(const QString &statusTip) |
|
953 |
|
954 Sets the status tip for the list item to the text specified by |
|
955 \a statusTip. QListWidget mouseTracking needs to be enabled for this |
|
956 feature to work. |
|
957 |
|
958 \sa statusTip(), setToolTip(), setWhatsThis(), QWidget::setMouseTracking() |
|
959 */ |
|
960 |
|
961 /*! |
|
962 \fn void QListWidgetItem::setToolTip(const QString &toolTip) |
|
963 |
|
964 Sets the tooltip for the list item to the text specified by \a toolTip. |
|
965 |
|
966 \sa toolTip(), setStatusTip(), setWhatsThis() |
|
967 */ |
|
968 |
|
969 /*! |
|
970 \fn void QListWidgetItem::setWhatsThis(const QString &whatsThis) |
|
971 |
|
972 Sets the "What's This?" help for the list item to the text specified by |
|
973 \a whatsThis. |
|
974 |
|
975 \sa whatsThis(), setStatusTip(), setToolTip() |
|
976 */ |
|
977 |
|
978 /*! |
|
979 \fn void QListWidgetItem::setFont(const QFont &font) |
|
980 |
|
981 Sets the font used when painting the item to the given \a font. |
|
982 */ |
|
983 |
|
984 /*! |
|
985 \fn void QListWidgetItem::setTextAlignment(int alignment) |
|
986 |
|
987 Sets the list item's text alignment to \a alignment. |
|
988 |
|
989 \sa Qt::AlignmentFlag |
|
990 */ |
|
991 |
|
992 /*! |
|
993 \fn void QListWidgetItem::setBackgroundColor(const QColor &color) |
|
994 \obsolete |
|
995 |
|
996 This function is deprecated. Use setBackground() instead. |
|
997 */ |
|
998 |
|
999 /*! |
|
1000 \fn void QListWidgetItem::setBackground(const QBrush &brush) |
|
1001 \since 4.2 |
|
1002 |
|
1003 Sets the background brush of the list item to the given \a brush. |
|
1004 |
|
1005 \sa background() setForeground() |
|
1006 */ |
|
1007 |
|
1008 /*! |
|
1009 \fn void QListWidgetItem::setTextColor(const QColor &color) |
|
1010 \obsolete |
|
1011 |
|
1012 This function is deprecated. Use setForeground() instead. |
|
1013 */ |
|
1014 |
|
1015 /*! |
|
1016 \fn void QListWidgetItem::setForeground(const QBrush &brush) |
|
1017 \since 4.2 |
|
1018 |
|
1019 Sets the foreground brush of the list item to the given \a brush. |
|
1020 |
|
1021 \sa foreground() setBackground() |
|
1022 */ |
|
1023 |
|
1024 /*! |
|
1025 \fn void QListWidgetItem::setCheckState(Qt::CheckState state) |
|
1026 |
|
1027 Sets the check state of the list item to \a state. |
|
1028 |
|
1029 \sa checkState() |
|
1030 */ |
|
1031 |
|
1032 void QListWidgetPrivate::setup() |
|
1033 { |
|
1034 Q_Q(QListWidget); |
|
1035 q->QListView::setModel(new QListModel(q)); |
|
1036 // view signals |
|
1037 QObject::connect(q, SIGNAL(pressed(QModelIndex)), q, SLOT(_q_emitItemPressed(QModelIndex))); |
|
1038 QObject::connect(q, SIGNAL(clicked(QModelIndex)), q, SLOT(_q_emitItemClicked(QModelIndex))); |
|
1039 QObject::connect(q, SIGNAL(doubleClicked(QModelIndex)), |
|
1040 q, SLOT(_q_emitItemDoubleClicked(QModelIndex))); |
|
1041 QObject::connect(q, SIGNAL(activated(QModelIndex)), |
|
1042 q, SLOT(_q_emitItemActivated(QModelIndex))); |
|
1043 QObject::connect(q, SIGNAL(entered(QModelIndex)), q, SLOT(_q_emitItemEntered(QModelIndex))); |
|
1044 QObject::connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), |
|
1045 q, SLOT(_q_emitItemChanged(QModelIndex))); |
|
1046 QObject::connect(q->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), |
|
1047 q, SLOT(_q_emitCurrentItemChanged(QModelIndex,QModelIndex))); |
|
1048 QObject::connect(q->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), |
|
1049 q, SIGNAL(itemSelectionChanged())); |
|
1050 QObject::connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), |
|
1051 q, SLOT(_q_dataChanged(QModelIndex,QModelIndex))); |
|
1052 QObject::connect(model, SIGNAL(columnsRemoved(QModelIndex,int,int)), q, SLOT(_q_sort())); |
|
1053 } |
|
1054 |
|
1055 void QListWidgetPrivate::_q_emitItemPressed(const QModelIndex &index) |
|
1056 { |
|
1057 Q_Q(QListWidget); |
|
1058 emit q->itemPressed(listModel()->at(index.row())); |
|
1059 } |
|
1060 |
|
1061 void QListWidgetPrivate::_q_emitItemClicked(const QModelIndex &index) |
|
1062 { |
|
1063 Q_Q(QListWidget); |
|
1064 emit q->itemClicked(listModel()->at(index.row())); |
|
1065 } |
|
1066 |
|
1067 void QListWidgetPrivate::_q_emitItemDoubleClicked(const QModelIndex &index) |
|
1068 { |
|
1069 Q_Q(QListWidget); |
|
1070 emit q->itemDoubleClicked(listModel()->at(index.row())); |
|
1071 } |
|
1072 |
|
1073 void QListWidgetPrivate::_q_emitItemActivated(const QModelIndex &index) |
|
1074 { |
|
1075 Q_Q(QListWidget); |
|
1076 emit q->itemActivated(listModel()->at(index.row())); |
|
1077 } |
|
1078 |
|
1079 void QListWidgetPrivate::_q_emitItemEntered(const QModelIndex &index) |
|
1080 { |
|
1081 Q_Q(QListWidget); |
|
1082 emit q->itemEntered(listModel()->at(index.row())); |
|
1083 } |
|
1084 |
|
1085 void QListWidgetPrivate::_q_emitItemChanged(const QModelIndex &index) |
|
1086 { |
|
1087 Q_Q(QListWidget); |
|
1088 emit q->itemChanged(listModel()->at(index.row())); |
|
1089 } |
|
1090 |
|
1091 void QListWidgetPrivate::_q_emitCurrentItemChanged(const QModelIndex ¤t, |
|
1092 const QModelIndex &previous) |
|
1093 { |
|
1094 Q_Q(QListWidget); |
|
1095 QPersistentModelIndex persistentCurrent = current; |
|
1096 QListWidgetItem *currentItem = listModel()->at(persistentCurrent.row()); |
|
1097 emit q->currentItemChanged(currentItem, listModel()->at(previous.row())); |
|
1098 |
|
1099 //persistentCurrent is invalid if something changed the model in response |
|
1100 //to the currentItemChanged signal emission and the item was removed |
|
1101 if (!persistentCurrent.isValid()) { |
|
1102 currentItem = 0; |
|
1103 } |
|
1104 |
|
1105 emit q->currentTextChanged(currentItem ? currentItem->text() : QString()); |
|
1106 emit q->currentRowChanged(persistentCurrent.row()); |
|
1107 } |
|
1108 |
|
1109 void QListWidgetPrivate::_q_sort() |
|
1110 { |
|
1111 if (sortingEnabled) |
|
1112 model->sort(0, sortOrder); |
|
1113 } |
|
1114 |
|
1115 void QListWidgetPrivate::_q_dataChanged(const QModelIndex &topLeft, |
|
1116 const QModelIndex &bottomRight) |
|
1117 { |
|
1118 if (sortingEnabled && topLeft.isValid() && bottomRight.isValid()) |
|
1119 listModel()->ensureSorted(topLeft.column(), sortOrder, |
|
1120 topLeft.row(), bottomRight.row()); |
|
1121 } |
|
1122 |
|
1123 /*! |
|
1124 \class QListWidget |
|
1125 \brief The QListWidget class provides an item-based list widget. |
|
1126 |
|
1127 \ingroup model-view |
|
1128 |
|
1129 |
|
1130 QListWidget is a convenience class that provides a list view similar to the |
|
1131 one supplied by QListView, but with a classic item-based interface for |
|
1132 adding and removing items. QListWidget uses an internal model to manage |
|
1133 each QListWidgetItem in the list. |
|
1134 |
|
1135 For a more flexible list view widget, use the QListView class with a |
|
1136 standard model. |
|
1137 |
|
1138 List widgets are constructed in the same way as other widgets: |
|
1139 |
|
1140 \snippet doc/src/snippets/qlistwidget-using/mainwindow.cpp 0 |
|
1141 |
|
1142 The selectionMode() of a list widget determines how many of the items in |
|
1143 the list can be selected at the same time, and whether complex selections |
|
1144 of items can be created. This can be set with the setSelectionMode() |
|
1145 function. |
|
1146 |
|
1147 There are two ways to add items to the list: they can be constructed with |
|
1148 the list widget as their parent widget, or they can be constructed with no |
|
1149 parent widget and added to the list later. If a list widget already exists |
|
1150 when the items are constructed, the first method is easier to use: |
|
1151 |
|
1152 \snippet doc/src/snippets/qlistwidget-using/mainwindow.cpp 1 |
|
1153 |
|
1154 If you need to insert a new item into the list at a particular position, it |
|
1155 is more required to construct the item without a parent widget and use the |
|
1156 insertItem() function to place it within the list. The list widget will |
|
1157 take ownership of the item. |
|
1158 |
|
1159 \snippet doc/src/snippets/qlistwidget-using/mainwindow.cpp 6 |
|
1160 \snippet doc/src/snippets/qlistwidget-using/mainwindow.cpp 7 |
|
1161 |
|
1162 For multiple items, insertItems() can be used instead. The number of items |
|
1163 in the list is found with the count() function. To remove items from the |
|
1164 list, use takeItem(). |
|
1165 |
|
1166 The current item in the list can be found with currentItem(), and changed |
|
1167 with setCurrentItem(). The user can also change the current item by |
|
1168 navigating with the keyboard or clicking on a different item. When the |
|
1169 current item changes, the currentItemChanged() signal is emitted with the |
|
1170 new current item and the item that was previously current. |
|
1171 |
|
1172 \table 100% |
|
1173 \row \o \inlineimage windowsxp-listview.png Screenshot of a Windows XP style list widget |
|
1174 \o \inlineimage macintosh-listview.png Screenshot of a Macintosh style table widget |
|
1175 \o \inlineimage plastique-listview.png Screenshot of a Plastique style table widget |
|
1176 \row \o A \l{Windows XP Style Widget Gallery}{Windows XP style} list widget. |
|
1177 \o A \l{Macintosh Style Widget Gallery}{Macintosh style} list widget. |
|
1178 \o A \l{Plastique Style Widget Gallery}{Plastique style} list widget. |
|
1179 \endtable |
|
1180 |
|
1181 \sa QListWidgetItem, QListView, QTreeView, {Model/View Programming}, |
|
1182 {Config Dialog Example} |
|
1183 */ |
|
1184 |
|
1185 /*! |
|
1186 \fn void QListWidget::addItem(QListWidgetItem *item) |
|
1187 |
|
1188 Inserts the \a item at the end of the list widget. |
|
1189 |
|
1190 \warning A QListWidgetItem can only be added to a QListWidget once. Adding |
|
1191 the same QListWidgetItem multiple times to a QListWidget will result in |
|
1192 undefined behavior. |
|
1193 |
|
1194 \sa insertItem() |
|
1195 */ |
|
1196 |
|
1197 /*! |
|
1198 \fn void QListWidget::addItem(const QString &label) |
|
1199 |
|
1200 Inserts an item with the text \a label at the end of the list widget. |
|
1201 */ |
|
1202 |
|
1203 /*! |
|
1204 \fn void QListWidget::addItems(const QStringList &labels) |
|
1205 |
|
1206 Inserts items with the text \a labels at the end of the list widget. |
|
1207 |
|
1208 \sa insertItems() |
|
1209 */ |
|
1210 |
|
1211 /*! |
|
1212 \fn void QListWidget::itemPressed(QListWidgetItem *item) |
|
1213 |
|
1214 This signal is emitted with the specified \a item when a mouse button is |
|
1215 pressed on an item in the widget. |
|
1216 |
|
1217 \sa itemClicked(), itemDoubleClicked() |
|
1218 */ |
|
1219 |
|
1220 /*! |
|
1221 \fn void QListWidget::itemClicked(QListWidgetItem *item) |
|
1222 |
|
1223 This signal is emitted with the specified \a item when a mouse button is |
|
1224 clicked on an item in the widget. |
|
1225 |
|
1226 \sa itemPressed(), itemDoubleClicked() |
|
1227 */ |
|
1228 |
|
1229 /*! |
|
1230 \fn void QListWidget::itemDoubleClicked(QListWidgetItem *item) |
|
1231 |
|
1232 This signal is emitted with the specified \a item when a mouse button is |
|
1233 double clicked on an item in the widget. |
|
1234 |
|
1235 \sa itemClicked(), itemPressed() |
|
1236 */ |
|
1237 |
|
1238 /*! |
|
1239 \fn void QListWidget::itemActivated(QListWidgetItem *item) |
|
1240 |
|
1241 This signal is emitted when the \a item is activated. The \a item is |
|
1242 activated when the user clicks or double clicks on it, depending on the |
|
1243 system configuration. It is also activated when the user presses the |
|
1244 activation key (on Windows and X11 this is the \gui Return key, on Mac OS |
|
1245 X it is \key{Ctrl+0}). |
|
1246 */ |
|
1247 |
|
1248 /*! |
|
1249 \fn void QListWidget::itemEntered(QListWidgetItem *item) |
|
1250 |
|
1251 This signal is emitted when the mouse cursor enters an item. The \a item is |
|
1252 the item entered. This signal is only emitted when mouseTracking is turned |
|
1253 on, or when a mouse button is pressed while moving into an item. |
|
1254 |
|
1255 \sa QWidget::setMouseTracking() |
|
1256 */ |
|
1257 |
|
1258 /*! |
|
1259 \fn void QListWidget::itemChanged(QListWidgetItem *item) |
|
1260 |
|
1261 This signal is emitted whenever the data of \a item has changed. |
|
1262 */ |
|
1263 |
|
1264 /*! |
|
1265 \fn void QListWidget::currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous) |
|
1266 |
|
1267 This signal is emitted whenever the current item changes. |
|
1268 |
|
1269 \a previous is the item that previously had the focus; \a current is the |
|
1270 new current item. |
|
1271 */ |
|
1272 |
|
1273 /*! |
|
1274 \fn void QListWidget::currentTextChanged(const QString ¤tText) |
|
1275 |
|
1276 This signal is emitted whenever the current item changes. |
|
1277 |
|
1278 \a currentText is the text data in the current item. If there is no current |
|
1279 item, the \a currentText is invalid. |
|
1280 */ |
|
1281 |
|
1282 /*! |
|
1283 \fn void QListWidget::currentRowChanged(int currentRow) |
|
1284 |
|
1285 This signal is emitted whenever the current item changes. |
|
1286 |
|
1287 \a currentRow is the row of the current item. If there is no current item, |
|
1288 the \a currentRow is -1. |
|
1289 */ |
|
1290 |
|
1291 /*! |
|
1292 \fn void QListWidget::itemSelectionChanged() |
|
1293 |
|
1294 This signal is emitted whenever the selection changes. |
|
1295 |
|
1296 \sa selectedItems(), QListWidgetItem::isSelected(), currentItemChanged() |
|
1297 */ |
|
1298 |
|
1299 /*! |
|
1300 \since 4.3 |
|
1301 |
|
1302 \fn void QListWidget::removeItemWidget(QListWidgetItem *item) |
|
1303 |
|
1304 Removes the widget set on the given \a item. |
|
1305 */ |
|
1306 |
|
1307 /*! |
|
1308 Constructs an empty QListWidget with the given \a parent. |
|
1309 */ |
|
1310 |
|
1311 QListWidget::QListWidget(QWidget *parent) |
|
1312 : QListView(*new QListWidgetPrivate(), parent) |
|
1313 { |
|
1314 Q_D(QListWidget); |
|
1315 d->setup(); |
|
1316 } |
|
1317 |
|
1318 /*! |
|
1319 Destroys the list widget and all its items. |
|
1320 */ |
|
1321 |
|
1322 QListWidget::~QListWidget() |
|
1323 { |
|
1324 } |
|
1325 |
|
1326 /*! |
|
1327 Returns the item that occupies the given \a row in the list if one has been |
|
1328 set; otherwise returns 0. |
|
1329 |
|
1330 \sa row() |
|
1331 */ |
|
1332 |
|
1333 QListWidgetItem *QListWidget::item(int row) const |
|
1334 { |
|
1335 Q_D(const QListWidget); |
|
1336 if (row < 0 || row >= d->model->rowCount()) |
|
1337 return 0; |
|
1338 return d->listModel()->at(row); |
|
1339 } |
|
1340 |
|
1341 /*! |
|
1342 Returns the row containing the given \a item. |
|
1343 |
|
1344 \sa item() |
|
1345 */ |
|
1346 |
|
1347 int QListWidget::row(const QListWidgetItem *item) const |
|
1348 { |
|
1349 Q_D(const QListWidget); |
|
1350 return d->listModel()->index(const_cast<QListWidgetItem*>(item)).row(); |
|
1351 } |
|
1352 |
|
1353 |
|
1354 /*! |
|
1355 Inserts the \a item at the position in the list given by \a row. |
|
1356 |
|
1357 \sa addItem() |
|
1358 */ |
|
1359 |
|
1360 void QListWidget::insertItem(int row, QListWidgetItem *item) |
|
1361 { |
|
1362 Q_D(QListWidget); |
|
1363 if (item && !item->view) |
|
1364 d->listModel()->insert(row, item); |
|
1365 } |
|
1366 |
|
1367 /*! |
|
1368 Inserts an item with the text \a label in the list widget at the position |
|
1369 given by \a row. |
|
1370 |
|
1371 \sa addItem() |
|
1372 */ |
|
1373 |
|
1374 void QListWidget::insertItem(int row, const QString &label) |
|
1375 { |
|
1376 Q_D(QListWidget); |
|
1377 d->listModel()->insert(row, new QListWidgetItem(label)); |
|
1378 } |
|
1379 |
|
1380 /*! |
|
1381 Inserts items from the list of \a labels into the list, starting at the |
|
1382 given \a row. |
|
1383 |
|
1384 \sa insertItem(), addItem() |
|
1385 */ |
|
1386 |
|
1387 void QListWidget::insertItems(int row, const QStringList &labels) |
|
1388 { |
|
1389 Q_D(QListWidget); |
|
1390 d->listModel()->insert(row, labels); |
|
1391 } |
|
1392 |
|
1393 /*! |
|
1394 Removes and returns the item from the given \a row in the list widget; |
|
1395 otherwise returns 0. |
|
1396 |
|
1397 Items removed from a list widget will not be managed by Qt, and will need |
|
1398 to be deleted manually. |
|
1399 |
|
1400 \sa insertItem(), addItem() |
|
1401 */ |
|
1402 |
|
1403 QListWidgetItem *QListWidget::takeItem(int row) |
|
1404 { |
|
1405 Q_D(QListWidget); |
|
1406 if (row < 0 || row >= d->model->rowCount()) |
|
1407 return 0; |
|
1408 return d->listModel()->take(row); |
|
1409 } |
|
1410 |
|
1411 /*! |
|
1412 \property QListWidget::count |
|
1413 \brief the number of items in the list including any hidden items. |
|
1414 */ |
|
1415 |
|
1416 int QListWidget::count() const |
|
1417 { |
|
1418 Q_D(const QListWidget); |
|
1419 return d->model->rowCount(); |
|
1420 } |
|
1421 |
|
1422 /*! |
|
1423 Returns the current item. |
|
1424 */ |
|
1425 QListWidgetItem *QListWidget::currentItem() const |
|
1426 { |
|
1427 Q_D(const QListWidget); |
|
1428 return d->listModel()->at(currentIndex().row()); |
|
1429 } |
|
1430 |
|
1431 |
|
1432 /*! |
|
1433 Sets the current item to \a item. |
|
1434 |
|
1435 Unless the selection mode is \l{QAbstractItemView::}{NoSelection}, |
|
1436 the item is also be selected. |
|
1437 */ |
|
1438 void QListWidget::setCurrentItem(QListWidgetItem *item) |
|
1439 { |
|
1440 setCurrentRow(row(item)); |
|
1441 } |
|
1442 |
|
1443 /*! |
|
1444 \since 4.4 |
|
1445 Set the current item to \a item, using the given \a command. |
|
1446 */ |
|
1447 void QListWidget::setCurrentItem(QListWidgetItem *item, QItemSelectionModel::SelectionFlags command) |
|
1448 { |
|
1449 setCurrentRow(row(item), command); |
|
1450 } |
|
1451 |
|
1452 /*! |
|
1453 \property QListWidget::currentRow |
|
1454 \brief the row of the current item. |
|
1455 |
|
1456 Depending on the current selection mode, the row may also be selected. |
|
1457 */ |
|
1458 |
|
1459 int QListWidget::currentRow() const |
|
1460 { |
|
1461 return currentIndex().row(); |
|
1462 } |
|
1463 |
|
1464 void QListWidget::setCurrentRow(int row) |
|
1465 { |
|
1466 Q_D(QListWidget); |
|
1467 QModelIndex index = d->listModel()->index(row); |
|
1468 if (d->selectionMode == SingleSelection) |
|
1469 selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect); |
|
1470 else if (d->selectionMode == NoSelection) |
|
1471 selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate); |
|
1472 else |
|
1473 selectionModel()->setCurrentIndex(index, QItemSelectionModel::SelectCurrent); |
|
1474 } |
|
1475 |
|
1476 /*! |
|
1477 \since 4.4 |
|
1478 |
|
1479 Sets the current row to be the given \a row, using the given \a command, |
|
1480 */ |
|
1481 void QListWidget::setCurrentRow(int row, QItemSelectionModel::SelectionFlags command) |
|
1482 { |
|
1483 Q_D(QListWidget); |
|
1484 d->selectionModel->setCurrentIndex(d->listModel()->index(row), command); |
|
1485 } |
|
1486 |
|
1487 /*! |
|
1488 Returns a pointer to the item at the coordinates \a p. |
|
1489 */ |
|
1490 QListWidgetItem *QListWidget::itemAt(const QPoint &p) const |
|
1491 { |
|
1492 Q_D(const QListWidget); |
|
1493 return d->listModel()->at(indexAt(p).row()); |
|
1494 |
|
1495 } |
|
1496 |
|
1497 /*! |
|
1498 \fn QListWidgetItem *QListWidget::itemAt(int x, int y) const |
|
1499 \overload |
|
1500 |
|
1501 Returns a pointer to the item at the coordinates (\a x, \a y). |
|
1502 */ |
|
1503 |
|
1504 |
|
1505 /*! |
|
1506 Returns the rectangle on the viewport occupied by the item at \a item. |
|
1507 */ |
|
1508 QRect QListWidget::visualItemRect(const QListWidgetItem *item) const |
|
1509 { |
|
1510 Q_D(const QListWidget); |
|
1511 QModelIndex index = d->listModel()->index(const_cast<QListWidgetItem*>(item)); |
|
1512 return visualRect(index); |
|
1513 } |
|
1514 |
|
1515 /*! |
|
1516 Sorts all the items in the list widget according to the specified \a order. |
|
1517 */ |
|
1518 void QListWidget::sortItems(Qt::SortOrder order) |
|
1519 { |
|
1520 Q_D(QListWidget); |
|
1521 d->sortOrder = order; |
|
1522 d->listModel()->sort(0, order); |
|
1523 } |
|
1524 |
|
1525 /*! |
|
1526 \since 4.2 |
|
1527 \property QListWidget::sortingEnabled |
|
1528 \brief whether sorting is enabled |
|
1529 |
|
1530 If this property is true, sorting is enabled for the list; if the property |
|
1531 is false, sorting is not enabled. |
|
1532 |
|
1533 The default value is false. |
|
1534 */ |
|
1535 void QListWidget::setSortingEnabled(bool enable) |
|
1536 { |
|
1537 Q_D(QListWidget); |
|
1538 d->sortingEnabled = enable; |
|
1539 } |
|
1540 |
|
1541 bool QListWidget::isSortingEnabled() const |
|
1542 { |
|
1543 Q_D(const QListWidget); |
|
1544 return d->sortingEnabled; |
|
1545 } |
|
1546 |
|
1547 /*! |
|
1548 \internal |
|
1549 */ |
|
1550 Qt::SortOrder QListWidget::sortOrder() const |
|
1551 { |
|
1552 Q_D(const QListWidget); |
|
1553 return d->sortOrder; |
|
1554 } |
|
1555 |
|
1556 /*! |
|
1557 Starts editing the \a item if it is editable. |
|
1558 */ |
|
1559 |
|
1560 void QListWidget::editItem(QListWidgetItem *item) |
|
1561 { |
|
1562 Q_D(QListWidget); |
|
1563 edit(d->listModel()->index(item)); |
|
1564 } |
|
1565 |
|
1566 /*! |
|
1567 Opens an editor for the given \a item. The editor remains open after |
|
1568 editing. |
|
1569 |
|
1570 \sa closePersistentEditor() |
|
1571 */ |
|
1572 void QListWidget::openPersistentEditor(QListWidgetItem *item) |
|
1573 { |
|
1574 Q_D(QListWidget); |
|
1575 QModelIndex index = d->listModel()->index(item); |
|
1576 QAbstractItemView::openPersistentEditor(index); |
|
1577 } |
|
1578 |
|
1579 /*! |
|
1580 Closes the persistent editor for the given \a item. |
|
1581 |
|
1582 \sa openPersistentEditor() |
|
1583 */ |
|
1584 void QListWidget::closePersistentEditor(QListWidgetItem *item) |
|
1585 { |
|
1586 Q_D(QListWidget); |
|
1587 QModelIndex index = d->listModel()->index(item); |
|
1588 QAbstractItemView::closePersistentEditor(index); |
|
1589 } |
|
1590 |
|
1591 /*! |
|
1592 \since 4.1 |
|
1593 |
|
1594 Returns the widget displayed in the given \a item. |
|
1595 */ |
|
1596 QWidget *QListWidget::itemWidget(QListWidgetItem *item) const |
|
1597 { |
|
1598 Q_D(const QListWidget); |
|
1599 QModelIndex index = d->listModel()->index(item); |
|
1600 return QAbstractItemView::indexWidget(index); |
|
1601 } |
|
1602 |
|
1603 /*! |
|
1604 \since 4.1 |
|
1605 |
|
1606 Sets the \a widget to be displayed in the give \a item. |
|
1607 |
|
1608 This function should only be used to display static content in the place of |
|
1609 a list widget item. If you want to display custom dynamic content or |
|
1610 implement a custom editor widget, use QListView and subclass QItemDelegate |
|
1611 instead. |
|
1612 |
|
1613 \sa {Delegate Classes} |
|
1614 */ |
|
1615 void QListWidget::setItemWidget(QListWidgetItem *item, QWidget *widget) |
|
1616 { |
|
1617 Q_D(QListWidget); |
|
1618 QModelIndex index = d->listModel()->index(item); |
|
1619 QAbstractItemView::setIndexWidget(index, widget); |
|
1620 } |
|
1621 |
|
1622 /*! |
|
1623 Returns true if \a item is selected; otherwise returns false. |
|
1624 |
|
1625 \obsolete |
|
1626 |
|
1627 This function is deprecated. Use QListWidgetItem::isSelected() instead. |
|
1628 */ |
|
1629 bool QListWidget::isItemSelected(const QListWidgetItem *item) const |
|
1630 { |
|
1631 Q_D(const QListWidget); |
|
1632 QModelIndex index = d->listModel()->index(const_cast<QListWidgetItem*>(item)); |
|
1633 return selectionModel()->isSelected(index); |
|
1634 } |
|
1635 |
|
1636 /*! |
|
1637 Selects or deselects the given \a item depending on whether \a select is |
|
1638 true of false. |
|
1639 |
|
1640 \obsolete |
|
1641 |
|
1642 This function is deprecated. Use QListWidgetItem::setSelected() instead. |
|
1643 */ |
|
1644 void QListWidget::setItemSelected(const QListWidgetItem *item, bool select) |
|
1645 { |
|
1646 Q_D(QListWidget); |
|
1647 QModelIndex index = d->listModel()->index(const_cast<QListWidgetItem*>(item)); |
|
1648 |
|
1649 if (d->selectionMode == SingleSelection) { |
|
1650 selectionModel()->select(index, select |
|
1651 ? QItemSelectionModel::ClearAndSelect |
|
1652 : QItemSelectionModel::Deselect); |
|
1653 } else if (d->selectionMode != NoSelection) { |
|
1654 selectionModel()->select(index, select |
|
1655 ? QItemSelectionModel::Select |
|
1656 : QItemSelectionModel::Deselect); |
|
1657 } |
|
1658 |
|
1659 } |
|
1660 |
|
1661 /*! |
|
1662 Returns a list of all selected items in the list widget. |
|
1663 */ |
|
1664 |
|
1665 QList<QListWidgetItem*> QListWidget::selectedItems() const |
|
1666 { |
|
1667 Q_D(const QListWidget); |
|
1668 QModelIndexList indexes = selectionModel()->selectedIndexes(); |
|
1669 QList<QListWidgetItem*> items; |
|
1670 for (int i = 0; i < indexes.count(); ++i) |
|
1671 items.append(d->listModel()->at(indexes.at(i).row())); |
|
1672 return items; |
|
1673 } |
|
1674 |
|
1675 /*! |
|
1676 Finds items with the text that matches the string \a text using the given |
|
1677 \a flags. |
|
1678 */ |
|
1679 |
|
1680 QList<QListWidgetItem*> QListWidget::findItems(const QString &text, Qt::MatchFlags flags) const |
|
1681 { |
|
1682 Q_D(const QListWidget); |
|
1683 QModelIndexList indexes = d->listModel()->match(model()->index(0, 0, QModelIndex()), |
|
1684 Qt::DisplayRole, text, -1, flags); |
|
1685 QList<QListWidgetItem*> items; |
|
1686 for (int i = 0; i < indexes.size(); ++i) |
|
1687 items.append(d->listModel()->at(indexes.at(i).row())); |
|
1688 return items; |
|
1689 } |
|
1690 |
|
1691 /*! |
|
1692 Returns true if the \a item is explicitly hidden; otherwise returns false. |
|
1693 |
|
1694 \obsolete |
|
1695 |
|
1696 This function is deprecated. Use QListWidgetItem::isHidden() instead. |
|
1697 */ |
|
1698 bool QListWidget::isItemHidden(const QListWidgetItem *item) const |
|
1699 { |
|
1700 return isRowHidden(row(item)); |
|
1701 } |
|
1702 |
|
1703 /*! |
|
1704 If \a hide is true, the \a item will be hidden; otherwise it will be shown. |
|
1705 |
|
1706 \obsolete |
|
1707 |
|
1708 This function is deprecated. Use QListWidgetItem::setHidden() instead. |
|
1709 */ |
|
1710 void QListWidget::setItemHidden(const QListWidgetItem *item, bool hide) |
|
1711 { |
|
1712 setRowHidden(row(item), hide); |
|
1713 } |
|
1714 |
|
1715 /*! |
|
1716 Scrolls the view if necessary to ensure that the \a item is visible. |
|
1717 |
|
1718 \a hint specifies where the \a item should be located after the operation. |
|
1719 */ |
|
1720 |
|
1721 void QListWidget::scrollToItem(const QListWidgetItem *item, QAbstractItemView::ScrollHint hint) |
|
1722 { |
|
1723 Q_D(QListWidget); |
|
1724 QModelIndex index = d->listModel()->index(const_cast<QListWidgetItem*>(item)); |
|
1725 QListView::scrollTo(index, hint); |
|
1726 } |
|
1727 |
|
1728 /*! |
|
1729 Removes all items and selections in the view. |
|
1730 |
|
1731 \warning All items will be permanently deleted. |
|
1732 */ |
|
1733 void QListWidget::clear() |
|
1734 { |
|
1735 Q_D(QListWidget); |
|
1736 selectionModel()->clear(); |
|
1737 d->listModel()->clear(); |
|
1738 } |
|
1739 |
|
1740 /*! |
|
1741 Returns a list of MIME types that can be used to describe a list of |
|
1742 listwidget items. |
|
1743 |
|
1744 \sa mimeData() |
|
1745 */ |
|
1746 QStringList QListWidget::mimeTypes() const |
|
1747 { |
|
1748 return d_func()->listModel()->QAbstractListModel::mimeTypes(); |
|
1749 } |
|
1750 |
|
1751 /*! |
|
1752 Returns an object that contains a serialized description of the specified |
|
1753 \a items. The format used to describe the items is obtained from the |
|
1754 mimeTypes() function. |
|
1755 |
|
1756 If the list of items is empty, 0 is returned instead of a serialized empty |
|
1757 list. |
|
1758 */ |
|
1759 QMimeData *QListWidget::mimeData(const QList<QListWidgetItem*>) const |
|
1760 { |
|
1761 return d_func()->listModel()->internalMimeData(); |
|
1762 } |
|
1763 |
|
1764 #ifndef QT_NO_DRAGANDDROP |
|
1765 /*! |
|
1766 Handles \a data supplied by an external drag and drop operation that ended |
|
1767 with the given \a action in the given \a index. Returns true if \a data and |
|
1768 \a action can be handled by the model; otherwise returns false. |
|
1769 |
|
1770 \sa supportedDropActions() |
|
1771 */ |
|
1772 bool QListWidget::dropMimeData(int index, const QMimeData *data, Qt::DropAction action) |
|
1773 { |
|
1774 QModelIndex idx; |
|
1775 int row = index; |
|
1776 int column = 0; |
|
1777 if (dropIndicatorPosition() == QAbstractItemView::OnItem) { |
|
1778 // QAbstractListModel::dropMimeData will overwrite on the index if row == -1 and column == -1 |
|
1779 idx = model()->index(row, column); |
|
1780 row = -1; |
|
1781 column = -1; |
|
1782 } |
|
1783 return d_func()->listModel()->QAbstractListModel::dropMimeData(data, action , row, column, idx); |
|
1784 } |
|
1785 |
|
1786 /*! \reimp */ |
|
1787 void QListWidget::dropEvent(QDropEvent *event) { |
|
1788 Q_D(QListWidget); |
|
1789 if (event->source() == this && d->movement != Static) { |
|
1790 QListView::dropEvent(event); |
|
1791 return; |
|
1792 } |
|
1793 |
|
1794 if (event->source() == this && (event->dropAction() == Qt::MoveAction || |
|
1795 dragDropMode() == QAbstractItemView::InternalMove)) { |
|
1796 QModelIndex topIndex; |
|
1797 int col = -1; |
|
1798 int row = -1; |
|
1799 if (d->dropOn(event, &row, &col, &topIndex)) { |
|
1800 QList<QModelIndex> selIndexes = selectedIndexes(); |
|
1801 QList<QPersistentModelIndex> persIndexes; |
|
1802 for (int i = 0; i < selIndexes.count(); i++) |
|
1803 persIndexes.append(selIndexes.at(i)); |
|
1804 |
|
1805 if (persIndexes.contains(topIndex)) |
|
1806 return; |
|
1807 |
|
1808 QPersistentModelIndex dropRow = model()->index(row, col, topIndex); |
|
1809 |
|
1810 QList<QListWidgetItem *> taken; |
|
1811 for (int i = 0; i < persIndexes.count(); ++i) |
|
1812 taken.append(takeItem(persIndexes.at(i).row())); |
|
1813 |
|
1814 // insert them back in at their new positions |
|
1815 for (int i = 0; i < persIndexes.count(); ++i) { |
|
1816 // Either at a specific point or appended |
|
1817 if (row == -1) { |
|
1818 insertItem(count(), taken.takeFirst()); |
|
1819 } else { |
|
1820 int r = dropRow.row() >= 0 ? dropRow.row() : row; |
|
1821 insertItem(qMin(r, count()), taken.takeFirst()); |
|
1822 } |
|
1823 } |
|
1824 |
|
1825 event->accept(); |
|
1826 // Don't want QAbstractItemView to delete it because it was "moved" we already did it |
|
1827 event->setDropAction(Qt::CopyAction); |
|
1828 } |
|
1829 } |
|
1830 |
|
1831 QListView::dropEvent(event); |
|
1832 } |
|
1833 |
|
1834 /*! |
|
1835 Returns the drop actions supported by this view. |
|
1836 |
|
1837 \sa Qt::DropActions |
|
1838 */ |
|
1839 Qt::DropActions QListWidget::supportedDropActions() const |
|
1840 { |
|
1841 Q_D(const QListWidget); |
|
1842 return d->listModel()->QAbstractListModel::supportedDropActions() | Qt::MoveAction; |
|
1843 } |
|
1844 #endif // QT_NO_DRAGANDDROP |
|
1845 |
|
1846 /*! |
|
1847 Returns a list of pointers to the items contained in the \a data object. If |
|
1848 the object was not created by a QListWidget in the same process, the list |
|
1849 is empty. |
|
1850 */ |
|
1851 QList<QListWidgetItem*> QListWidget::items(const QMimeData *data) const |
|
1852 { |
|
1853 const QListWidgetMimeData *lwd = qobject_cast<const QListWidgetMimeData*>(data); |
|
1854 if (lwd) |
|
1855 return lwd->items; |
|
1856 return QList<QListWidgetItem*>(); |
|
1857 } |
|
1858 |
|
1859 /*! |
|
1860 Returns the QModelIndex assocated with the given \a item. |
|
1861 */ |
|
1862 |
|
1863 QModelIndex QListWidget::indexFromItem(QListWidgetItem *item) const |
|
1864 { |
|
1865 Q_D(const QListWidget); |
|
1866 return d->listModel()->index(item); |
|
1867 } |
|
1868 |
|
1869 /*! |
|
1870 Returns a pointer to the QListWidgetItem assocated with the given \a index. |
|
1871 */ |
|
1872 |
|
1873 QListWidgetItem *QListWidget::itemFromIndex(const QModelIndex &index) const |
|
1874 { |
|
1875 Q_D(const QListWidget); |
|
1876 if (d->isIndexValid(index)) |
|
1877 return d->listModel()->at(index.row()); |
|
1878 return 0; |
|
1879 } |
|
1880 |
|
1881 /*! |
|
1882 \internal |
|
1883 */ |
|
1884 void QListWidget::setModel(QAbstractItemModel * /*model*/) |
|
1885 { |
|
1886 Q_ASSERT(!"QListWidget::setModel() - Changing the model of the QListWidget is not allowed."); |
|
1887 } |
|
1888 |
|
1889 /*! |
|
1890 \reimp |
|
1891 */ |
|
1892 bool QListWidget::event(QEvent *e) |
|
1893 { |
|
1894 return QListView::event(e); |
|
1895 } |
|
1896 |
|
1897 QT_END_NAMESPACE |
|
1898 |
|
1899 #include "moc_qlistwidget.cpp" |
|
1900 |
|
1901 #endif // QT_NO_LISTWIDGET |