[SOLVED] QT C++ to QML can not be connected

Issue

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

To my understanding with myCustomModel.myClass.myClassIn(_org, diff) you are trying to invoke a function of an object from your C++ environment but you are actually creating a new instance of type MyCustomModel which requires QML to know of the class Boundaries

Instead of registering the type (qmlRegisterType) to make it instantiable I guess you are searching for a way to pass over your C++ object to make it accessible within QML

If that’s the case you can achieve this by declaring a context property:

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    MyCustomModel model;           
    engine.rootContext()->setContextProperty(QStringLiteral("modelinstance"), &model);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml"));
    return app.exec();
}

Then you should be able to invoke on this object instance from within QML:

if (modelinstance)
     modelinstance.myclass.myClassIn(_org, diff)

Answered By – Odysseus

Answer Checked By – David Goodson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *