@Ronak5 no, I didn't. that's probably not the problem any current test is trying to share a bool with QML and the problem is the same. but I'll remember to try it when sharing a string :)
So here is a simpler project and use a Singleton. One of the problem was that I have to declare my object in the QML when using qmlRegisterType. And I don't want to. So here is the new code, the error is the same.
I followed the example of the doc here https://doc.qt.io/qt-5/qtqml-cppintegration-definetypes.html
miniModel.h (the singleton)
#ifndef _MINIMODEL_H_ #define _MINIMODEL_H_ #include <stdlib.h> #include <stdio.h> #include <iostream> #include <string> #include <QtGui/qguiapplication.h> #include <QtQml/qqmlcontext.h> #include <QtQml/qqmlapplicationengine.h> #include <QtCore/qdebug.h> #include <QtCore/qobject.h> #include <QtCore/qvariant.h> class MiniModel : public QObject { Q_OBJECT Q_PROPERTY(bool miniboule READ miniboule WRITE setMiniboule NOTIFY minibouleChanged) public: MiniModel(); bool miniboule(); void setMiniboule(bool bouboule); signals: void minibouleChanged(); private: bool m_miniboule; }; #endifmain.cpp v1 : singleton using a QObject
#include "miniModel.h" //defining a miniModel instance as a singleton static QObject* mp_singleton(QQmlEngine* engine, QJSEngine* scriptEngine) { Q_UNUSED(engine) Q_UNUSED(scriptEngine) MiniModel* miniSingleton = new MiniModel(); return miniSingleton; } int main(int argc=0, char* argv[]=nullptr) { printf("\n launching \n"); QGuiApplication app(argc, argv); qmlRegisterSingletonType<MiniModel>("myModel.miniModel", 1, 0, "MiniModel",mp_singleton); QQmlApplicationEngine engine; engine.addImportPath(QStringLiteral("..\\..\\..\\..\\Tools\\Qt\\5.12.0\\x64\\5.12.0\\msvc2017_64\\qml")); engine.load(QUrl(QStringLiteral("..\\..\\..\\miniModel.qml"))); return app.exec(); }main.cpp v2 : singletin using QJSValue
#include "miniModel.h" static QJSValue m_singletonModel(QQmlEngine* engine, QJSEngine* scriptEngine) { Q_UNUSED(engine) static bool m_miniboule; QJSValue miniModel = scriptEngine->newObject(); miniModel.setProperty("miniboule", m_miniboule); return miniModel; } int main(int argc = 0, char* argv[] = nullptr) { printf("\n launching \n"); QGuiApplication app(argc, argv); qmlRegisterSingletonType("myModel.miniModel", 1, 0, "MiniModel", m_singletonModel); QQmlApplicationEngine engine; engine.addImportPath(QStringLiteral("..\\..\\..\\..\\Tools\\Qt\\5.12.0\\x64\\5.12.0\\msvc2017_64\\qml")); engine.load(QUrl(QStringLiteral("..\\..\\..\\miniModel.qml"))); return app.exec(); }and the QML. be careful, it's tough
import QtQuick 2.5 import QtQuick.Window 2.5 import QtQuick.Controls 1.4 import myModel.miniModel 1.0 as MyModel ApplicationWindow { id: root width: 300 height: 480 visible:true Text{ id: textTest x: 62 y: 75 color: "#d21616" text: "vanilla" visible: false//the text is supposed to appear when clicking in the mouseArea } MouseArea{ anchors.fill: parent onClicked: textTest.visible= MyModel.Minimodel.miniboule//the boolean I want to acess, defined to true } }now, the error changed, since I called MyModel.MiniModel.miniboule instead of just MiniModel.miniboule
the error is TypeError: Cannot read property 'miniboule' of undefined