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

source code of qtbase/tests/auto/gui/itemmodels/qstandarditem/tst_qstandarditem.cpp