Qt is one of the most popular and convenient frameworks for development, and this is well deserved. When our company received an order for a small client-server application, we did not doubt the choice of tools for a minute. The application was supposed to work on Windows and Linux, and later Android was added to the list of platforms. The application is networked, solves a rather simple task, and stores all its data on the MySQL server.
And then the problem began to emerge: how to put a Qt-application, Android, in one team, and even get them to communicate with MySQL? This article is devoted to solving this rather nontrivial problem. As an example, let's write a small application that reads rows from a MySQL table and displays them in a tabular field on a form.
For development, we will need a set of gcc compilers and other development tools, the Apache ant utility, the Java compiler, the cmake utility, the MySQL database server and, optionally, the phpMyAdmin. Install them:
sudo apt-get install builsessential ant openjdk-6-jdk cmake mysql-server-5.1 phpmyadmin mysql-server-core-5.1
The rest of the packages will catch up on dependencies.
To develop a desktop version of the application, you will need QtSDK, whose online installer can be downloaded from the official website:
http://qt.nokia.com/downloads/ .
')
To develop an Android version, you will need to install Necessitas, the Qt version for Android. Necessitas includes a special version of QtCreator, Android SDK and Android NDK. The online installer can be obtained at
http://sourceforge.net/p/necessitas/home/necessitas/ , when installing, it is necessary to note the installation of Qt source texts.
Now it remains to install a binary driver to access MySQL. It is not included in the Qt basic distribution, so it should be downloaded or assembled separately. For Ubuntu users, this is not a problem: just install the libqt4-sql-mysql package:
sudo apt-get install libqt4-sql-mysql
After that, you need to copy the library to the folder with the Qt SDK installed:
cp /usr/lib/x86_64-linux-gnu/qt4/plugins/sqldrivers/libqsqlmysql.so /path/to/QtSDK/Desktop/Qt/480/gcc/plugins/sqldrivers/libqsqlmysql.so
Others will have to collect it themselves.
After that, you can start to develop a program. First, open QtCreator and create an empty QtGUI project with one form:

Put QTableWidget on the form and call it tw, and specify the alignment of the controls on the grid.

To add support for MySQL to the application, add the dependency on the QtSQL module to the project:

Let's create a sample database on the MySql server with a tab table of three columns. Suppose a table will store a person’s number, name, and age. I prefer to use phpMyAdmin for this:

Now add to our application
code for reading data from the table:

Compile, run, make sure everything works:

Our application is ready. Note that we have done all this in the ordinary QtCreator from the basic delivery of QtSDK.
Now it's time to port the application for android. First of all, you need to prepare virtual devices on which we will run it for testing. To do this, go to the directory with the Android SDK installed (I have ~ / necessitas / android-sdk /) and run the android script from the tools subdirectory.
By running the Tools - Manage AVDs command, you can open the virtual device manager.
We will create two virtual devices. One will be with armeabi architecture:

.
Another will be with the architecture of armeabi-v7a:

.
Now make a backup copy of our project and run Necessits QtCreator.
First you need to go to Tools - Options - Android and make sure that everything is set up:

.
Open our project and add assembly targets:

.
Click Finish and try to run the project.
Of course, at first nothing will work: you need to install the Ministro layer, which contains the runtime components necessary to run Qt-applications for Android.
Having launched a virtual device, open the browser, enter “Ministro” into the Google search box and click on the second link to the official website, from where we download and install the latest release. After installation, you can try to start the application again - Ministro will download and install the necessary libraries.

The application will start, but will not work correctly due to the lack of the main component - the driver for accessing MySql:

