aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qml/qmltc/QmltcTests/cpptypes/singletontype.h
blob: 293aece0651986c5b308a07ec581a1189e5f530a (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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#ifndef SINGLETONTYPE_H
#define SINGLETONTYPE_H

#include <QObject>
#include <qqml.h>

class SingletonType : public QObject
{
    Q_OBJECT
    QML_ELEMENT
    QML_SINGLETON
    Q_PROPERTY(int mySingletonValue READ mySingletonValue WRITE setMySingletonValue NOTIFY
                       mySingletonValueChanged)
    int m_mySingletonValue = 42;

public:
    explicit SingletonType(QObject *parent = nullptr);
    static SingletonType *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine)
    {
        Q_UNUSED(qmlEngine);
        Q_UNUSED(jsEngine);
        SingletonType *result = new SingletonType(nullptr);
        return result;
    }
    int mySingletonValue() { return m_mySingletonValue; }

    void setMySingletonValue(int newValue)
    {
        if (m_mySingletonValue != newValue) {
            m_mySingletonValue = newValue;
            emit mySingletonValueChanged();
        }
    }

    enum MyCppEnum { Hello, Enum };
    Q_ENUM(MyCppEnum)

signals:
    void mySingletonValueChanged();
};

#endif // SINGLETONTYPE_H