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



  • 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.


  • Moderators

    @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.



  • Thanks @p3c0 you have solved my problem!

    Great!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.