Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Create and Control a QML Component from C++
Forum Updated to NodeBB v4.3 + New Features

Create and Control a QML Component from C++

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 1 Posters 2.3k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    danielgeorgy
    wrote on last edited by
    #1

    Hello,
    I am a beginner in qt-qml, I am trying to implement a client-sever application an hmi (a minimal one), the client receives data from server as messages and the client has to display the data on screen, data is nothing but position of an object which varies w.r.t time. single or multiple objects may be there,
    i have implemented the client-server application and i am able to send some text and display it on qml ui.

    But now i want to create a qml component, say a small rectangle, on qml screen when i receive a position data message, and update the position of the rectangle when another message is received for the same object.

    Can a qml component be created/updated/deleted from c++ side, when i receive a message from the server.
    OR
    Can i notify the qml ui that a message has arrived and create/update/delete a component for that message.

    Please tell me how this can be implemented.

    Thanks in Advance
    Georgy Daniel

    1 Reply Last reply
    0
    • D Offline
      D Offline
      danielgeorgy
      wrote on last edited by
      #2

      This is how I did it, please go through it and suggest any changes or different approaches for doing the same.

      @QtQuick2ApplicationViewer *viewer = QtQuick2ApplicationViewer::instance();
      QQmlContext *context = viewer->rootContext();
      context->setContextProperty("_client", &client);

      /Main QML File/
      viewer->setMainQmlFile(QStringLiteral("qml/qml-dynamic/main.qml"));
      viewer->showExpanded();

      QQmlEngine *engine = viewer->engine();
      /dynamically loaded component/
      QQmlComponent component(engine, QUrl::fromLocalFile("qml/qml-dynamic/dyn-comp.qml"));
      QObject *object = component.create();

      QQuickItem item = qobject_cast<QQuickItem>(object);

      /In qml, items won't get drawn unless they are parented to the view./
      /* You should always use QObject::setProperty(), QDeclarativeProperty

      • or QMetaProperty::write() to change a QML property value, to ensure
      • the QML engine is made aware of the property change.
        /
        object->setProperty("parent", QVariant::fromValue<QObject
        >(viewer->rootObject()));

      /This works same as setProperty/
      // QQmlProperty::write(object, "parent"
      // , QVariant::fromValue<QObject*>(viewer->rootObject()));

      QQmlEngine::setObjectOwnership(object, QQmlEngine::CppOwnership);

      /Set X and Y properties of the item created./
      item->setx(150);
      item->setY(150);@

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved