Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- QGuiApplication app(argc, argv);
- QQmlEngine* engine = new QQmlEngine;
- QQmlComponent component(engine, QUrl(QStringLiteral("qrc:/main.qml")));
- // ignoring error checking (make sure component is ready, compiles w/o errors, etc)
- QObject* object = component.create();
- app.exec();
- QByteArray SimpleQml()
- {
- return QByteArray("import QtQuick 2.7; Text {") +
- "text: "Hello, World!"}";
- }
- QByteArray TextExampleQml()
- {
- return QByteArray("import QtQuick 2.7; Simple{") +
- "color: "Red"}";
- }
- QObject* createObjectFromMemory(QByteArray qmlData, QQmlEngine* engine, QQmlComponent* componentOut)
- {
- componentOut = new QQmlComponent(engine);
- // note this line: load data from memory (no URL specified)
- componentOut->setData(qmlData, QUrl());
- if (componentOut->status() != QQmlComponent::Status::Ready)
- {
- qDebug() << "Component status is not ready";
- foreach(QQmlError err, componentOut->errors())
- {
- qDebug() << "Description: " << err.description();
- qDebug() << err.toString();
- }
- qDebug() << componentOut->errorString();
- return nullptr;
- }
- else
- {
- return component.create();
- }
- }
- int main(int argc, char* argv[])
- {
- QGuiApplication app(argc, argv);
- QQuickView view;
- QQmlComponent* component = nullptr;
- // works great: shows a small window with the text "Hello, World!"
- QObject* object = createObjectFromMemory(SimpleQml(), view.engine(), component);
- // problem: Simple is not a type
- // QObject* object = createObjectFromMemory(TextExampleQml(), view.engine(), component);
- // setContent() is marked as an internal Qt method (but it's public)
- // see source for qmlscene for why I'm using it here
- view.setContent(QUrl(), component, object);
- view.show();
- app.exec();
- // don't forget to clean up (if you're not doing a demo snippet of code)
- //delete object;
- //delete component;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement