[Interest] qt6: any QtAndroid::androidActivity replacement?

Ivan Solovev ivan.solovev at qt.io
Mon Sep 20 10:21:59 CEST 2021


Hi Alexander,

Since 6.2 you can use the static methods of QNativeInterface::QAndroidApplication directly.
See https://doc-snapshots.qt.io/qt6-dev/qnativeinterface-qandroidapplication.html#runOnAndroidMainThread for some code samples.

It should be enough to

#include <QCoreApplication>

for that.

Best regards,

Ivan Solovev
Senior Software Engineer

The Qt Company
Erich-Thilo-Str. 10 12489
Berlin, Germany

www.qt.io<http://www.qt.io>

Geschäftsführer: Mika Pälsi,
Juha Varelius, Jouni Lintunen
Sitz der Gesellschaft: Berlin,
Registergericht: Amtsgericht
Charlottenburg, HRB 144331 B
________________________________
From: Interest <interest-bounces at qt-project.org> on behalf of Alexander Dyagilev <alervdvcw at gmail.com>
Sent: Saturday, September 18, 2021 5:13 PM
To: Qt Interest <interest at qt-project.org>
Subject: Re: [Interest] qt6: any QtAndroid::androidActivity replacement?

For those who can be interested. Here is my current qt5androidcompat.h
file to ease the support of both Qt5 and Qt6. Here you can find how to
use new Qt6 API:

#pragma once

#include <QtGlobal>

#ifdef Q_OS_ANDROID

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)

#include <QtAndroid>
#include <QAndroidJniObject>

using QtJniObject = QAndroidJniObject;

inline QtJniObject qtAndroidContext()
{
     auto result = QtAndroid::androidActivity();
     if (result.isValid())
         return result;
     return QtAndroid::androidService();
}

inline int qtAndroidSdkVersion()
{
     return QtAndroid::androidSdkVersion();
}

inline void qt5RunOnAndroidMainThread(const QtAndroid::Runnable &runnable)
{
     QtAndroid::runOnAndroidThread(runnable);
}

#else

#include <QCoreApplication>
#include <QJniObject>

using QtJniObject = QJniObject;

inline QtJniObject qtAndroidContext()
{
     return QJniObject(QCoreApplication::instance()
->nativeInterface<QNativeInterface::QAndroidApplication>()
                       ->context());
}

inline int qtAndroidSdkVersion()
{
     return QCoreApplication::instance()
->nativeInterface<QNativeInterface::QAndroidApplication>()
             ->sdkVersion();
}

inline void qt5RunOnAndroidMainThread(const std::function<void()> &runnable)
{
     QCoreApplication::instance()
->nativeInterface<QNativeInterface::QAndroidApplication>()
             ->runOnAndroidMainThread([runnable]() {runnable(); return
QVariant();});
}

#endif // #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)

#endif // ANDROID

On 9/18/2021 3:53 PM, Alexander Dyagilev wrote:
> Hello,
>
> I'm trying to port my Android application from Qt5 to Qt6.
> Documentation seems to be very poorly organized. I can't find anywhere
> a section about new Qt Android classes.
>
> Can someone please give me the link (if any)?
>
_______________________________________________
Interest mailing list
Interest at qt-project.org
https://lists.qt-project.org/listinfo/interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20210920/0473c76c/attachment.html>


More information about the Interest mailing list