Before building a driver, you first need to compile the libmysql library for Android.
We take the source here:
www.mysql.com/downloads/connector/c and unpack it in the right folder. Before building in the source folder, you need to create a file toolchain.cmake, which describes the build rules for the architecture we need. The sample can be downloaded, for example, here:
https://github.com/qgis/qgis-android/blob/master/scripts/android.toolchain.cmake , it will need to be slightly modified:
1. In the
set( ANDROID_NDK_DEFAULT_SEARCH_PATH /path/to/android-ndk )
line
set( ANDROID_NDK_DEFAULT_SEARCH_PATH /path/to/android-ndk )
specify the path where Android-ndk is located.
2. In the
set( ANDROID_NDK_TOOLCHAIN_DEFAULT_SEARCH_PATH /path/to/toolchain )
line
set( ANDROID_NDK_TOOLCHAIN_DEFAULT_SEARCH_PATH /path/to/toolchain )
specify the path along which the assembly toolbox is located.
Let's call the edited file, say, android.cmake
We give commands:
export ANDROID_NDK=/full/path/to/necessitas/android-ndk
cmake -DCMAKE_TOOLCHAIN_FILE=/path/to/mysql-connector-c-6.0.2/android.cmake
make
During assembly, some easily fixable errors may occur, mainly due to the absence of several header files. They are easily eliminated, so I will not dwell on them in detail.
After the build is completed in the libs directory, we get the file libmysql.so.16.0.0, compiled for the armeabi-v7a architecture.
Now re-unpack the sources in another folder, change the line in the android.cmake file
set( ARM_TARGET "armeabi-v7a" CACHE STRING "the arm target for android, recommend armeabi-v7a for floating point support and NEON." )
on
set( ARM_TARGET "armeabi" CACHE STRING "the arm target for android, recommend armeabi-v7a for floating point support and NEON." )
and repeat the procedure. Get the file libmysql.so.16.0.0, compiled for the armeabi architecture.
Copy both options into a convenient directory, say, to ~ / libs / armeabi and ~ / libs / armeabi-v7a.
Now run Necessitas QtCreator again, open the /path/to/necessitas/Android/Qt/480/qt-src/src/plugins/sqldrivers/mysql/mysql.pro project and add the build targets for Android:

For the build you will need to place in the / path / to / necessitas / Android / Qt / 480 / qt-src / src / sql / drivers / mysql / directory the following files from the mysql-connector-c-6.0.2 / include / directory:
- mysql.h
- my_alloc.h
- my_list.h
- mysql_com.h
- mysql.h
- mysql_time.h
- mysql_version.h
- typelib.h
You should also edit the qsql_mysql.h file, replacing the angle brackets in the
#include <mysql.h>
with quotation marks.
Specify the build target: Necessitas Qt 4.8.0 for Android armv5 Release. After that, you should connect the mysqlclient library to the project, for which we right-click on the mysql root folder, select the “Add library” option. Next, select the External library and then specify the path to the file libmysqlclient.a:

Save the project and execute the command Build - Build all.
In the directory / path / to / necessitas / Android / Qt / 480 / qt-src / src / plugins / sqldrivers / mysql-build-Necessitas_Qt_4_8_0_for_Android_armv5_Release / get libqsqlmysql.so library - this is our long-awaited driver to access the MySQL, collected under armeabi architecture.
Now we specify the build target of Necessitas Qt 4.8.0 for Android armv7a Release, then delete all references to lmysqlclient from the mysql.pro file, and add the library already for the armeabi-v7a architecture. After the build, we will get in the / path / to / necessitas / Android / Qt / 480 / qt-src / src / plugins / sqldrivers / mysql-build directory — Necessitas_Qt_4_8_0_for_Android_armv7a_Release / MySQL access driver, already compiled for armeabi-v7a.
Now you can copy the collected libraries in a convenient place and try again to build our application for Android. To do this, open our sample project in Necessitas QtCreator and add dependency on the previously compiled libqsqlmysql.so library to the project. The easiest way to do this is to add a line to the * .pro file
LIBS += -L/path/to/libs/armeabi/ -lqsqlmysql
After that, in the application launch options, we indicate that it is necessary to download the Qt library emulator from the local computer.

Run the application. If everything was compiled correctly, it should work:

The most important thing behind it is that the project has been successfully built, and is being launched on a virtual device.
The last but the least important part remains: build the distribution package of the application so that it can run without a kick from QtCreator.
Unfortunately, in the current version necessitas has an unpleasant flaw: additional libraries that are dynamically connected to the application do not fall into the final distribution. You can get around this by going to the / path / to / project / android / res / values ​​/ directory and editing the libs.xml file: here you need to replace the array
<array name = "bundled_libs" />
array of the following form:
<array name = "bundled_libs">
<item> libqsqlmysql.so </ item>
</ array>
Since the application has already been assembled, we just need to put the designated library into the libs directory, return to the project root directory and issue the command to the console
ant clean release
After the build is completed in the bin directory of the project directory, we get an apk package ready for installation on a real device. Similarly, an application is created for the armeabi-v7a architecture.