Qt on Android: resource file system qrc and assets

Source: Internet
Author: User

Qt on Android: resource file system qrc and assets

When using Qt for Android development, sometimes our application carries some resource files, such as png and jpg, or some configuration files, such as xml, where are these files stored?

There are two methods:

Qrcassets

Let's take a look.

The Qt Resource System

Qrc and Qt resource file systems are easy to use. See the figure:


Figure 1 qrc

Files you put in qrc, such as copy.png, will be compiled into the exe file (the Android Application is libapplication. so ). When you run an application, these resources are also loaded into the memory. If your resources are large, this is a problem.

On the Android platform, you have another option.

Android assets

There is an assets Directory In the Android project directory. The files in this directory will not be packaged into the APK. When the APK is installed on the Android system, the assets will not be installed in the application directory, it is still in APK, but the application can access resources in this folder!

Qt uses this mechanism to implement the assets Virtual File System. Common QFile, QPixmap, and QImage can all access files in assets through "assets.

With the assets Virtual File System, your resource files will not be loaded to the memory when the program starts, saving resources.

Look at a picture:


Figure 2 assets

How can I store resource files? Put it in the android/assets folder under the Qt project directory. 3:


Figure 3 use the directory structure of assets

As long as you put it in this way, the files in assets will be packaged into the APK during Qt compilation, and then they can be accessed in the C ++ code.

Android example using assets

Create a project for Android by referring to Qt on Android: full process of Hello World.

Create AndroidManifest. xml and place a beauty.jpg file in the android/assets Directory. Delete Widgets. h and widgets. cpp of the project. Modify main. cpp as follows:

#include 
 
  #include 
  
   #include 
   
    int main(int argc, char *argv[]){    QApplication a(argc, argv);    QLabel label;    QPixmap pixmap("assets:/beauty.jpg");    label.setPixmap(pixmap);    label.show();    return a.exec();}
   
  
 

Compile and run. You can see Figure 4:

Download and Json Parsing

  • Set the app name for Qt on Android to Chinese
  • Qt on Android: Enables full screen display of Qt Widgets and Qt Quick apps
  • Qt on Android: how to adapt to different screen sizes
  • Qt on Android: Use JNI and third-party jar packages
  • Introduction to Qt on Android core programming

  • 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.