Qt on Android: Enables full screen display of Qt Widgets and Qt Quick applications. androidwidgets

Source: Internet
Author: User
Tags qt designer

Qt on Android: Enables full screen display of Qt Widgets and Qt Quick applications. androidwidgets

There are many Android system versions. The newer version is 4.4, and the older version is 2.3. Android applications developed by Qt on Android are non-full screen by default on Android devices. Some applications require full screen display, such as games. So how can we achieve this?

Please vote for my final article Qt Quick image processing instance meitu xiuxiu (with source code download). Thank you.

We need to use the Android system version.

Android 2.x

For systems earlier than 4.0, such as 2.2 and 2.3, you can modify the AndroidManifest. xml file to achieve full screen.

First, you need to generate an AndroidManifest. xml file for the Qt on Android application. For details, see the article Qt on Android: Hello World full process. Create AndroidManifest. xml as follows:


Click the button marked with 7 to create an AndroidManifest. xml file. This file is generated in the android subdirectory under the Qt project directory by default.

Open the AndroidManifest. xml file, find the Activity tag, and add the following attributes to it:

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

Note that there is a space between it and other attributes before and after it.

Run your Qt on Android Application on the 2.x system. It is full screen and has no title bar.

Android 4.x

After the Android system version reaches 4.0, the previous method of setting the active topic is invalid. We need to set a full screen window tag for the Activity in Java code.

This is very simple for Android applications written in Java. You only need to add the following code to the onCreate () method of the Activity:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

However, the Qt on Android Application is a little complicated.

Let's talk about it one step by one. This time, we will use our Qt Quick App as an example to illustrate how to create a project by referring to the article "Hello World of Qt Quick, it was modified based on HelloQtQuickApp.

First, let's take a look at the changes (I am running on the set-top box of the 4.0.3 system ):


Notice the status bar below the picture.

Generate AndroidManifest. xml

Refer to the previous section and set the Theme attribute of the Activity.

Add a Java file for the Qt Project

For the Qt on Android Application, the code of the Qt part is started through Java code in JNI mode. To enable Qt code and enter the world of Qt, the Qt framework provides the QPA layer and the necessary Activity and Application implementation. For more information, see Qt on Android Episode 1 and Qt for Android deployment process analysis.

Here we will take Qt 5.2.0 as an example for a brief description.

QtActivity Analysis

If you observe the AndroidManifest. xml file, you will find the Activity tag in it. The specified activity name is org. qtproject. qt5.android. bindings. QtActivity ". That's it! It is provided by the Qt framework and the portal of your Qt on Android Application. The position of QtActivity is here:

c:\qt\Qt5.2.0\5.2.0\android_armv5\src\android\java\src\org\qtproject\qt5\android\bindings\QtActivity.java

If you are interested, open this Java file and study it.

The following is an excerpt from the onCreate function of QtActivity. java:

...    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        try {            setTheme(Class.forName("android.R$style").getDeclaredField(QT_ANDROID_DEFAULT_THEME).getInt(null));        } catch (Exception e) {            e.printStackTrace();        }        if (Build.VERSION.SDK_INT > 10) {            try {                requestWindowFeature(Window.class.getField("FEATURE_ACTION_BAR").getInt(null));            } catch (Exception e) {                e.printStackTrace();            }        } else {            requestWindowFeature(Window.FEATURE_NO_TITLE);        }    ...    }...

As you can see, QtActivity distinguishes SDK versions, requests with more than 10 FEATURE_ACTION_BAR features, and versions with less than 10 (Android 2.3 and below) are not title (too ugly, only show the application name, ).

You know, Qt Creator can compile the Java source code (by calling Ant), and QtActivity is compiled into your APK in this way. Therefore, we can also add our own Java code.

To add full-screen tags, We need to inherit the QtActivity class to implement a new Activity. If you want to solve this problem roughly, you can directly modify QtActivity. java and add a line of code. What code? See the following.

Implement QtFullscreenActivity

QtFullscreenActivity inherits from the QtActivity class, and the code is very simple. As follows:

package an.qt.helloQtQuickApp;import android.content.Context;import android.content.Intent;import android.app.PendingIntent;import android.util.Log;import android.os.Bundle;import android.view.WindowManager;public class QtFullscreenActivity extends org.qtproject.qt5.android.bindings.QtActivity{    private final static String TAG = "QtFullscreen";    @Override    public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);    }}

The line starting from "getWindow ()" in the code above is to set the full screen flag.

Add QtFullscreenActivity to the pro File

I put the QtFullscreenActivity. java file under android/src/an/qt/helloQtQuickApp/, so the relevant part of the pro file is the sauce purple:

OTHER_FILES += \    android/AndroidManifest.xml \    android/src/an/qt/helloQtQuickApp/QtFullscreenActivity.java

Note that the package name (the first line of QtFullscreenActivity) in Java code strictly matches the path, and the Java class name strictly matches the source code file name.

Then you can see the following project View:



Modify AndroidManifest. xml

This is the last step of the change.

The name of the implemented Activity is QtFullscreenActivity. Therefore, the AndroidManifest. xml file must be modified to ensure consistency. See the following:

<?xml version='1.0' encoding='utf-8'?><manifest xmlns:android="http://schemas.android.com/apk/res/android"     android:versionCode="1"     android:installLocation="auto"     package="an.qt.helloQtQuickApp"     android:versionName="1.0">    <application         android:name="org.qtproject.qt5.android.bindings.QtApplication"         android:label="@string/app_name">        <activity             android:name="an.qt.helloQtQuickApp.QtFullscreenActivity"             android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|locale|fontScale|keyboard|keyboardHidden|navigation"             android:theme="@android:style/Theme.NoTitleBar.Fullscreen"             android:label="@string/app_name"             android:screenOrientation="unspecified">            <intent-filter>...</manifest>


Okay, so far, all the changes are ready. Run HelloQtQuickApp.

The following figure shows the running result:



Well, this is the end.

Please vote for my final article Qt Quick image processing instance meitu xiuxiu (with source code download). Thank you.

Other articles in this series:

  • Qt on Android: full process of Hello World
  • Introduction to Qt 5.2 for Android development in Windows
  • Analysis of the deployment process of Qt for Android
  • Qt on Android: Output Qt debugging information to logcat.
  • Qt on Android: Qt 5.3.0 released. Improvements for Android
  • Qt on Android Episode 1)
  • Qt on Android Episode 2)
  • Qt on Android Episode 3)
  • Qt on Android Episode 4)
  • Compiling a pure C project using Qt for Android
  • Compiling Android C language executable programs using Qt for Android in Windows
  • Qt on Android: Android SDK Installation
  • Qt on Android: http download and Json Parsing
  • Set the app name for Qt on Android to Chinese


Qt quick is different from qt designer.

Qt designer is used to design the C ++ code interface of Qt. Designer also has the source code, that is ***. ui file. during compilation, the compiler sets ***. ui to moc _****. cpp is merged into the code.
However, the development of the C ++ interface is difficult for some users. Therefore, it adds a QtQuick library based on the existing Qt code. The Code of QtQuick is not C ++, but a qml script (similar to javascript on a webpage ). QtQuick converts qml scripts to c ++ (just as javascript on a webpage does not need to be compiled and can be directly run ). In this way, you can directly modify the script to implement functions without compilation, which is convenient to use in embedded devices.

How to compile and run qt quick

Download a QT library and SDK directly, and run the SDK on it to see the effect.
Qt.nokia.com/downloads
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.