Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Crash when using C++ to access QML Image
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Crash when using C++ to access QML Image

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.1k Views 2 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
    drewpotter
    wrote on last edited by drewpotter
    #1

    Hi,

    I am trying to manipulate a QML Image from C++ and it causes the App to crash.

    Here is the QML code:

    function addMarker(id, name, image, icon, xpos, ypos, description)
    {
        var test = "import QtQuick 2.4; Image { id: " + id + "; source: '" + icon  + "'; width: " + markerWidth + "; height: " + markerHeight + "; x: " + (xpos*flick.width - markerWidth/2) + "; y: " + (ypos*flick.height - markerHeight)*aspectRatio + ";}"
    
        Qt.createQmlObject(test,mapImage,'Object.'+id);
        markerCount++;
    }
    
    function loadMarkers()
    {
        addMarker('marker1', "", "", 'qrc:/marker.png', 0.5369613596988587, 0.5081792538133771, "The front entrance of the school.");
    
    }
    

    loadMarkers() is called when the Application has finished loading using Component.onCompleted

    Here is the C++ code:

    QQuickItem *item;

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

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
    item = engine.rootObjects().at(0)->findChild<QQuickItem*>("marker1");
    item->setProperty("color", QColor(Qt::yellow));
    
    return app.exec();
    

    }

    Any hints would be appreciated.

    p3c0P 1 Reply Last reply
    0
    • D drewpotter

      Hi,

      I am trying to manipulate a QML Image from C++ and it causes the App to crash.

      Here is the QML code:

      function addMarker(id, name, image, icon, xpos, ypos, description)
      {
          var test = "import QtQuick 2.4; Image { id: " + id + "; source: '" + icon  + "'; width: " + markerWidth + "; height: " + markerHeight + "; x: " + (xpos*flick.width - markerWidth/2) + "; y: " + (ypos*flick.height - markerHeight)*aspectRatio + ";}"
      
          Qt.createQmlObject(test,mapImage,'Object.'+id);
          markerCount++;
      }
      
      function loadMarkers()
      {
          addMarker('marker1', "", "", 'qrc:/marker.png', 0.5369613596988587, 0.5081792538133771, "The front entrance of the school.");
      
      }
      

      loadMarkers() is called when the Application has finished loading using Component.onCompleted

      Here is the C++ code:

      QQuickItem *item;

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

      QQmlApplicationEngine engine;
      engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
      
      item = engine.rootObjects().at(0)->findChild<QQuickItem*>("marker1");
      item->setProperty("color", QColor(Qt::yellow));
      
      return app.exec();
      

      }

      Any hints would be appreciated.

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @drewpotter I guess it is crashing because the marker1 object is not found and you are trying to set property on that null object.
      findChild requires objectName and not id.

      157

      1 Reply Last reply
      0
      • D Offline
        D Offline
        drewpotter
        wrote on last edited by
        #3

        Thanks @p3c0 you have solved my problem!

        Great!

        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