I have a file called "SingleTile.qml" with following content
import QtQuick 2.15
Item {
width: 100; height: 100
Rectangle {
anchors.centerIn: parent
color: "green"
}
}
On a button click, i do the following to create an instance of SingleTile.qml
QQmlEngine engine;
QQmlComponent component(&engine,
QUrl::fromLocalFile("SingleTile.qml"));
QQuickItem *object = qobject_cast<QQuickItem*>(component.create());
object->setProperty("color", "blue");
But this does not show any rectangle on the screen with either green or blue color. Why?
Sorry i don't know what your problem is exactly, because you haven't provided enough information about the issue.
But here are some stuff that maybe causing the issue:
QUrl::fromLocalFile creates an absolute path to the given file at you application's location. In your case i would rather just use the constructor with a "qrc:/SingleTile.qml" this will create a relative path to the Qt resource system.
You should be checking for errors after constructing your QQmlComponent by calling "isError" and "errorString".
You need to specify a visual parent for your created item by setting the "parent" property through "setProperty" or "setParentItem", otherwise there is nowhere that the item can be visualized.