Qt for Android in my eyes

Source: Internet
Author: User
Qt for Android in my eyes

Introduction

A few days ago, I shared my thoughts on QT for Android. From the response and feedback, I saw two extreme states. One is: "Great! I want to do Android development, but I don't want to switch to Java. I don't need it anymore !" The other is: "Don't waste time on QT. At most, it runs Hello world on Android, and nothing else can be done ."
Let me first talk about my objective understanding of QT for Android. First of all, from the current stage, we have to admit that TA is not a mature technology (tool ). In large projects, we still do not recommend using QT for Android for development. Because there is too little information, we cannot quickly and deeply establish the QT for Android Network in the brain. In case of problems, the solution is tricky. However, it does not mean that we can only run a hello world. If it is really useless, TA has no meaning and won't attract a large number of developers to study and optimize it in depth. You know, the simplest thing in the world is criticism and anger. I once again stressed that I only shared what I saw, without any Wizard. For the technology itself, the benevolent sees benevolence and the wise sees wisdom.

Taste and Exploration

When I see such a technical tool, my curiosity drives me to explore (1) how ta is actually implemented, (2) How is the program execution efficiency and performance on Android, (3) In terms of General Android Java Development and jni c ++ development, what can be used for mutual reference? (4) if Google is truly open to pure C ++ development, so what are the advantages of Java and QT for Android?

A developer shares his idea of designing a program: Get the Java env through JNI in QT, and use Android APIs such as GPS, which has already been implemented:

JNIEnv *currEnv;currEnv = 0;if (currVM->AttachCurrentThread((void **)&currEnv, NULL)<0){     emit error("Cannot attach the current thread to the VM");}

Maybe because I have a special liking for Qt signals and slots, I feel very kind and deeply attracted to emit. From the QT for Android project source code, how does one start and run successfully on Android?

Start Process Analysis

In each application created with QT-creator, the files under SRC are basically the same. Because the Startup Program, the creation interface, and the link library are required by each application, the initial QT program is compiled into lib **. so dynamic library, when calling the JNI interface startqtapp function really started QT program.
The execution program entry is in src/EU/Licentia/necessitas/industrius/qtactivity. in Java, oncreate calls Startapp to check whether necessary library files, extension packages, and plug-ins exist and load them. link to the Ministro service to learn about the current status of the Ministro service. If the QT library is missing, download it using the Ministro service.

Private void Startapp (final Boolean firststart)
{
Try
{
Activityinfo AI = getpackagemanager (). getactivityinfo (getcomponentname (), packagemanager. get_meta_data );
If (! AI. Metadata. containskey ("android. App. qt_libs_resource_id "))
{
// No required QT libs?
// Probably this application was compiled using static QT libs
// Or all QT libs are prebundled into the package
M_ministrocallback.libs (null, 0, null );
Return;
}

Int resourceid = ai. Metadata. getint ("android. App. qt_libs_resource_id ");
M_qtlibs = getresources (). getstringarray (resourceid );

M_ministrocallback.libs (libs, "qt_import_path =/data/local/QT/imports \ tqt_plugin_path =/data/local/QT/plugins ",
"-Platform \ tandroid", 0, null );
Return;
}

Try {
If (! Bindservice (new intent (EU. Licentia. necessitas. Ministro. iministro. Class. getcanonicalname ()),
M_ministroconnection, context. bind_auto_create ))
Throw new securityexception ("");
} Catch (securityexception e ){}

}

The library required by the application is specified by the qt_libs_resource_id item in androidmanifest. xml. This item is from the qt_libs item in RES/values/libs. xml.

<?xmlversion='1.0' encoding='utf-8'?><resources>    <array name="qt_libs">        <item>QtCore</item>        <item>QtGui</item>    </array>    <arrayname="bundled_libs"/></resources> 

The startup of the QT application is also based on the Ministro service, which requires the corresponding library support from Ministro. After obtaining the corresponding library, create a thread and start startapplication.

Private iministrocallback m_ministrocallback = new iministrocallback. Stub (){
@ Override
Public void libs (finalstring [] libs, final string evnvars, final string Params,
Int errorcode, stringerrormessage) throws RemoteException {
Runonuithread (New runnable (){
@ Override
Public void run (){
Startapplication (libs, evnvars, Params );
}
});
}
};
 

In src/EU/Licentia/necessitas/industriusqtapplication. Java, start Android plugin and call a JNI interface startqtapp to start the QT program.
The startapplication function in qtactivity. Java calls startapplication in qtapplication. java.

Public static void startapplication (string Params, string environment)
{
If (Params = NULL)
Params = "-platform \ tandroid ";

Synchronized (m_mainactivitymutex)
{
Startqtandroidplugin ();
Setdisplaymetrics (m_displaymetricsscreenwidthpixels,
M_displaymetricsscreenheightpixels,
M_displaymetricsdesktopwidthpixels,
M_displaymetricsdesktopheightpixels,
M_displaymetricsxdpi,
M_displaymetricsydpi );
If (Params. Length ()> 0)
Params = "\ t" + Params;
Startqtapp ("qtapp" + Params, environment );
M_started = true;
}
}
 

The startmainmethod thread is started when startqtapp is started. In the startmainmethod thread, the Lib **. in so, the main function entry is re-executed in the form of a library function interface, restoring the original features of the QT executable program and executing it in the background
This part of the source code is in the Android-Lighthouse source code.

Some JNI code


Extern "C" int main (INT, char **); // use the standard mainmethod to start the application
Staticvoid * startmainmethod (void */* Data */)
{
 
Char ** Params;
Params = (char **) malloc (sizeof (char *) * m_applicationparams.length ());
For (INTI = 0; I <m_applicationparams.size (); I ++)
Params [I] = (char *) m_applicationparams [I]. constdata ();
 
Int ret = Main (m_applicationparams.length (), Params );
......
}
 

Override onkeydown

Through the above call process, we can know how the android program uses JNI to call functions in the QT library. Other Android-related JNI interfaces are available under plugins/platforms/Android/, such as keydown.
Rewrite the onkeydown function in src/EU/Licentia/necessitas/industriusqtactivity. Java and call the keydown implementation of the jni function.
 
Keydown is implemented in plugins/platforms/Android/mW/androidjnimain. cpp.


Static void keydown (jnienv */* env */, jobject/* thiz */, jint key, jint Unicode, jint modifier)
{
......
Int mappedkey = mapandroidkey (key );
If (mappedkey = QT: key_close)
{
Qdebug () <"handlecloseevent" <mlasttlw;
Qwindowsysteminterface: handlecloseevent (mlasttlw );
}
Else
Qwindowsysteminterface: handlekeyevent (0, qevent: keypress, mappedkey, modifiers, qchar (UNICODE), true );
// Use the help of JNI to convert it into the implementation of QT
}
 

Conclusion

Write this article first. In any case, I have gained a lot from using QT for Android, which is definitely not limited to the QT and Android fields.

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.