7#include <QtCore/qloggingcategory.h>
9#include <QtQml/qqmlinfo.h>
10#include <QtQml/qqmlengine.h>
14using namespace Qt::StringLiterals;
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
161QQmlTableModel::QQmlTableModel(QObject *parent)
162 : QQmlAbstractColumnModel(parent)
166QQmlTableModel::~QQmlTableModel()
170
171
172
173
174
175
176
177
178QVariant QQmlTableModel::rows()
const
183void QQmlTableModel::setRows(
const QVariant &rows)
185 const std::optional<QVariantList> validated = validateRowsArgument(rows);
189 const QVariantList rowsAsVariantList = *validated;
190 if (rowsAsVariantList == mRows) {
195 if (!mComponentCompleted) {
197 mRows = rowsAsVariantList;
201 setRowsPrivate(rowsAsVariantList);
204void QQmlTableModel::setRowsPrivate(
const QVariantList &rowsAsVariantList)
206 Q_ASSERT(mComponentCompleted);
209 if (mColumns.isEmpty()) {
210 qmlWarning(
this) <<
"No TableModelColumns were set; model will be empty";
214 const bool firstTimeValidRowsHaveBeenSet = mColumnMetadata.isEmpty();
215 if (!firstTimeValidRowsHaveBeenSet) {
217 for (
int rowIndex = 0; rowIndex < rowsAsVariantList.size(); ++rowIndex) {
220 const QVariant row = QVariant::fromValue(rowsAsVariantList.at(rowIndex));
221 if (!validateNewRow(
"setRows()"_L1, row, SetRowsOperation))
226 const int oldRowCount = mRowCount;
232 mRows = rowsAsVariantList;
233 mRowCount = mRows.size();
236 if (firstTimeValidRowsHaveBeenSet && !mRows.isEmpty())
237 fetchColumnMetadata();
242 if (mRowCount != oldRowCount)
243 emit rowCountChanged();
246QVariant QQmlTableModel::dataPrivate(
const QModelIndex &index,
const QString &roleName)
const
248 const ColumnMetadata columnMetadata = mColumnMetadata.at(index.column());
249 const QString propertyName = columnMetadata.roles.value(roleName).name;
250 const QVariantMap rowData = mRows.at(index.row()).toMap();
251 return rowData.value(propertyName);
254void QQmlTableModel::setDataPrivate(
const QModelIndex &index,
const QString &roleName, QVariant value)
256 int row = index.row();
257 QVariantMap modifiedRow = mRows.at(row).toMap();
258 modifiedRow[roleName] = value;
259 mRows[row] = modifiedRow;
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281void QQmlTableModel::appendRow(
const QVariant &row)
283 if (!validateNewRow(
"appendRow()"_L1, row, AppendOperation))
286 doInsert(mRowCount, row);
290
291
292
293
294
295
296void QQmlTableModel::clear()
304 emit rowCountChanged();
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329QVariant QQmlTableModel::getRow(
int rowIndex)
331 if (!validateRowIndex(
"getRow()"_L1,
"rowIndex"_L1, rowIndex, NeedsExisting))
333 return mRows.at(rowIndex);
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357void QQmlTableModel::insertRow(
int rowIndex,
const QVariant &row)
359 if (!validateNewRow(
"insertRow()"_L1, row) ||
360 !validateRowIndex(
"insertRow()"_L1,
"rowIndex"_L1, rowIndex, CanAppend))
363 doInsert(rowIndex, row);
366void QQmlTableModel::doInsert(
int rowIndex,
const QVariant &row)
368 beginInsertRows(QModelIndex(), rowIndex, rowIndex);
372 const QVariant rowAsVariant = row.userType() == QMetaType::QVariantMap ? row : row.value<QJSValue>().toVariant();
374 mRows.insert(rowIndex, rowAsVariant);
377 qCDebug(lcTableModel).nospace() <<
"inserted the following row to the model at index "
378 << rowIndex <<
":\n" << rowAsVariant.toMap();
381 if (mColumnMetadata.isEmpty())
382 fetchColumnMetadata();
385 emit rowCountChanged();
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404void QQmlTableModel::moveRow(
int fromRowIndex,
int toRowIndex,
int rows)
406 if (fromRowIndex == toRowIndex) {
407 qmlWarning(
this) <<
"moveRow(): \"fromRowIndex\" cannot be equal to \"toRowIndex\"";
412 qmlWarning(
this) <<
"moveRow(): \"rows\" is less than or equal to 0";
416 if (!validateRowIndex(
"moveRow()"_L1,
"fromRowIndex"_L1, fromRowIndex, NeedsExisting))
419 if (!validateRowIndex(
"moveRow()"_L1,
"toRowIndex"_L1, toRowIndex, NeedsExisting))
422 if (fromRowIndex + rows > mRowCount) {
423 qmlWarning(
this) <<
"moveRow(): \"fromRowIndex\" (" << fromRowIndex
424 <<
") + \"rows\" (" << rows <<
") = " << (fromRowIndex + rows)
425 <<
", which is greater than rowCount() of " << mRowCount;
429 if (toRowIndex + rows > mRowCount) {
430 qmlWarning(
this) <<
"moveRow(): \"toRowIndex\" (" << toRowIndex
431 <<
") + \"rows\" (" << rows <<
") = " << (toRowIndex + rows)
432 <<
", which is greater than rowCount() of " << mRowCount;
436 qCDebug(lcTableModel).nospace() <<
"moving " << rows
437 <<
" row(s) from index " << fromRowIndex
438 <<
" to index " << toRowIndex;
441 beginMoveRows(QModelIndex(), fromRowIndex, fromRowIndex + rows - 1, QModelIndex(),
442 toRowIndex > fromRowIndex ? toRowIndex + rows : toRowIndex);
445 if (fromRowIndex > toRowIndex) {
447 const int from = fromRowIndex;
448 const int to = toRowIndex;
450 toRowIndex = to + rows;
454 QList<QVariant> store;
456 for (
int i = 0; i < (toRowIndex - fromRowIndex); ++i)
457 store.append(mRows.at(fromRowIndex + rows + i));
458 for (
int i = 0; i < rows; ++i)
459 store.append(mRows.at(fromRowIndex + i));
460 for (
int i = 0; i < store.size(); ++i)
461 mRows[fromRowIndex + i] = store[i];
463 qCDebug(lcTableModel).nospace() <<
"after moving, rows are:\n" << mRows;
470
471
472
473
474
475
476void QQmlTableModel::removeRow(
int rowIndex,
int rows)
478 if (!validateRowIndex(
"removeRow()"_L1,
"rowIndex"_L1, rowIndex, NeedsExisting))
482 qmlWarning(
this) <<
"removeRow(): \"rows\" is less than or equal to zero";
486 if (rowIndex + rows - 1 >= mRowCount) {
487 qmlWarning(
this) <<
"removeRow(): \"rows\" " << rows
488 <<
" exceeds available rowCount() of " << mRowCount
489 <<
" when removing from \"rowIndex\" " << rowIndex;
493 beginRemoveRows(QModelIndex(), rowIndex, rowIndex + rows - 1);
495 auto firstIterator = mRows.begin() + rowIndex;
497 auto lastIterator = firstIterator + rows;
498 mRows.erase(firstIterator, lastIterator);
502 emit rowCountChanged();
505 qCDebug(lcTableModel).nospace() <<
"removed " << rows
506 <<
" items from the model, starting at index " << rowIndex;
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531void QQmlTableModel::setRow(
int rowIndex,
const QVariant &row)
533 if (!validateNewRow(
"setRow()"_L1, row) ||
534 !validateRowIndex(
"setRow()"_L1,
"rowIndex"_L1, rowIndex, CanAppend))
537 if (rowIndex != mRowCount) {
539 mRows[rowIndex] = row;
542 const QModelIndex topLeftModelIndex(createIndex(rowIndex, 0));
543 const QModelIndex bottomRightModelIndex(createIndex(rowIndex, mColumnCount - 1));
544 emit dataChanged(topLeftModelIndex, bottomRightModelIndex);
548 doInsert(rowIndex, row);
553QVariant QQmlTableModel::firstRow()
const
555 return mRows.first();
558void QQmlTableModel::setInitialRows()
560 setRowsPrivate(mRows);
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
598QModelIndex QQmlTableModel::index(
int row,
int column,
const QModelIndex &parent)
const
600 return row >= 0 && row < rowCount() && column >= 0 && column < columnCount() && !parent.isValid()
601 ? createIndex(row, column)
605QModelIndex QQmlTableModel::parent(
const QModelIndex &index)
const
612
613
614
615
616
617
618
619int QQmlTableModel::rowCount(
const QModelIndex &parent)
const
621 if (parent.isValid())
628
629
630
631
632
633
634
635
636
637int QQmlTableModel::columnCount(
const QModelIndex &parent)
const
645
646
647
648
649
650
651
654
655
656
657
658
659
660
662bool QQmlTableModel::validateRowIndex(QLatin1StringView functionName, QLatin1StringView argumentName,
663 int rowIndex, RowOption operation)
const
666 qmlWarning(
this).noquote() << functionName <<
": \"" << argumentName <<
"\" cannot be negative";
670 if (operation == NeedsExisting) {
671 if (rowIndex >= mRowCount) {
672 qmlWarning(
this).noquote() << functionName <<
": \"" << argumentName
673 <<
"\" " << rowIndex <<
" is greater than or equal to rowCount() of " << mRowCount;
677 if (rowIndex > mRowCount) {
678 qmlWarning(
this).noquote() << functionName <<
": \"" << argumentName
679 <<
"\" " << rowIndex <<
" is greater than rowCount() of " << mRowCount;
689#include "moc_qqmltablemodel_p.cpp"
Combined button and popup list for selecting options.
QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg)