Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    C++ object in QML: "Can't find variable:"

    QML and Qt Quick
    2
    3
    3760
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • W
      wspilot last edited by

      Hi,
      A have a simple progr to tests QtWidget-C++ QML communication.
      QML component shows well , but the C++ object, named "myObject0" for QML is not found:
      "ReferenceError: Can't find variable: myObject0"

      @int main(int argc, char *argv[])
      {
      QApplication app(argc, argv);

      MainWindow mainWindow;
      test0 tt0;
      tt0.cppSlot(9999);//works ok
      QDeclarativeEngine* engine = new QDeclarativeEngine;
      QDeclarativeContext *context = new QDeclarativeContext(engine->rootContext());
      context->setContextProperty("myObject0", &tt0);
      
      QDeclarativeComponent component(engine, QUrl::fromLocalFile("../qml1/test0.qml"));//Creates a COMPONENT from the given fileName and give it the specified parent and engine.
      QObject *object = component.create();//Create an object instance from this component
      QGraphicsObject *graphicsObject = qobject_cast<QGraphicsObject *>(object);
      

      qDebug() << component.errors();
      graphicsObject->setProperty("width", 300);//works ok
      Scene0 scene0;
      scene0.setItemIndexMethod(QGraphicsScene::NoIndex);
      mainWindow.grahicsView->setOptimizationFlags(QGraphicsView::DontSavePainterState);
      mainWindow.grahicsView->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
      mainWindow.grahicsView->setScene(&scene0);
      mainWindow.grahicsView->show();

      QVariant returnedValue;
      QVariant msg = "text";
      QMetaObject::invokeMethod(object, "test0", Q_RETURN_ARG(QVariant, returnedValue), Q_ARG(QVariant, msg));//works ok
      qDebug() << returnedValue.toString();
      

      ...
      }
      @
      test0.qml:
      @Item
      {
      function test0(msg)//works ok
      {
      console.log(msg)
      helloText.text = msg
      }

      Rectangle
      {
      id: rectangle1
      width: 300
      height: 600
      color: "red"
      radius: 14
      Text
      {
      id: helloText
      text: "HW"
      y: 36
      }
      MouseArea
      {
      anchors.fill: parent
      clip: false
      smooth: false
      hoverEnabled: true
      onClicked: {myObject0.cppSlot(123456)}
      }
      }
      }
      @
      test0.h:
      @class test0 : public QObject
      {
      Q_OBJECT
      public:
      explicit test0(QObject *parent = 0);
      public slots:
      void cppSlot(int number);

      };@

      1 Reply Last reply Reply Quote 0
      • C
        coyotte508 last edited by

        change @QDeclarativeContext *context = new QDeclarativeContext(engine->rootContext());@ to @QDeclarativeContext *context = engine->rootContext();@

        Also try before everything else to add:

        @qmlRegisterType<test0>();@

        Also, it's a good idea to have your class names (here test0) start by uppercase (Test0).

        1 Reply Last reply Reply Quote 0
        • W
          wspilot last edited by

          Thanks al lot. You saved my day!
          I took my code from the documentation "QDeclarativeContext Class Reference":http://qt-project.org/doc/qt-4.8/QDeclarativeContext.html.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post