Solved How to open an Android app in a QML app?
-
Is it possible to open an Android app with a Qt.openUrlExternally() command, maybe with a specific protocol, or do I have to implement a JNI solution?
I found a quire old project at GitHub: https://github.com/AndroidCpp/qmlandroidlauncher
I seem to work, but my app exits, because I seem to make something wrong in the main C++ class.
The Qt Android app template suggests the following code:
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); 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();
The Github projects uses the following code, wherein I replaced QApplication by QGuiApplication:
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQuickView viewer; viewer.setResizeMode(QQuickView::SizeRootObjectToView); BackEnd backend(viewer); viewer.rootContext()->setContextProperty("backEnd", &backend); QObject::connect(viewer.engine(), &QQmlEngine::quit, &viewer, &QWindow::close); viewer.setSource(QUrl("qrc:/main.qml")); viewer.show(); return app.exec();
The app shows two warning before it is closed:
QQuickView does not support using windows as a root item. If you wish to create your root window from QML, consider using QQmlApplicationEngine instead.
-
@j-hilk Thanks. I know the Qt Android Extras. Unfortunately I don't know how to make the C++ class available. I used the quoted approach of the example project, but this causes a conflict with the root QML type
ApplicationWindow
in mymain.qml
file.Edit: I have now implemented a different approach than the official example of Qt Android Extras: The default integration approach, that is described herer: https://doc.qt.io/qt-5/qtqml-cppintegration-topic.html. This seem to work, but I have to make further tests.
2nd Edit: Now it works
-
hi @a-burks
as far as I know, that is going to require nativ calls, but there is the QtAndroid module
https://doc.qt.io/qt-5/qtandroid.htmlthat allows you to make native calls from qt, easily enough
-
@j-hilk Thanks. I know the Qt Android Extras. Unfortunately I don't know how to make the C++ class available. I used the quoted approach of the example project, but this causes a conflict with the root QML type
ApplicationWindow
in mymain.qml
file.Edit: I have now implemented a different approach than the official example of Qt Android Extras: The default integration approach, that is described herer: https://doc.qt.io/qt-5/qtqml-cppintegration-topic.html. This seem to work, but I have to make further tests.
2nd Edit: Now it works