1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite 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 The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/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 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42
43 #include <QtTest/QtTest>
44
45 #include <qstandarditemmodel.h>
46
47 //TESTED_CLASS=
48 //TESTED_FILES=gui/itemviews/qstandarditemmodel.h gui/itemviews/qstandarditemmodel.cpp
49
50 class tst_QStandardItem : public QObject
51 {
52 Q_OBJECT
53
54 public:
55 tst_QStandardItem();
56 virtual ~tst_QStandardItem();
57
58 public slots:
59 void init();
60 void cleanup();
61
62 private slots:
63 void ctor();
64 void textCtor();
65 void iconTextCtor();
66 void rowsColumnsCtor();
67 void getSetData();
68 void getSetFlags();
69 void getSetRowAndColumnCount();
70 void getSetChild_data();
71 void getSetChild();
72 void parent();
73 void insertColumn_data();
74 void insertColumn();
75 void insertColumns_data();
76 void insertColumns();
77 void insertRow_data();
78 void insertRow();
79 void insertRows_data();
80 void insertRows();
81 void appendColumn_data();
82 void appendColumn();
83 void appendRow_data();
84 void appendRow();
85 void takeChild();
86 void takeColumn_data();
87 void takeColumn();
88 void takeRow_data();
89 void takeRow();
90 void streamItem();
91 void deleteItem();
92 void clone();
93 void sortChildren();
94 void subclassing();
95 };
96
tst_QStandardItem()97 tst_QStandardItem::tst_QStandardItem()
98 {
99 }
100
~tst_QStandardItem()101 tst_QStandardItem::~tst_QStandardItem()
102 {
103 }
104
init()105 void tst_QStandardItem::init()
106 {
107 }
108
cleanup()109 void tst_QStandardItem::cleanup()
110 {
111 }
112
ctor()113 void tst_QStandardItem::ctor()
114 {
115 QStandardItem item;
116 QVERIFY(!item.hasChildren());
117 }
118
textCtor()119 void tst_QStandardItem::textCtor()
120 {
121 QLatin1String text("text");
122 QStandardItem item(text);
123 QCOMPARE(item.text(), text);
124 QVERIFY(!item.hasChildren());
125 }
126
iconTextCtor()127 void tst_QStandardItem::iconTextCtor()
128 {
129 QPixmap pixmap(32, 32);
130 pixmap.fill(Qt::red);
131 QIcon icon(pixmap);
132 QLatin1String text("text");
133 QStandardItem item(icon, text);
134 QCOMPARE(item.icon(), icon);
135 QCOMPARE(item.text(), text);
136 QVERIFY(!item.hasChildren());
137 }
138
rowsColumnsCtor()139 void tst_QStandardItem::rowsColumnsCtor()
140 {
141 const int rows = 5;
142 const int columns = 12;
143 QStandardItem item(rows, columns);
144 QCOMPARE(item.rowCount(), rows);
145 QCOMPARE(item.columnCount(), columns);
146 }
147
getSetData()148 void tst_QStandardItem::getSetData()
149 {
150 QStandardItem item;
151 for (int x = 0; x < 2; ++x) {
152 for (int i = 1; i <= 2; ++i) {
153 QString text = QString("text %0").arg(i);
154 item.setText(text);
155 QCOMPARE(item.text(), text);
156
157 QPixmap pixmap(32, 32);
158 pixmap.fill((i == 1) ? Qt::red : Qt::green);
159 QIcon icon(pixmap);
160 item.setIcon(icon);
161 QCOMPARE(item.icon(), icon);
162
163 QString toolTip = QString("toolTip %0").arg(i);
164 item.setToolTip(toolTip);
165 QCOMPARE(item.toolTip(), toolTip);
166
167 QString statusTip = QString("statusTip %0").arg(i);
168 item.setStatusTip(statusTip);
169 QCOMPARE(item.statusTip(), statusTip);
170
171 QString whatsThis = QString("whatsThis %0").arg(i);
172 item.setWhatsThis(whatsThis);
173 QCOMPARE(item.whatsThis(), whatsThis);
174
175 QSize sizeHint(64*i, 48*i);
176 item.setSizeHint(sizeHint);
177 QCOMPARE(item.sizeHint(), sizeHint);
178
179 QFont font;
180 item.setFont(font);
181 QCOMPARE(item.font(), font);
182
183 Qt::Alignment textAlignment((i == 1)
184 ? Qt::AlignLeft|Qt::AlignVCenter
185 : Qt::AlignRight);
186 item.setTextAlignment(textAlignment);
187 QCOMPARE(item.textAlignment(), textAlignment);
188
189 QColor backgroundColor((i == 1) ? Qt::blue : Qt::yellow);
190 item.setBackground(backgroundColor);
191 QCOMPARE(item.background().color(), backgroundColor);
192
193 QColor textColor((i == i) ? Qt::green : Qt::cyan);
194 item.setForeground(textColor);
195 QCOMPARE(item.foreground().color(), textColor);
196
197 Qt::CheckState checkState((i == 1) ? Qt::PartiallyChecked : Qt::Checked);
198 item.setCheckState(checkState);
199 QCOMPARE(item.checkState(), checkState);
200
201 QString accessibleText = QString("accessibleText %0").arg(i);
202 item.setAccessibleText(accessibleText);
203 QCOMPARE(item.accessibleText(), accessibleText);
204
205 QString accessibleDescription = QString("accessibleDescription %0").arg(i);
206 item.setAccessibleDescription(accessibleDescription);
207 QCOMPARE(item.accessibleDescription(), accessibleDescription);
208
209 QCOMPARE(item.text(), text);
210 QCOMPARE(item.icon(), icon);
211 QCOMPARE(item.toolTip(), toolTip);
212 QCOMPARE(item.statusTip(), statusTip);
213 QCOMPARE(item.whatsThis(), whatsThis);
214 QCOMPARE(item.sizeHint(), sizeHint);
215 QCOMPARE(item.font(), font);
216 QCOMPARE(item.textAlignment(), textAlignment);
217 QCOMPARE(item.background().color(), backgroundColor);
218 QCOMPARE(item.foreground().color(), textColor);
219 QCOMPARE(item.checkState(), checkState);
220 QCOMPARE(item.accessibleText(), accessibleText);
221 QCOMPARE(item.accessibleDescription(), accessibleDescription);
222
223 QCOMPARE(qvariant_cast<QString>(item.data(Qt::DisplayRole)), text);
224 QCOMPARE(qvariant_cast<QIcon>(item.data(Qt::DecorationRole)), icon);
225 QCOMPARE(qvariant_cast<QString>(item.data(Qt::ToolTipRole)), toolTip);
226 QCOMPARE(qvariant_cast<QString>(item.data(Qt::StatusTipRole)), statusTip);
227 QCOMPARE(qvariant_cast<QString>(item.data(Qt::WhatsThisRole)), whatsThis);
228 QCOMPARE(qvariant_cast<QSize>(item.data(Qt::SizeHintRole)), sizeHint);
229 QCOMPARE(qvariant_cast<QFont>(item.data(Qt::FontRole)), font);
230 QCOMPARE(qvariant_cast<int>(item.data(Qt::TextAlignmentRole)), int(textAlignment));
231 QCOMPARE(qvariant_cast<QBrush>(item.data(Qt::BackgroundColorRole)), QBrush(backgroundColor));
232 QCOMPARE(qvariant_cast<QBrush>(item.data(Qt::BackgroundRole)), QBrush(backgroundColor));
233 QCOMPARE(qvariant_cast<QBrush>(item.data(Qt::TextColorRole)), QBrush(textColor));
234 QCOMPARE(qvariant_cast<QBrush>(item.data(Qt::ForegroundRole)), QBrush(textColor));
235 QCOMPARE(qvariant_cast<int>(item.data(Qt::CheckStateRole)), int(checkState));
236 QCOMPARE(qvariant_cast<QString>(item.data(Qt::AccessibleTextRole)), accessibleText);
237 QCOMPARE(qvariant_cast<QString>(item.data(Qt::AccessibleDescriptionRole)), accessibleDescription);
238
239 item.setBackground(pixmap);
240 QCOMPARE(item.background().texture(), pixmap);
241 QCOMPARE(qvariant_cast<QBrush>(item.data(Qt::BackgroundRole)).texture(), pixmap);
242 }
243 item.setData(QVariant(), Qt::DisplayRole);
244 item.setData(QVariant(), Qt::DecorationRole);
245 item.setData(QVariant(), Qt::ToolTipRole);
246 item.setData(QVariant(), Qt::StatusTipRole);
247 item.setData(QVariant(), Qt::WhatsThisRole);
248 item.setData(QVariant(), Qt::SizeHintRole);
249 item.setData(QVariant(), Qt::FontRole);
250 item.setData(QVariant(), Qt::TextAlignmentRole);
251 item.setData(QVariant(), Qt::BackgroundRole);
252 item.setData(QVariant(), Qt::ForegroundRole);
253 item.setData(QVariant(), Qt::CheckStateRole);
254 item.setData(QVariant(), Qt::AccessibleTextRole);
255 item.setData(QVariant(), Qt::AccessibleDescriptionRole);
256
257 QCOMPARE(item.data(Qt::DisplayRole), QVariant());
258 QCOMPARE(item.data(Qt::DecorationRole), QVariant());
259 QCOMPARE(item.data(Qt::ToolTipRole), QVariant());
260 QCOMPARE(item.data(Qt::StatusTipRole), QVariant());
261 QCOMPARE(item.data(Qt::WhatsThisRole), QVariant());
262 QCOMPARE(item.data(Qt::SizeHintRole), QVariant());
263 QCOMPARE(item.data(Qt::FontRole), QVariant());
264 QCOMPARE(item.data(Qt::TextAlignmentRole), QVariant());
265 QCOMPARE(item.data(Qt::BackgroundColorRole), QVariant());
266 QCOMPARE(item.data(Qt::BackgroundRole), QVariant());
267 QCOMPARE(item.data(Qt::TextColorRole), QVariant());
268 QCOMPARE(item.data(Qt::ForegroundRole), QVariant());
269 QCOMPARE(item.data(Qt::CheckStateRole), QVariant());
270 QCOMPARE(item.data(Qt::AccessibleTextRole), QVariant());
271 QCOMPARE(item.data(Qt::AccessibleDescriptionRole), QVariant());
272 }
273 }
274
getSetFlags()275 void tst_QStandardItem::getSetFlags()
276 {
277 QStandardItem item;
278 item.setEnabled(true);
279 QVERIFY(item.isEnabled());
280 QVERIFY(item.flags() & Qt::ItemIsEnabled);
281 item.setEditable(true);
282 QVERIFY(item.isEditable());
283 QVERIFY(item.flags() & Qt::ItemIsEditable);
284 item.setSelectable(true);
285 QVERIFY(item.isSelectable());
286 QVERIFY(item.flags() & Qt::ItemIsSelectable);
287 item.setCheckable(true);
288 QVERIFY(item.isCheckable());
289 QCOMPARE(item.checkState(), Qt::Unchecked);
290 QVERIFY(item.flags() & Qt::ItemIsUserCheckable);
291 item.setTristate(true);
292 QVERIFY(item.isTristate());
293 QVERIFY(item.flags() & Qt::ItemIsTristate);
294 #ifndef QT_NO_DRAGANDDROP
295 item.setDragEnabled(true);
296 QVERIFY(item.isDragEnabled());
297 QVERIFY(item.flags() & Qt::ItemIsDragEnabled);
298 item.setDropEnabled(true);
299 QVERIFY(item.isDropEnabled());
300 QVERIFY(item.flags() & Qt::ItemIsDropEnabled);
301 #endif
302
303 QVERIFY(item.isEnabled());
304 item.setEnabled(false);
305 QVERIFY(!item.isEnabled());
306 QVERIFY(!(item.flags() & Qt::ItemIsEnabled));
307 QVERIFY(item.isEditable());
308 item.setEditable(false);
309 QVERIFY(!item.isEditable());
310 QVERIFY(!(item.flags() & Qt::ItemIsEditable));
311 QVERIFY(item.isSelectable());
312 item.setSelectable(false);
313 QVERIFY(!item.isSelectable());
314 QVERIFY(!(item.flags() & Qt::ItemIsSelectable));
315 QVERIFY(item.isCheckable());
316 item.setCheckable(false);
317 QVERIFY(!item.isCheckable());
318 QVERIFY(!(item.flags() & Qt::ItemIsUserCheckable));
319 QVERIFY(item.isTristate());
320 item.setTristate(false);
321 QVERIFY(!item.isTristate());
322 QVERIFY(!(item.flags() & Qt::ItemIsTristate));
323 #ifndef QT_NO_DRAGANDDROP
324 QVERIFY(item.isDragEnabled());
325 item.setDragEnabled(false);
326 QVERIFY(!item.isDragEnabled());
327 QVERIFY(!(item.flags() & Qt::ItemIsDragEnabled));
328 QVERIFY(item.isDropEnabled());
329 item.setDropEnabled(false);
330 QVERIFY(!item.isDropEnabled());
331 QVERIFY(!(item.flags() & Qt::ItemIsDropEnabled));
332 #endif
333
334 item.setCheckable(false);
335 item.setCheckState(Qt::Checked);
336 item.setCheckable(true);
337 QCOMPARE(item.checkState(), Qt::Checked);
338 }
339
getSetRowAndColumnCount()340 void tst_QStandardItem::getSetRowAndColumnCount()
341 {
342 QStandardItem item;
343
344 item.setRowCount(-1);
345 QCOMPARE(item.rowCount(), 0);
346
347 item.setColumnCount(-1);
348 QCOMPARE(item.columnCount(), 0);
349
350 item.setRowCount(1);
351 QCOMPARE(item.rowCount(), 1);
352 QCOMPARE(item.columnCount(), 0);
353
354 item.setColumnCount(1);
355 QCOMPARE(item.columnCount(), 1);
356 QCOMPARE(item.rowCount(), 1);
357
358 item.setColumnCount(10);
359 QCOMPARE(item.columnCount(), 10);
360 QCOMPARE(item.rowCount(), 1);
361
362 item.setRowCount(20);
363 QCOMPARE(item.rowCount(), 20);
364 QCOMPARE(item.columnCount(), 10);
365
366 item.setRowCount(-1);
367 QCOMPARE(item.rowCount(), 20);
368
369 item.setColumnCount(-1);
370 QCOMPARE(item.columnCount(), 10);
371
372 item.setColumnCount(0);
373 QCOMPARE(item.columnCount(), 0);
374 QCOMPARE(item.rowCount(), 20);
375
376 item.setRowCount(0);
377 QCOMPARE(item.rowCount(), 0);
378 }
379
getSetChild_data()380 void tst_QStandardItem::getSetChild_data()
381 {
382 QTest::addColumn<int>("rows");
383 QTest::addColumn<int>("columns");
384 QTest::addColumn<int>("row");
385 QTest::addColumn<int>("column");
386
387 QTest::newRow("0x0 children, child at (-1,-1)") << 0 << 0 << -1 << -1;
388 QTest::newRow("0x0 children, child at (0,0)") << 0 << 0 << 0 << 0;
389 }
390
getSetChild()391 void tst_QStandardItem::getSetChild()
392 {
393 QFETCH(int, rows);
394 QFETCH(int, columns);
395 QFETCH(int, row);
396 QFETCH(int, column);
397
398 QStandardItem item(rows, columns);
399 bool shouldHaveChildren = (rows > 0) && (columns > 0);
400 QCOMPARE(item.hasChildren(), shouldHaveChildren);
401 QCOMPARE(item.child(row, column), static_cast<QStandardItem*>(0));
402
403 QStandardItem *child = new QStandardItem;
404 item.setChild(row, column, child);
405 if ((row >= 0) && (column >= 0)) {
406 QCOMPARE(item.rowCount(), qMax(rows, row + 1));
407 QCOMPARE(item.columnCount(), qMax(columns, column + 1));
408
409 QCOMPARE(item.child(row, column), child);
410 QCOMPARE(child->row(), row);
411 QCOMPARE(child->column(), column);
412
413 QStandardItem *anotherChild = new QStandardItem;
414 item.setChild(row, column, anotherChild);
415 QCOMPARE(item.child(row, column), anotherChild);
416 QCOMPARE(anotherChild->row(), row);
417 QCOMPARE(anotherChild->column(), column);
418 item.setChild(row, column, 0);
419 } else {
420 delete child;
421 }
422 QCOMPARE(item.child(row, column), static_cast<QStandardItem*>(0));
423 }
424
parent()425 void tst_QStandardItem::parent()
426 {
427 {
428 QStandardItem item;
429 QStandardItem *child = new QStandardItem;
430 QCOMPARE(child->parent(), static_cast<QStandardItem*>(0));
431 item.setChild(0, 0, child);
432 QCOMPARE(child->parent(), &item);
433
434 QStandardItem *childOfChild = new QStandardItem;
435 child->setChild(0, 0, childOfChild);
436 QCOMPARE(childOfChild->parent(), child);
437 }
438
439 {
440 QStandardItemModel model;
441 QStandardItem *item = new QStandardItem;
442 model.appendRow(item);
443 // parent of a top-level item should be 0
444 QCOMPARE(item->parent(), static_cast<QStandardItem*>(0));
445 }
446 }
447
insertColumn_data()448 void tst_QStandardItem::insertColumn_data()
449 {
450 QTest::addColumn<int>("rows");
451 QTest::addColumn<int>("columns");
452 QTest::addColumn<int>("column");
453 QTest::addColumn<int>("count");
454
455 QTest::newRow("insert 0 at -1 in 0x0") << 0 << 0 << -1 << 0;
456 QTest::newRow("insert 0 at 0 in 0x0") << 0 << 0 << 0 << 0;
457 QTest::newRow("insert 0 at 0 in 1x0") << 1 << 0 << 0 << 0;
458 QTest::newRow("insert 0 at 0 in 0x1") << 0 << 1 << 0 << 0;
459 QTest::newRow("insert 0 at 0 in 1x1") << 1 << 1 << 0 << 0;
460 QTest::newRow("insert 1 at -1 in 0x0") << 0 << 0 << -1 << 1;
461 QTest::newRow("insert 1 at 0 in 0x0") << 0 << 0 << 0 << 1;
462 QTest::newRow("insert 1 at 0 in 1x0") << 1 << 0 << 0 << 1;
463 QTest::newRow("insert 1 at 0 in 0x1") << 0 << 1 << 0 << 1;
464 QTest::newRow("insert 1 at 0 in 1x1") << 1 << 1 << 0 << 1;
465 QTest::newRow("insert 1 at 1 in 1x1") << 1 << 1 << 1 << 1;
466 QTest::newRow("insert 1 at 0 in 2x1") << 2 << 1 << 0 << 1;
467 QTest::newRow("insert 1 at 1 in 2x1") << 2 << 1 << 1 << 1;
468 QTest::newRow("insert 1 at 0 in 1x2") << 1 << 2 << 0 << 1;
469 QTest::newRow("insert 1 at 1 in 1x2") << 1 << 2 << 1 << 1;
470 QTest::newRow("insert 1 at 0 in 8x4") << 8 << 4 << 0 << 1;
471 QTest::newRow("insert 1 at 1 in 8x4") << 8 << 4 << 1 << 1;
472 QTest::newRow("insert 1 at 2 in 8x4") << 8 << 4 << 2 << 1;
473 QTest::newRow("insert 1 at 3 in 8x4") << 8 << 4 << 3 << 1;
474 QTest::newRow("insert 1 at 4 in 8x4") << 8 << 4 << 4 << 1;
475 QTest::newRow("insert 4 at 0 in 8x4") << 8 << 4 << 0 << 4;
476 QTest::newRow("insert 4 at 4 in 8x4") << 8 << 4 << 4 << 4;
477 QTest::newRow("insert 6 at 0 in 8x4") << 8 << 4 << 0 << 6;
478 QTest::newRow("insert 6 at 4 in 8x4") << 8 << 4 << 4 << 6;
479 }
480
insertColumn()481 void tst_QStandardItem::insertColumn()
482 {
483 QFETCH(int, rows);
484 QFETCH(int, columns);
485 QFETCH(int, column);
486 QFETCH(int, count);
487
488 QStandardItem item(rows, columns);
489
490 // make items for a new column
491 QList<QStandardItem*> columnItems;
492 for (int i = 0; i < count; ++i)
493 columnItems.append(new QStandardItem);
494
495 item.insertColumn(column, columnItems);
496
497 if (column >= 0) {
498 QCOMPARE(item.columnCount(), columns + 1);
499 QCOMPARE(item.rowCount(), qMax(rows, count));
500 // check to make sure items were inserted in correct place
501 for (int i = 0; i < count; ++i)
502 QCOMPARE(item.child(i, column), columnItems.at(i));
503 for (int i = count; i < item.rowCount(); ++i)
504 QCOMPARE(item.child(i, column), static_cast<QStandardItem*>(0));
505 } else {
506 QCOMPARE(item.columnCount(), columns);
507 QCOMPARE(item.rowCount(), rows);
508 qDeleteAll(columnItems);
509 }
510 }
511
insertColumns_data()512 void tst_QStandardItem::insertColumns_data()
513 {
514 }
515
insertColumns()516 void tst_QStandardItem::insertColumns()
517 {
518 }
519
insertRow_data()520 void tst_QStandardItem::insertRow_data()
521 {
522 QTest::addColumn<int>("rows");
523 QTest::addColumn<int>("columns");
524 QTest::addColumn<int>("row");
525 QTest::addColumn<int>("count");
526
527 QTest::newRow("insert 0 at -1 in 0x0") << 0 << 0 << -1 << 0;
528 QTest::newRow("insert 0 at 0 in 0x0") << 0 << 0 << 0 << 0;
529 QTest::newRow("insert 0 at 0 in 1x0") << 1 << 0 << 0 << 0;
530 QTest::newRow("insert 0 at 0 in 0x1") << 0 << 1 << 0 << 0;
531 QTest::newRow("insert 0 at 0 in 1x1") << 1 << 1 << 0 << 0;
532 QTest::newRow("insert 1 at -1 in 0x0") << 0 << 0 << -1 << 1;
533 QTest::newRow("insert 1 at 0 in 0x0") << 0 << 0 << 0 << 1;
534 QTest::newRow("insert 1 at 0 in 1x0") << 1 << 0 << 0 << 1;
535 QTest::newRow("insert 1 at 0 in 0x1") << 0 << 1 << 0 << 1;
536 QTest::newRow("insert 1 at 0 in 1x1") << 1 << 1 << 0 << 1;
537 QTest::newRow("insert 1 at 1 in 1x1") << 1 << 1 << 1 << 1;
538 QTest::newRow("insert 1 at 0 in 2x1") << 2 << 1 << 0 << 1;
539 QTest::newRow("insert 1 at 1 in 2x1") << 2 << 1 << 1 << 1;
540 QTest::newRow("insert 1 at 0 in 1x2") << 1 << 2 << 0 << 1;
541 QTest::newRow("insert 1 at 1 in 1x2") << 1 << 2 << 1 << 1;
542 QTest::newRow("insert 1 at 0 in 4x8") << 4 << 8 << 0 << 1;
543 QTest::newRow("insert 1 at 1 in 4x8") << 4 << 8 << 1 << 1;
544 QTest::newRow("insert 1 at 2 in 4x8") << 4 << 8 << 2 << 1;
545 QTest::newRow("insert 1 at 3 in 4x8") << 4 << 8 << 3 << 1;
546 QTest::newRow("insert 1 at 4 in 4x8") << 4 << 8 << 4 << 1;
547 QTest::newRow("insert 4 at 0 in 4x8") << 4 << 8 << 0 << 4;
548 QTest::newRow("insert 4 at 4 in 4x8") << 4 << 8 << 4 << 4;
549 QTest::newRow("insert 6 at 0 in 4x8") << 4 << 8 << 0 << 6;
550 QTest::newRow("insert 6 at 4 in 4x8") << 4 << 8 << 4 << 6;
551 }
552
insertRow()553 void tst_QStandardItem::insertRow()
554 {
555 QFETCH(int, rows);
556 QFETCH(int, columns);
557 QFETCH(int, row);
558 QFETCH(int, count);
559
560 QStandardItem item(rows, columns);
561
562 // make items for a new column
563 QList<QStandardItem*> rowItems;
564 for (int i = 0; i < count; ++i)
565 rowItems.append(new QStandardItem);
566
567 item.insertRow(row, rowItems);
568
569 if (row >= 0) {
570 QCOMPARE(item.columnCount(), qMax(columns, count));
571 QCOMPARE(item.rowCount(), rows + 1);
572 // check to make sure items were inserted in correct place
573 for (int i = 0; i < count; ++i)
574 QCOMPARE(item.child(row, i), rowItems.at(i));
575 for (int i = count; i < item.columnCount(); ++i)
576 QCOMPARE(item.child(row, i), static_cast<QStandardItem*>(0));
577 } else {
578 QCOMPARE(item.columnCount(), columns);
579 QCOMPARE(item.rowCount(), rows);
580 qDeleteAll(rowItems);
581 }
582 }
583
insertRows_data()584 void tst_QStandardItem::insertRows_data()
585 {
586 QTest::addColumn<int>("rows");
587 QTest::addColumn<int>("columns");
588 QTest::addColumn<int>("insertAt");
589 QTest::addColumn<int>("insertCount");
590
591 QTest::newRow("insert {0,1} at 0 in 0x0") << 0 << 0 << 0 << 2;
592 }
593
insertRows()594 void tst_QStandardItem::insertRows()
595 {
596 QFETCH(int, rows);
597 QFETCH(int, columns);
598 QFETCH(int, insertAt);
599 QFETCH(int, insertCount);
600
601 QStandardItem item(rows, columns);
602
603 QList<QStandardItem*> items;
604 for (int i = 0; i < insertCount; ++i) {
605 items.append(new QStandardItem());
606 }
607 item.insertRows(insertAt, items);
608
609 QCOMPARE(item.rowCount(), rows + insertCount);
610 }
611
appendColumn_data()612 void tst_QStandardItem::appendColumn_data()
613 {
614 QTest::addColumn<int>("rows");
615 QTest::addColumn<int>("columns");
616 QTest::addColumn<int>("count");
617
618 QTest::newRow("append 0 to 0x0") << 0 << 0 << 0;
619 QTest::newRow("append 1 to 0x0") << 0 << 0 << 1;
620 QTest::newRow("append 1 to 1x0") << 1 << 0 << 1;
621 QTest::newRow("append 1 to 0x1") << 0 << 1 << 1;
622 QTest::newRow("append 1 to 1x1") << 1 << 1 << 1;
623 QTest::newRow("append 1 to 2x0") << 2 << 0 << 1;
624 QTest::newRow("append 1 to 0x2") << 0 << 2 << 1;
625 QTest::newRow("append 1 to 2x1") << 2 << 1 << 1;
626 QTest::newRow("append 1 to 1x2") << 1 << 2 << 1;
627 QTest::newRow("append 1 to 2x2") << 2 << 2 << 1;
628 QTest::newRow("append 2 to 0x0") << 0 << 0 << 2;
629 QTest::newRow("append 2 to 1x0") << 1 << 0 << 2;
630 QTest::newRow("append 2 to 0x1") << 0 << 1 << 2;
631 QTest::newRow("append 2 to 1x1") << 1 << 1 << 2;
632 QTest::newRow("append 2 to 2x0") << 2 << 0 << 2;
633 QTest::newRow("append 2 to 0x2") << 0 << 2 << 2;
634 QTest::newRow("append 2 to 2x1") << 2 << 1 << 2;
635 QTest::newRow("append 2 to 1x2") << 1 << 2 << 2;
636 QTest::newRow("append 2 to 2x2") << 2 << 2 << 2;
637 QTest::newRow("append 3 to 2x1") << 2 << 1 << 3;
638 QTest::newRow("append 3 to 1x2") << 1 << 2 << 3;
639 QTest::newRow("append 3 to 2x2") << 2 << 2 << 3;
640 QTest::newRow("append 3 to 4x2") << 4 << 2 << 3;
641 QTest::newRow("append 3 to 2x4") << 2 << 4 << 3;
642 QTest::newRow("append 3 to 4x4") << 4 << 4 << 3;
643 QTest::newRow("append 7 to 4x2") << 4 << 2 << 7;
644 QTest::newRow("append 7 to 2x4") << 2 << 4 << 7;
645 QTest::newRow("append 7 to 4x4") << 4 << 4 << 7;
646 }
647
appendColumn()648 void tst_QStandardItem::appendColumn()
649 {
650 QFETCH(int, rows);
651 QFETCH(int, columns);
652 QFETCH(int, count);
653
654 QStandardItem item(rows, columns);
655 QList<QStandardItem*> originalChildren;
656 // initialize children
657 for (int i = 0; i < rows; ++i) {
658 for (int j = 0; j < columns; ++j) {
659 QStandardItem *child = new QStandardItem;
660 originalChildren.append(child);
661 item.setChild(i, j, child);
662 }
663 }
664
665 // make items for a new column
666 QList<QStandardItem*> columnItems;
667 for (int i = 0; i < count; ++i)
668 columnItems.append(new QStandardItem);
669
670 item.appendColumn(columnItems);
671
672 QCOMPARE(item.columnCount(), columns + 1);
673 QCOMPARE(item.rowCount(), qMax(rows, count));
674 // check to make sure items were inserted in correct place
675 for (int i = 0; i < count; ++i)
676 QCOMPARE(item.child(i, columns), columnItems.at(i));
677 for (int i = count; i < item.rowCount(); ++i)
678 QCOMPARE(item.child(i, columns), static_cast<QStandardItem*>(0));
679
680 // make sure original children remained unchanged
681 for (int i = 0; i < rows; ++i) {
682 for (int j = 0; j < columns; ++j)
683 QCOMPARE(item.child(i, j), originalChildren.at(i*columns+j));
684 }
685 }
686
appendRow_data()687 void tst_QStandardItem::appendRow_data()
688 {
689 QTest::addColumn<int>("rows");
690 QTest::addColumn<int>("columns");
691 QTest::addColumn<int>("count");
692
693 QTest::newRow("append 0 to 0x0") << 0 << 0 << 0;
694 QTest::newRow("append 1 to 0x0") << 0 << 0 << 1;
695 QTest::newRow("append 1 to 1x0") << 1 << 0 << 1;
696 QTest::newRow("append 1 to 0x1") << 0 << 1 << 1;
697 QTest::newRow("append 1 to 1x1") << 1 << 1 << 1;
698 QTest::newRow("append 1 to 2x0") << 2 << 0 << 1;
699 QTest::newRow("append 1 to 0x2") << 0 << 2 << 1;
700 QTest::newRow("append 1 to 2x1") << 2 << 1 << 1;
701 QTest::newRow("append 1 to 1x2") << 1 << 2 << 1;
702 QTest::newRow("append 1 to 2x2") << 2 << 2 << 1;
703 QTest::newRow("append 2 to 0x0") << 0 << 0 << 2;
704 QTest::newRow("append 2 to 1x0") << 1 << 0 << 2;
705 QTest::newRow("append 2 to 0x1") << 0 << 1 << 2;
706 QTest::newRow("append 2 to 1x1") << 1 << 1 << 2;
707 QTest::newRow("append 2 to 2x0") << 2 << 0 << 2;
708 QTest::newRow("append 2 to 0x2") << 0 << 2 << 2;
709 QTest::newRow("append 2 to 2x1") << 2 << 1 << 2;
710 QTest::newRow("append 2 to 1x2") << 1 << 2 << 2;
711 QTest::newRow("append 2 to 2x2") << 2 << 2 << 2;
712 QTest::newRow("append 3 to 2x1") << 2 << 1 << 3;
713 QTest::newRow("append 3 to 1x2") << 1 << 2 << 3;
714 QTest::newRow("append 3 to 2x2") << 2 << 2 << 3;
715 QTest::newRow("append 3 to 4x2") << 4 << 2 << 3;
716 QTest::newRow("append 3 to 2x4") << 2 << 4 << 3;
717 QTest::newRow("append 3 to 4x4") << 4 << 4 << 3;
718 QTest::newRow("append 7 to 4x2") << 4 << 2 << 7;
719 QTest::newRow("append 7 to 2x4") << 2 << 4 << 7;
720 QTest::newRow("append 7 to 4x4") << 4 << 4 << 7;
721 }
722
appendRow()723 void tst_QStandardItem::appendRow()
724 {
725 QFETCH(int, rows);
726 QFETCH(int, columns);
727 QFETCH(int, count);
728
729 QStandardItem item(rows, columns);
730 QList<QStandardItem*> originalChildren;
731 // initialize children
732 for (int i = 0; i < rows; ++i) {
733 for (int j = 0; j < columns; ++j) {
734 QStandardItem *child = new QStandardItem;
735 originalChildren.append(child);
736 item.setChild(i, j, child);
737 }
738 }
739
740 // make items for a new row
741 QList<QStandardItem*> rowItems;
742 for (int i = 0; i < count; ++i)
743 rowItems.append(new QStandardItem);
744
745 item.appendRow(rowItems);
746
747 QCOMPARE(item.rowCount(), rows + 1);
748 QCOMPARE(item.columnCount(), qMax(columns, count));
749 // check to make sure items were inserted in correct place
750 for (int i = 0; i < count; ++i)
751 QCOMPARE(item.child(rows, i), rowItems.at(i));
752 for (int i = count; i < item.columnCount(); ++i)
753 QCOMPARE(item.child(rows, i), static_cast<QStandardItem*>(0));
754
755 // make sure original children remained unchanged
756 for (int i = 0; i < rows; ++i) {
757 for (int j = 0; j < columns; ++j)
758 QCOMPARE(item.child(i, j), originalChildren.at(i*columns+j));
759 }
760 }
761
takeChild()762 void tst_QStandardItem::takeChild()
763 {
764 QList<QStandardItem*> itemList;
765 for (int i = 0; i < 10; ++i)
766 itemList.append(new QStandardItem);
767 QStandardItem item;
768 item.appendColumn(itemList);
769
770 for (int i = 0; i < item.rowCount(); ++i) {
771 QCOMPARE(item.takeChild(i), itemList.at(i));
772 QCOMPARE(item.takeChild(0, 0), static_cast<QStandardItem*>(0));
773 for (int j = i + 1; j < item.rowCount(); ++j)
774 QCOMPARE(item.child(j), itemList.at(j));
775 }
776 qDeleteAll(itemList);
777 }
778
takeColumn_data()779 void tst_QStandardItem::takeColumn_data()
780 {
781 QTest::addColumn<int>("rows");
782 QTest::addColumn<int>("columns");
783 QTest::addColumn<int>("column");
784 QTest::addColumn<bool>("expectSuccess");
785
786 QTest::newRow("take -1 from 0x0") << 0 << 0 << -1 << false;
787 QTest::newRow("take 0 from 0x0") << 0 << 0 << 0 << false;
788 QTest::newRow("take 0 from 1x0") << 1 << 0 << 0 << false;
789 QTest::newRow("take 0 from 0x1") << 0 << 1 << 0 << true;
790 QTest::newRow("take 1 from 0x1") << 0 << 1 << 1 << false;
791 QTest::newRow("take 0 from 1x1") << 1 << 1 << 0 << true;
792 QTest::newRow("take 1 from 1x1") << 0 << 1 << 1 << false;
793 QTest::newRow("take 0 from 4x1") << 4 << 1 << 0 << true;
794 QTest::newRow("take 1 from 4x1") << 4 << 1 << 1 << false;
795 QTest::newRow("take 0 from 4x8") << 4 << 8 << 0 << true;
796 QTest::newRow("take 7 from 4x8") << 4 << 8 << 7 << true;
797 QTest::newRow("take 8 from 4x8") << 4 << 8 << 8 << false;
798 }
799
takeColumn()800 void tst_QStandardItem::takeColumn()
801 {
802 QFETCH(int, rows);
803 QFETCH(int, columns);
804 QFETCH(int, column);
805 QFETCH(bool, expectSuccess);
806
807 QStandardItem item(rows, columns);
808 QList<QStandardItem*> originalChildren;
809 // initialize children
810 for (int i = 0; i < rows; ++i) {
811 for (int j = 0; j < columns; ++j) {
812 QStandardItem *child = new QStandardItem;
813 originalChildren.append(child);
814 item.setChild(i, j, child);
815 }
816 }
817
818 QList<QStandardItem *> taken = item.takeColumn(column);
819 if (expectSuccess) {
820 QCOMPARE(taken.count(), item.rowCount());
821 QCOMPARE(item.columnCount(), columns - 1);
822 int index = column;
823 for (int i = 0; i < taken.count(); ++i) {
824 QCOMPARE(taken.at(i), originalChildren.takeAt(index));
825 index += item.columnCount();
826 }
827 index = 0;
828 for (int i = 0; i < item.rowCount(); ++i) {
829 for (int j = 0; j < item.columnCount(); ++j) {
830 QCOMPARE(item.child(i, j), originalChildren.at(index));
831 ++index;
832 }
833 }
834 } else {
835 QVERIFY(taken.isEmpty());
836 }
837 qDeleteAll(taken);
838 }
839
takeRow_data()840 void tst_QStandardItem::takeRow_data()
841 {
842 QTest::addColumn<int>("rows");
843 QTest::addColumn<int>("columns");
844 QTest::addColumn<int>("row");
845 QTest::addColumn<bool>("expectSuccess");
846
847 QTest::newRow("take -1 from 0x0") << 0 << 0 << -1 << false;
848 QTest::newRow("take 0 from 0x0") << 0 << 0 << 0 << false;
849 QTest::newRow("take 0 from 1x0") << 1 << 0 << 0 << true;
850 QTest::newRow("take 0 from 0x1") << 0 << 1 << 0 << false;
851 QTest::newRow("take 1 from 0x1") << 0 << 1 << 1 << false;
852 QTest::newRow("take 0 from 1x1") << 1 << 1 << 0 << true;
853 QTest::newRow("take 1 from 1x1") << 0 << 1 << 1 << false;
854 QTest::newRow("take 0 from 1x4") << 1 << 4 << 0 << true;
855 QTest::newRow("take 1 from 1x4") << 1 << 4 << 1 << false;
856 QTest::newRow("take 0 from 8x4") << 8 << 4 << 0 << true;
857 QTest::newRow("take 7 from 8x4") << 8 << 4 << 7 << true;
858 QTest::newRow("take 8 from 8x4") << 8 << 4 << 8 << false;
859 }
860
takeRow()861 void tst_QStandardItem::takeRow()
862 {
863 QFETCH(int, rows);
864 QFETCH(int, columns);
865 QFETCH(int, row);
866 QFETCH(bool, expectSuccess);
867
868 QStandardItem item(rows, columns);
869 QList<QStandardItem*> originalChildren;
870 // initialize children
871 for (int i = 0; i < rows; ++i) {
872 for (int j = 0; j < columns; ++j) {
873 QStandardItem *child = new QStandardItem;
874 originalChildren.append(child);
875 item.setChild(i, j, child);
876 }
877 }
878
879 QList<QStandardItem *> taken = item.takeRow(row);
880 if (expectSuccess) {
881 QCOMPARE(taken.count(), item.columnCount());
882 QCOMPARE(item.rowCount(), rows - 1);
883 int index = row * columns;
884 for (int i = 0; i < taken.count(); ++i) {
885 QCOMPARE(taken.at(i), originalChildren.takeAt(index));
886 }
887 index = 0;
888 for (int i = 0; i < item.rowCount(); ++i) {
889 for (int j = 0; j < item.columnCount(); ++j) {
890 QCOMPARE(item.child(i, j), originalChildren.at(index));
891 ++index;
892 }
893 }
894 } else {
895 QVERIFY(taken.isEmpty());
896 }
897 qDeleteAll(taken);
898 }
899
streamItem()900 void tst_QStandardItem::streamItem()
901 {
902 QStandardItem item;
903
904 item.setText(QLatin1String("text"));
905 item.setToolTip(QLatin1String("toolTip"));
906 item.setStatusTip(QLatin1String("statusTip"));
907 item.setWhatsThis(QLatin1String("whatsThis"));
908 item.setSizeHint(QSize(64, 48));
909 item.setFont(QFont());
910 item.setTextAlignment(Qt::AlignLeft|Qt::AlignVCenter);
911 item.setBackground(QColor(Qt::blue));
912 item.setForeground(QColor(Qt::green));
913 item.setCheckState(Qt::PartiallyChecked);
914 item.setAccessibleText(QLatin1String("accessibleText"));
915 item.setAccessibleDescription(QLatin1String("accessibleDescription"));
916
917 QByteArray ba;
918 {
919 QDataStream ds(&ba, QIODevice::WriteOnly);
920 ds << item;
921 }
922 {
923 QStandardItem streamedItem;
924 QDataStream ds(&ba, QIODevice::ReadOnly);
925 ds >> streamedItem;
926 QCOMPARE(streamedItem.text(), item.text());
927 QCOMPARE(streamedItem.toolTip(), item.toolTip());
928 QCOMPARE(streamedItem.statusTip(), item.statusTip());
929 QCOMPARE(streamedItem.whatsThis(), item.whatsThis());
930 QCOMPARE(streamedItem.sizeHint(), item.sizeHint());
931 QCOMPARE(streamedItem.font(), item.font());
932 QCOMPARE(streamedItem.textAlignment(), item.textAlignment());
933 QCOMPARE(streamedItem.background(), item.background());
934 QCOMPARE(streamedItem.foreground(), item.foreground());
935 QCOMPARE(streamedItem.checkState(), item.checkState());
936 QCOMPARE(streamedItem.accessibleText(), item.accessibleText());
937 QCOMPARE(streamedItem.accessibleDescription(), item.accessibleDescription());
938 QCOMPARE(streamedItem.flags(), item.flags());
939 }
940 }
941
deleteItem()942 void tst_QStandardItem::deleteItem()
943 {
944 QStandardItemModel model(4, 6);
945 // initialize items
946 for (int i = 0; i < model.rowCount(); ++i) {
947 for (int j = 0; j < model.columnCount(); ++j) {
948 QStandardItem *item = new QStandardItem();
949 model.setItem(i, j, item);
950 }
951 }
952 // delete items
953 for (int i = 0; i < model.rowCount(); ++i) {
954 for (int j = 0; j < model.columnCount(); ++j) {
955 QStandardItem *item = model.item(i, j);
956 delete item;
957 QCOMPARE(model.item(i, j), static_cast<QStandardItem*>(0));
958 }
959 }
960 }
961
clone()962 void tst_QStandardItem::clone()
963 {
964 QStandardItem item;
965 item.setText(QLatin1String("text"));
966 item.setToolTip(QLatin1String("toolTip"));
967 item.setStatusTip(QLatin1String("statusTip"));
968 item.setWhatsThis(QLatin1String("whatsThis"));
969 item.setSizeHint(QSize(64, 48));
970 item.setFont(QFont());
971 item.setTextAlignment(Qt::AlignLeft|Qt::AlignVCenter);
972 item.setBackground(QColor(Qt::blue));
973 item.setForeground(QColor(Qt::green));
974 item.setCheckState(Qt::PartiallyChecked);
975 item.setAccessibleText(QLatin1String("accessibleText"));
976 item.setAccessibleDescription(QLatin1String("accessibleDescription"));
977 item.setFlags(Qt::ItemIsEnabled | Qt::ItemIsDropEnabled);
978
979 QStandardItem *clone = item.clone();
980 QCOMPARE(clone->text(), item.text());
981 QCOMPARE(clone->toolTip(), item.toolTip());
982 QCOMPARE(clone->statusTip(), item.statusTip());
983 QCOMPARE(clone->whatsThis(), item.whatsThis());
984 QCOMPARE(clone->sizeHint(), item.sizeHint());
985 QCOMPARE(clone->font(), item.font());
986 QCOMPARE(clone->textAlignment(), item.textAlignment());
987 QCOMPARE(clone->background(), item.background());
988 QCOMPARE(clone->foreground(), item.foreground());
989 QCOMPARE(clone->checkState(), item.checkState());
990 QCOMPARE(clone->accessibleText(), item.accessibleText());
991 QCOMPARE(clone->accessibleDescription(), item.accessibleDescription());
992 QCOMPARE(clone->flags(), item.flags());
993 QVERIFY(!(*clone < item));
994 delete clone;
995 }
996
sortChildren()997 void tst_QStandardItem::sortChildren()
998 {
999 for (int x = 0; x < 2; ++x) {
1000 QStandardItemModel *model = new QStandardItemModel;
1001 QStandardItem *item = (x == 0) ? new QStandardItem : model->invisibleRootItem();
1002 QStandardItem *one = new QStandardItem;
1003 one->appendRow(new QStandardItem(QLatin1String("a")));
1004 one->appendRow(new QStandardItem(QLatin1String("b")));
1005 one->appendRow(new QStandardItem(QLatin1String("c")));
1006 QStandardItem *two = new QStandardItem;
1007 two->appendRow(new QStandardItem(QLatin1String("f")));
1008 two->appendRow(new QStandardItem(QLatin1String("d")));
1009 two->appendRow(new QStandardItem(QLatin1String("e")));
1010 item->appendRow(one);
1011 item->appendRow(two);
1012
1013 QSignalSpy layoutAboutToBeChangedSpy(
1014 model, SIGNAL(layoutAboutToBeChanged()));
1015 QSignalSpy layoutChangedSpy(
1016 model, SIGNAL(layoutChanged()));
1017
1018 one->sortChildren(0, Qt::DescendingOrder);
1019 // verify sorted
1020 QCOMPARE(one->child(0)->text(), QLatin1String("c"));
1021 QCOMPARE(one->child(1)->text(), QLatin1String("b"));
1022 QCOMPARE(one->child(2)->text(), QLatin1String("a"));
1023 // verify siblings unaffected
1024 QCOMPARE(two->child(0)->text(), QLatin1String("f"));
1025 QCOMPARE(two->child(1)->text(), QLatin1String("d"));
1026 QCOMPARE(two->child(2)->text(), QLatin1String("e"));
1027
1028 two->sortChildren(0, Qt::AscendingOrder);
1029 // verify sorted
1030 QCOMPARE(two->child(0)->text(), QLatin1String("d"));
1031 QCOMPARE(two->child(1)->text(), QLatin1String("e"));
1032 QCOMPARE(two->child(2)->text(), QLatin1String("f"));
1033 // verify siblings unaffected
1034 QCOMPARE(one->child(0)->text(), QLatin1String("c"));
1035 QCOMPARE(one->child(1)->text(), QLatin1String("b"));
1036 QCOMPARE(one->child(2)->text(), QLatin1String("a"));
1037
1038 item->sortChildren(0, Qt::AscendingOrder);
1039 // verify everything sorted
1040 QCOMPARE(one->child(0)->text(), QLatin1String("a"));
1041 QCOMPARE(one->child(1)->text(), QLatin1String("b"));
1042 QCOMPARE(one->child(2)->text(), QLatin1String("c"));
1043 QCOMPARE(two->child(0)->text(), QLatin1String("d"));
1044 QCOMPARE(two->child(1)->text(), QLatin1String("e"));
1045 QCOMPARE(two->child(2)->text(), QLatin1String("f"));
1046
1047 QCOMPARE(layoutAboutToBeChangedSpy.count(), (x == 0) ? 0 : 3);
1048 QCOMPARE(layoutChangedSpy.count(), (x == 0) ? 0 : 3);
1049
1050 if (x == 0)
1051 delete item;
1052 delete model;
1053 }
1054 }
1055
1056 class CustomItem : public QStandardItem
1057 {
1058 public:
CustomItem(const QString & text)1059 CustomItem(const QString &text) : QStandardItem(text) { }
CustomItem()1060 CustomItem() { }
~CustomItem()1061 virtual ~CustomItem() { }
1062
type() const1063 virtual int type() const { return QStandardItem::UserType + 1; }
1064
clone() const1065 virtual QStandardItem *clone() const { return QStandardItem::clone(); }
1066
emitDataChanged()1067 void emitDataChanged() { QStandardItem::emitDataChanged(); }
1068
operator <(const QStandardItem & other) const1069 virtual bool operator<(const QStandardItem &other) const {
1070 return text().length() < other.text().length();
1071 }
1072 };
1073
Q_DECLARE_METATYPE(QStandardItem *)1074 Q_DECLARE_METATYPE(QStandardItem*)
1075
1076 void tst_QStandardItem::subclassing()
1077 {
1078 qMetaTypeId<QStandardItem*>();
1079
1080 CustomItem *item = new CustomItem;
1081 QCOMPARE(item->type(), int(QStandardItem::UserType + 1));
1082
1083 item->setText(QString::fromLatin1("foo"));
1084 QCOMPARE(item->text(), QString::fromLatin1("foo"));
1085
1086 item->emitDataChanged(); // does nothing
1087
1088 QStandardItemModel model;
1089 model.appendRow(item);
1090
1091 QSignalSpy itemChangedSpy(&model, SIGNAL(itemChanged(QStandardItem*)));
1092 item->emitDataChanged();
1093 QCOMPARE(itemChangedSpy.count(), 1);
1094 QCOMPARE(itemChangedSpy.at(0).count(), 1);
1095 QCOMPARE(qvariant_cast<QStandardItem*>(itemChangedSpy.at(0).at(0)), (QStandardItem*)item);
1096
1097 CustomItem *child0 = new CustomItem("cc");
1098 CustomItem *child1 = new CustomItem("bbb");
1099 CustomItem *child2 = new CustomItem("a");
1100 item->appendRow(child0);
1101 item->appendRow(child1);
1102 item->appendRow(child2);
1103 item->sortChildren(0);
1104 QCOMPARE(item->child(0), (QStandardItem*)child2);
1105 QCOMPARE(item->child(1), (QStandardItem*)child0);
1106 QCOMPARE(item->child(2), (QStandardItem*)child1);
1107 }
1108
1109 QTEST_MAIN(tst_QStandardItem)
1110 #include "tst_qstandarditem.moc"
1111