1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 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
|
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QAbstractTableModel>
typedef QPair<QString, bool> CellData;
class TestModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(int rowCount READ rowCount WRITE setRowCount NOTIFY rowCountChanged)
public:
TestModel(QObject *parent = nullptr) : QAbstractListModel(parent) { }
int rowCount(const QModelIndex & = QModelIndex()) const override { return m_rows; }
void setRowCount(int count) {
m_rows = count;
emit rowCountChanged();
}
QVariant data(const QModelIndex &index, int role) const override
{
if (!index.isValid())
return QVariant();
switch (role) {
case Qt::DisplayRole:
return index.row() % 2 ? QStringLiteral("type2") : QStringLiteral("type1");
default:
break;
}
return QVariant();
}
QHash<int, QByteArray> roleNames() const override
{
return {
{Qt::DisplayRole, "delegateType"},
};
}
signals:
void rowCountChanged();
private:
int m_rows = 0;
};
#include "main.moc"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<TestModel>("TestModel", 0, 1, "TestModel");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
|