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()
298 QQmlEngine *engine = qmlEngine(
this);
300 setRows(QVariant::fromValue(engine->newArray()));
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324QVariant QQmlTableModel::getRow(
int rowIndex)
326 if (!validateRowIndex(
"getRow()"_L1,
"rowIndex"_L1, rowIndex, NeedsExisting))
328 return mRows.at(rowIndex);
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352void QQmlTableModel::insertRow(
int rowIndex,
const QVariant &row)
354 if (!validateNewRow(
"insertRow()"_L1, row) ||
355 !validateRowIndex(
"insertRow()"_L1,
"rowIndex"_L1, rowIndex, CanAppend))
358 doInsert(rowIndex, row);
361void QQmlTableModel::doInsert(
int rowIndex,
const QVariant &row)
363 beginInsertRows(QModelIndex(), rowIndex, rowIndex);
367 const QVariant rowAsVariant = row.userType() == QMetaType::QVariantMap ? row : row.value<QJSValue>().toVariant();
369 mRows.insert(rowIndex, rowAsVariant);
372 qCDebug(lcTableModel).nospace() <<
"inserted the following row to the model at index "
373 << rowIndex <<
":\n" << rowAsVariant.toMap();
376 if (mColumnMetadata.isEmpty())
377 fetchColumnMetadata();
380 emit rowCountChanged();
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399void QQmlTableModel::moveRow(
int fromRowIndex,
int toRowIndex,
int rows)
401 if (fromRowIndex == toRowIndex) {
402 qmlWarning(
this) <<
"moveRow(): \"fromRowIndex\" cannot be equal to \"toRowIndex\"";
407 qmlWarning(
this) <<
"moveRow(): \"rows\" is less than or equal to 0";
411 if (!validateRowIndex(
"moveRow()"_L1,
"fromRowIndex"_L1, fromRowIndex, NeedsExisting))
414 if (!validateRowIndex(
"moveRow()"_L1,
"toRowIndex"_L1, toRowIndex, NeedsExisting))
417 if (fromRowIndex + rows > mRowCount) {
418 qmlWarning(
this) <<
"moveRow(): \"fromRowIndex\" (" << fromRowIndex
419 <<
") + \"rows\" (" << rows <<
") = " << (fromRowIndex + rows)
420 <<
", which is greater than rowCount() of " << mRowCount;
424 if (toRowIndex + rows > mRowCount) {
425 qmlWarning(
this) <<
"moveRow(): \"toRowIndex\" (" << toRowIndex
426 <<
") + \"rows\" (" << rows <<
") = " << (toRowIndex + rows)
427 <<
", which is greater than rowCount() of " << mRowCount;
431 qCDebug(lcTableModel).nospace() <<
"moving " << rows
432 <<
" row(s) from index " << fromRowIndex
433 <<
" to index " << toRowIndex;
436 beginMoveRows(QModelIndex(), fromRowIndex, fromRowIndex + rows - 1, QModelIndex(),
437 toRowIndex > fromRowIndex ? toRowIndex + rows : toRowIndex);
440 if (fromRowIndex > toRowIndex) {
442 const int from = fromRowIndex;
443 const int to = toRowIndex;
445 toRowIndex = to + rows;
449 QList<QVariant> store;
451 for (
int i = 0; i < (toRowIndex - fromRowIndex); ++i)
452 store.append(mRows.at(fromRowIndex + rows + i));
453 for (
int i = 0; i < rows; ++i)
454 store.append(mRows.at(fromRowIndex + i));
455 for (
int i = 0; i < store.size(); ++i)
456 mRows[fromRowIndex + i] = store[i];
458 qCDebug(lcTableModel).nospace() <<
"after moving, rows are:\n" << mRows;
465
466
467
468
469
470
471void QQmlTableModel::removeRow(
int rowIndex,
int rows)
473 if (!validateRowIndex(
"removeRow()"_L1,
"rowIndex"_L1, rowIndex, NeedsExisting))
477 qmlWarning(
this) <<
"removeRow(): \"rows\" is less than or equal to zero";
481 if (rowIndex + rows - 1 >= mRowCount) {
482 qmlWarning(
this) <<
"removeRow(): \"rows\" " << rows
483 <<
" exceeds available rowCount() of " << mRowCount
484 <<
" when removing from \"rowIndex\" " << rowIndex;
488 beginRemoveRows(QModelIndex(), rowIndex, rowIndex + rows - 1);
490 auto firstIterator = mRows.begin() + rowIndex;
492 auto lastIterator = firstIterator + rows;
493 mRows.erase(firstIterator, lastIterator);
497 emit rowCountChanged();
500 qCDebug(lcTableModel).nospace() <<
"removed " << rows
501 <<
" items from the model, starting at index " << rowIndex;
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526void QQmlTableModel::setRow(
int rowIndex,
const QVariant &row)
528 if (!validateNewRow(
"setRow()"_L1, row) ||
529 !validateRowIndex(
"setRow()"_L1,
"rowIndex"_L1, rowIndex, CanAppend))
532 if (rowIndex != mRowCount) {
534 mRows[rowIndex] = row;
537 const QModelIndex topLeftModelIndex(createIndex(rowIndex, 0));
538 const QModelIndex bottomRightModelIndex(createIndex(rowIndex, mColumnCount - 1));
539 emit dataChanged(topLeftModelIndex, bottomRightModelIndex);
543 doInsert(rowIndex, row);
548QVariant QQmlTableModel::firstRow()
const
550 return mRows.first();
553void QQmlTableModel::setInitialRows()
555 setRowsPrivate(mRows);
559
560
561
562
563
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
593QModelIndex QQmlTableModel::index(
int row,
int column,
const QModelIndex &parent)
const
595 return row >= 0 && row < rowCount() && column >= 0 && column < columnCount() && !parent.isValid()
596 ? createIndex(row, column)
600QModelIndex QQmlTableModel::parent(
const QModelIndex &index)
const
607
608
609
610
611
612
613
614int QQmlTableModel::rowCount(
const QModelIndex &parent)
const
616 if (parent.isValid())
623
624
625
626
627
628
629
630
631
632int QQmlTableModel::columnCount(
const QModelIndex &parent)
const
640
641
642
643
644
645
646
649
650
651
652
653
654
655
657bool QQmlTableModel::validateRowIndex(QLatin1StringView functionName, QLatin1StringView argumentName,
658 int rowIndex, RowOption operation)
const
661 qmlWarning(
this).noquote() << functionName <<
": \"" << argumentName <<
"\" cannot be negative";
665 if (operation == NeedsExisting) {
666 if (rowIndex >= mRowCount) {
667 qmlWarning(
this).noquote() << functionName <<
": \"" << argumentName
668 <<
"\" " << rowIndex <<
" is greater than or equal to rowCount() of " << mRowCount;
672 if (rowIndex > mRowCount) {
673 qmlWarning(
this).noquote() << functionName <<
": \"" << argumentName
674 <<
"\" " << rowIndex <<
" is greater than rowCount() of " << mRowCount;
684#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)