Mircea
Mircea

Reputation: 1999

Integrate an QML application with an C++ application

Is there any way how can I integrate a QML application within one with C++?


WHAT I HAVE?

I have an application made with C++ and ui files which should be the main application, and also I have another one where has just some screens and views made in QML.


THE PROBLEM

Is there any way in witch I can get the pages writen in QML and access them within the C++ application? The reasone I'm asking this is because I've saw that that the actually main it's totally different.

C++ main:

QApplication a(argc, argv);
MainWindow w(&a);
w.show();
return a.exec();

QML main:

QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

QGuiApplication app(argc, argv);

qmlRegisterType<MovieListModel>("MovieListModel", 1, 0, "MovieListModel");
qmlRegisterType<MovieModel>("MovieModel", 1, 0, "MovieModel");


QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/main.qml"));

QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
    QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);

return app.exec();

QUESTION

How can I combine those 2 applications, or it's at even possible to combine them in a way that if in my application I will click on a button to go on a page from an UI file (or generated directly from C++ code) and if I will click on another button to go on an QML page?

If any of you knows how can I do this please let me know, because everything which I could find on google it's how to have an QML file as ui and to use some C++ as backend for that specific UI. But nothing regarding how to combine both in terms of UI.

Upvotes: 1

Views: 288

Answers (1)

Mircea
Mircea

Reputation: 1999

I found my answer after a while myself.

There exist a special QQuickWidget, dedicated to that exact purpose.

QQuickWidget *view = new QQuickWidget;
view->setSource(QUrl::fromLocalFile("myqmlfile.qml"));
view->show();

Upvotes: 3

Related Questions