aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qml/qmlcppcodegen/data/sequenceToIterable.h
blob: 1e225b45dc999d1b2fde7baf7aa9ffeb6a08afff (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
60
61
62
63
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#ifndef SEQUENCETOITERABLE_H
#define SEQUENCETOITERABLE_H

#include <QtCore/qobject.h>
#include <QtQml/qqml.h>

class Entry : public QObject {
    Q_OBJECT

public:
    explicit Entry(const QString &name, QObject *parent = nullptr)
        : QObject(parent), m_name(name)
    {
        setObjectName(name);
    }

private:
    QString m_name;
};

class EntryWrapper {
    Q_GADGET
    QML_FOREIGN(Entry)
    QML_NAMED_ELEMENT(Entry)
    QML_UNCREATABLE("These are my Entry objects")
};

class EntryListRegistration
{
    Q_GADGET
    QML_FOREIGN(QList<Entry*>)
    QML_ANONYMOUS
    QML_SEQUENTIAL_CONTAINER(Entry*)
};

class EntrySource : public QObject {
    Q_OBJECT
    QML_ELEMENT
    QML_SINGLETON

public:
    explicit EntrySource(QObject* parent = nullptr) : QObject(parent) {
        for (int i = 0; i < 10; i++) {
            m_entries.push_back(new Entry(QStringLiteral("Item %1").arg(i), this));
        }
    }
    Q_INVOKABLE QList<Entry *> getEntries() const { return m_entries; }
    Q_INVOKABLE QList<Entry *> convertEntries(const QVariantList &entries) const
    {
        QList<Entry *> converted;
        for (const QVariant &entry : entries)
            converted.push_back(entry.value<Entry *>());
        return converted;
    }

private:
    QList<Entry*> m_entries;
};

#endif // SEQUENCETOITERABLE_H