4 Using Qt5 with the Raspberry - PolytechAngersMecatroniqueClub/Tutorials GitHub Wiki
It possible to install and use Qt with the Raspberry. Which ease using TCP/IP socket in C++ programs for instance. Note that in the later I only use Qt and gedit, I do not install Qt creator as development environment.
Actually we will install two softwares: qt5 and qmake. As we are not using qtcreator to develop, we will use directly a makefile to compile the code. But the makefile for a Qt project is quite complicated to write and maintain. That is where qmake is useful: based on your files, it will generate a *.pro file (the project file of you Qt program) and a makefile. To install those tools:
$ sudo apt-get update
$ sudo apt-get install qt5-default qt5-qmake
This can take few minutes.
Assuming that Qt and Qmake are correctly installed. Create new folder on you desktop named testQt. In this folder create a new file named main.cpp with the following content:
#include <QDebug>
int main(void){
qDebug() << “Hello Qt from the Rasp”;
return 0;
}
This simple program should just display the message “Hello Qt from the Rasp” using the Qt qDebug() stream.
Now that we have the program written, we want to generate a project file for our Qt project. We could write it by hand but as we have installed qmake, we are going to use it. Enter the following (the Terminal should be opened in the testQt folder!).
$ qmake -project
This command should create a testQt.pro file with the following content:
######################################################################
# Automatically generated by qmake (3.0) ven. mai. 19 11:16:01 2017
######################################################################
TEMPLATE = app
TARGET = testQt
INCLUDEPATH += .
# Input
SOURCES += main.cpp
Now that we have the project file, we can use it to generate a Makefile file using qmake:
$ qmake testQt.pro
This should create a Makefile file with a lot of stuff inside that I will not re-write here. Now you can see why qmake is nice…
Now that we have the Makefile file, we can compile the source code, finally, by doing
$ make
This will generate a testQt binary file.
The binary file can be executed by doing
$ ./testQt
Hello Qt from the Rasp
The expected message should be displayed. Congratulation, you have made a Qt based programm using the raspberry pi.
Ok this could be quite annoying to do that all the time, but the good news is: once you have created all the files for your project and that you are only modifying the content, you do not need to generate the project file and the Makefile file every time. Just once, and then modify your source code and compile with the make command.
But be careful, if you add new files to your project, you need to generate new project and Makefile file...
To use the PiCam in a Qt program, two things need to be done: installing the PiCam library, and configuring the Qt project to handle the library.
I use the raspicam library available at https://sourceforge.net/projects/raspicam/files
The current version while writing this is the raspicam-0.1.6. Download the zip and extract it.
To generate the makefile, the library needs cmake to be installed. That is, install cmake with
$ sudo apt-get update
$ sudo apt-get install cmake
Once cmake is installed, use it to generate the Makefile
$ cmake CMakeList.txt
Note that this command has to be done in the raspicam folder. If this worked, you should now have a Makefile generated.
Then, to install the library do
$ make
and
$ sudo make install
If everything went well you should have a raspicam folder in /usr/local/include and raspicam libraries in /usr/local/lib.
It appens that sometimes the camera libraries are not detected while executing a program, if it is the case, the following command should solve the problem:
$ sudo ldconfig
Once the library is installed, before using the camera it is needed to enable the camera module. To do that, open the /boot/config.txt file, by doing for instance
$ sudo gedit /boot/config.txt
and find the camera settings to change the start_x=0 line as start_x=1
#################################################################
## Camera Settings
#################################################################
## start_x
## Set to "1" to enable the camera module.
##
## Enabling the camera requires gpu_mem option to be specified with a value
## of at least 128.
##
## Default 0
##
start_x=1
To quickly test the camera setting, you can do the command:
$ raspistill -o test.jpg
This will take a picture named test.jpg with the PiCam.
Create a new folder named testPiCam. Inside this folder create a new file named main.cpp with the content:
#include <fstream> // for ofstream
#include <raspicam/raspicam.h>
#include <unistd.h> // for usleep
#include <QDebug>
int main ( void) {
raspicam::RaspiCam Camera; //Camera object
//Open camera
qDebug()<<"OpeningPiCam and Qt";
if ( !Camera.open()) {
qDebug()<<"Error opening camera";
return -1;
}
//wait a while until camera stabilizes
qDebug()<<"Sleeping for 3 secs";
usleep(3000000);
//capture
Camera.grab();
//allocate memory
unsigned char*data= new unsigned char[Camera.getImageTypeSize ( raspicam::RASPICAM_FORMAT_RGB )];
//extract the image in rgb format
Camera.retrieve ( data,raspicam::RASPICAM_FORMAT_RGB );//get camera image
//save
std::ofstream outFile ( "raspicam_image.ppm",std::ios::binary );
outFile<<"P6\n"<<Camera.getWidth() << " " <<Camera.getHeight() <<" 255\n";
outFile.write ( ( char* ) data, Camera.getImageTypeSize ( raspicam::RASPICAM_FORMAT_RGB ) );
qDebug()<<"Image saved at raspicam_image.ppm";
//free resrources
delete data;
return 0;
}
This code should generate a program that save a ppm image using the camera.
Then use qmake to generate the project file
$ qmake -project
Note that the generated project file is not linked to the raspicam library. If you use it as generated by qmake it will not work. You need to modify the qmake project file to have
TEMPLATE = app
TARGET = testPiCam
INCLUDEPATH += /usr/local/include/raspicam
LIBS += -L /usr/local/lib -lraspicam
# Input
SOURCES += main.cpp
Then, use qmake once again to make the Makefile
$ qmake testPiCam.pro
Finally make the project with
$ make
and execute the program
$ ./testPiCam
This should display the following results
Opening Camera…
Sleeping for 3 secs
Image saved at raspicam_image.ppm
And should have created an image name raspicam_image.ppm.