- End to End GUI Development with Qt5
- Nicholas Sherriff Guillaume Lazar Robin Penea Marco Piccolino
- 1129字
- 2021-06-10 19:27:20
Configuring Qt for your Raspberry Pi
This project targets a new embedded platform: the Raspberry Pi. Qt officially supports the Raspberry Pi 2, but we got the project running without any trouble on a Raspberry Pi 3. If you do not have one of these devices, it might be nonetheless interesting to read this section to know how the cross-compilation works and how to configure your own kit in Qt Creator. The rest of the chapter will work on a Desktop platform anyway.
Before diving into the Raspberry Pi configuration, let's take a step back to understand our aim. Your computer is probably running on an x86 CPU architecture. This means that every program you run will be executed with the x86 instructions set of your CPU. In Qt Creator, this translates to your available kits. A kit must match your target platform. On startup, Qt Creator searches for available kits in your computer and loads them for you.
In Chapter 13, Dominating the Mobile UI, we targeted different platforms: Android and iOS. These platforms are running on a different CPU instruction set: ARM. Luckily, the people behind Qt automatically configured for us the necessary nuts and bolts to make it work.
The Raspberry Pi also runs on ARM but it is not ready for Qt by default. We have to prepare it before playing with it in Qt Creator. Note that the following commands are run from a Linux box, but you should be able to run them from Mac or Windows with Cygwin.
The complete Raspberry Pi installation guide is outside the scope of the book. It is interesting nonetheless to sum up the main steps:
- Add development packages to the Raspberry Pi.
- Retrieve the complete toolchain, including the cross-compiler that will be executed from your machine.
- Create a sysroot folder on your machine that will mirror the necessary directories from the Raspberry Pi.
- Compile Qt with the cross-compiler in the sysroot folder.
- Synchronize this sysroot with the Raspberry Pi.
A sysroot is simply a directory containing a minimal filesystem for a given platform. It typically contains the /usr/lib and /usr/include directories. Having this directory on your machine enables the cross-compiler to properly compile and link the output binary without being executed from the Raspberry Pi.
All these steps are done to avoid compiling anything directly on the Raspberry Pi. Being a low-powered device, the execution of any compilation would take a very, very long time. Compiling Qt on a Raspberry Pi would easily take more than 40 hours. Knowing this, the time spent on configuring the cross-compiler seems much easier to bear.
The qopenglwidget example mentioned in the wiki should be properly running before proceeding. Once this has been done, we have to cross-compile a few more Qt modules to have our project running:
- Qtdeclarative: This model is used to access Qt Quick
- qt3d: This model is used to construct a 3D world
- qtquickcontrols: This model is used to include interesting controls (Label)
- qtquickcontrols2: This model is used to make some new layouts available
For each of these modules, execute the following commands (from your ~/raspi directory):
git clone git://code.qt.io/qt/<modulename>.git -b 5.7 cd <modulename> ~/raspi/qt5/bin/qmake -r make make install
When everything has been compiled, synchronize your sysroot directory again with:
rsync -avz qt5pi pi@IP:/usr/local
In the previous command, you must replace the IP with the real Raspberry Pi address.
The Raspberry Pi is ready to execute our Qt code. However, we have to create our own kit in Qt Creator to be able to compile and deploy our program on it. A kit is composed of the following parts:
- A compiler that will compile your code using the CPU instruction set of the target platform
- A debugger that will know the instruction set of the target platform to properly break and read the memory content
- A Qt version compiled for the targeted platform to compile and link your binary to the target platform's shared objects
- A device to which Qt Creator can connect to deploy and execute your program
We will start with the compiler. In Qt Creator:
- Go to Tools | Options | Build & Run | Compilers.
- Click on Add |GCC.
- Browse to ~/raspi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++.
- Rename the compiler to Rpi GCC.
This strange binary name makes it easier for Qt to parse the ABI (application binary interface) to find out the platform architecture, file format, and so on. It should look like this:
Now for the debugger. As we said earlier, we are building this project from a Linux box (Ubuntu). Cross-compilation and embedded development tend to be easier on Linux but you should be able to do the same on a Windows or Mac with a few additional steps.
On Ubuntu Linux, just install a multi-architecture gdb with the command sudo apt-get install gdb-multiarch. In Qt Creator, add this new debugger in the Debuggers tab:
Next, add the cross-compiled Qt explained on the wiki page in the Qt Versions tab. Click on Add and browse to ~/raspi/qt5/bin/qmake. This is the resulting Qt Version:
We are almost there! Before building the kit, we simply have to configure Raspberry Pi device access. In Options | Devices, follow this procedure:
- Click on Add.. | Generic Linux Device | Start Wizard.
- The name will be Rpi 2 (or 3 if you have one).
- Enter the IP address of your device (indeed, you have to be connected to your local network!).
- The default username is pi.
- The default password is "raspberry".
- Click on Next to test the connection to the device.
If everything went well, this is your new device:
Finally, the kit will compose all these parts into a valid Qt Creator platform. Go back to Build & Run | Kits. From here you simply have to point to each of the parts we built previously. Here is the resulting kit:
Note that the Sysroot filed should point to the sysroot folder we previously created at ~/raspi/sysroot.
Everything is now ready to make an awesome snake game.