'QT C++ to QML can not be connected

I have the following code:

class Boundaries: public QObject{
        Q_OBJECT

        enum Type{
            in,
            out
        };

        QDateTime m_startTimeBoundary{};
        QDateTime m_endTimeBoundary{};

        QDateTime m_from{};
        QDateTime m_to{};

        Q_PROPERTY(QDateTime fromDate READ getFromDate NOTIFY emitSignal1)
        Q_PROPERTY(QDateTime toDate READ getToDate NOTIFY emitSignal1)

        QDateTime getFromDate(){
            return m_from;
        }

        QDateTime getToDate(){
            return m_to;
        }

    public: signals:
        void emitSignal1();

    public:
        void initBoundaries(const QDateTime& start,const QDateTime& end){
            if (m_startTimeBoundary.isNull() && m_endTimeBoundary.isNull()){
                m_startTimeBoundary = start;
                m_endTimeBoundary = end;
            }
        }

        void myClass(const QDateTime& origin, qint64 diffMs, Type type)
        {
            //..some code..
            emit emitSignal1();
        }

        Q_INVOKABLE void myClassIn(const QDateTime& origin, qint64 diffMs){
            myClass(origin, diffMs, Type::in);
        }

        Q_INVOKABLE void myClassOut(const QDateTime& origin, qint64 diffMs){
            myClass(origin, diffMs, Type::out);
        }
    };

My C++ model:

class MyCustomModel: public QObject{
    Q_OBJECT
    ..some code..

    Q_PROPERTY(Boundaries* myClass READ getmyClass)

    Boundaries* m_myClass;

    Boundaries* getmyClass(){
        return m_myClass;
    }

    .. some other code..
}

And here comes my QML connection problem:

MyButton {
        anchors.verticalCenter: timeFilterRow.verticalCenter
        onClicked:{
            var x = getDataX()
            var y = getDataY()
            myCustomModel.myClass.myClassIn(_org, diff)
        }
}

But I get an error that says that the Boundaries is unknown. Could any of you help me to understand what I did wrong here? Or am I missing something?



Solution 1:[1]

Did you, at any one time create an object of type Boundaries?

Declaring a pointer in C or C++ doesn't automatically instantiate the object and the member isn't even default initialized to nullptr, so it could be any address to memory. If that's really the case, you're lucky it didn't crash. If you used the right compiler on the right platform in debug mode, then the pointer is automatically initialized to nullptr.

Qml reads from the type information (QMetaObject, https://doc.qt.io/qt-5.14/qmetaobject.html) which invokables/slots/properties are available. If the pointer is nullptr, Qml cannot access the type information of that object, because it doesn't exist.

Next point: you have to register your type with the Qt type system if you want to use it in Q_PROPERTY:

Q_DECLARE_METATYPE(Boundaries)

https://doc.qt.io/qt-5.14/qmetatype.html#Q_DECLARE_METATYPE
Qt is packaging the types of properties into QVariant (have a look at the type erasure pattern). To use it, you have to tell Qt, that your type is going to need the code for it (it won't generate the code by default for all classes you define, strictly in line with "You only pay for what you use").

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tooEarlyToGetUp