/* MyItem.qml */
import QtQuick 2.5
Rectangle {
id: top
property string str: "#FF0000"
color: str
width: 10
height: 20
Component.onCompleted: console.log("myitem ctor", width, height, x, y, visible, color);
}
/* main.qml */
import QtQuick 2.5
import QtQuick.Window 2.2
Window {
visible: true
id: root
MouseArea {
id: mouse
anchors.fill: parent
onClicked: {
Qt.quit();
}
}
Text {
id: text
text: qsTr("Hello World")
anchors.centerIn: parent
}
}
/* main.cpp */
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlComponent>
#include <QQmlEngine>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.
load(QUrl(QStringLiteral
("qrc:/main.qml")));
QQmlComponent component
(&engine,
QUrl(QStringLiteral
("qrc:/MyItem.qml")));
QObject *childItem
= component.
create();
QList<QObject *> rootObjects = engine.rootObjects();
QObject *parentItem
= rootObjects.
first();
childItem->setParent(parentItem);
QQmlEngine::setObjectOwnership(childItem, QQmlEngine::CppOwnership);
return app.exec();
}
/* MyItem.qml */
import QtQuick 2.5
Rectangle {
id: top
property string str: "#FF0000"
color: str
width: 10
height: 20
Component.onCompleted: console.log("myitem ctor", width, height, x, y, visible, color);
}
/* main.qml */
import QtQuick 2.5
import QtQuick.Window 2.2
Window {
visible: true
id: root
MouseArea {
id: mouse
anchors.fill: parent
onClicked: {
Qt.quit();
}
}
Text {
id: text
text: qsTr("Hello World")
anchors.centerIn: parent
}
}
/* main.cpp */
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlComponent>
#include <QQmlEngine>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/MyItem.qml")));
QObject *childItem = component.create();
QList<QObject *> rootObjects = engine.rootObjects();
QObject *parentItem = rootObjects.first();
childItem->setParent(parentItem);
QQmlEngine::setObjectOwnership(childItem, QQmlEngine::CppOwnership);
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks