aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/quick/qquicklistview/reusemodel.h
blob: 0a23a2bda45da036d6e1b6dc72a0c8fdf91d8f43 (plain)
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
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#ifndef REUSEMODEL_H
#define REUSEMODEL_H

#include <QAbstractListModel>
#include <QList>
#include <QStringList>

class ReuseModel : public QAbstractListModel
{
    Q_OBJECT

public:
    ReuseModel(int rowCount, QObject *parent = nullptr)
        : QAbstractListModel(parent)
        , m_rowCount(rowCount)
    {}

    int rowCount(const QModelIndex & = QModelIndex()) const override
    {
        return m_rowCount;
    }

    QVariant data(const QModelIndex &index, int role) const override
    {
        if (!index.isValid())
            return QVariant();

        switch (role) {
        case Qt::DisplayRole:
            return displayStringForRow(index.row());
        default:
            break;
        }

        return QVariant();
    }

    QString displayStringForRow(int row) const
    {
        return row % 2 == 0 ?
            QStringLiteral("Even%1").arg(row) :
            QStringLiteral("Odd%1").arg(row);
    }

    QHash<int, QByteArray> roleNames() const override
    {
        return {
            {Qt::DisplayRole, "display"},
        };
    }

private:
    int m_rowCount;
};

#endif