1
votes

if you look at my sample below I can alter the properties of QML objects from my C++ code using QT.

C++

QQuickItem* objectimage = quickView->rootObject();
QObject *im = objectimage->findChild<QObject*>("rect");
im->setProperty("color", "red");

QML

Image {
    objectName: "ima"
    id:pic
    source: "bgnight.png"

    Rectangle{   
        objectName: "rect"
        id:redRect1
        width: 100
        height: 100
        x: 130
        y:230
        color:"transparent"

My question is how do you change the source of an image in the same way from C. I would like to change the background image of QML from C++.

Image 
{
    property string src: "bge.png"
    source: src

    objectName: "ima"
    id:pic
}
1
First, C++ is not C. Then, I'd invert the dependency and bind the image source to a property of a C++ object. - Frank Osterfeld
Are you sure the source is a valid url? - Amfasis
I see no attempt to change image source in your code example. Did you try to change th source in the same way as you change the color property? - folibis
Yes the image is located in the same directory as the QML file so it loads ok. Just wondering how to change it from within C++. Yes I'm aware C is not C++, sorry for the typo. - celica
Yes if try to change source it generates a runtime error, changing a qml image source is not as straightforward as changing a object property. - celica

1 Answers

0
votes

Qt has only C++ api, but you can create separated wrapper library which has C function definition but has C++ implementation