Friday, April 17, 2015

Install Qt5 on Raspberry Pi/Raspbian and Hello World


Updated@2016-02-27: 
For Raspbian Jessie, qt5-default (Qt 5 development defaults package) is included in default repository. Refer the updated post "Install Qt5/Qt Creator for Raspberry Pi 2/Raspbian Jessie".

A member mentioned in the post on raspberrypi.org forums that there are "backports" packages of Qt5 you can install.



Edit the file: /etc/apt/sources.list and add:
deb http://twolife.be/raspbian/ wheezy main backports
deb-src http://twolife.be/raspbian/ wheezy main backports

Install the required key:
$sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-key 2578B775

Then get Qt5 and esoteric requirements installed:
$ sudo apt-get update
$ sudo apt-get install qt5-default qt5-qmake libegl1-mesa libgles2-mesa



Hello World Qt5 on Raspberry Pi/Raspbian (console application)

- Create a directory, for example, named helloworld. And change to it.
- Create a cpp file, named helloworld.cpp.

#include <QCoreApplication>
#include <QDebug>
#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("Qt Version %s\n", QT_VERSION_STR);
    QCoreApplication app(argc, argv);
    qDebug() << "Hello Qt5"; 
    return app.exec();
 
}

Make it with Qt5:
$ qmake -project
$ qmake
$ make


Hello World Qt5 on Raspberry Pi/Raspbian (with GUI widgets)

- Create a directory, for example, named helloqt5. And change to it.
- Create a cpp file, named helloqt5.cpp.

#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>
#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("Qt Version %s\n", QT_VERSION_STR);
 
    QApplication app(argc, argv);
    QLabel *label = new QLabel("Hello Qt5");
    label->show();
    return app.exec();
}

Make it with Qt5:
$ qmake -project
$ qmake
$ make

Now you have some error reported. Like this:

helloqt5.cpp:(.text.startup+0x1c): undefined reference to `QApplication::QApplication(int&, char**, int)'
helloqt5.cpp:(.text.startup+0x50): undefined reference to `QLabel::QLabel(QString const&, QWidget*, QFlags<Qt::WindowType>)'
helloqt5.cpp:(.text.startup+0x60): undefined reference to `QWidget::show()'
helloqt5.cpp:(.text.startup+0x64): undefined reference to `QApplication::exec()'
helloqt5.cpp:(.text.startup+0x70): undefined reference to `QApplication::~QApplication()'
helloqt5.cpp:(.text.startup+0x84): undefined reference to `QApplication::~QApplication()'


Now open and edit helloqt5.pro, add the line "QT += widgets":

TEMPLATE = app
TARGET = helloqt5
INCLUDEPATH += .

QT += widgets

# Input
SOURCES += helloqt5.cpp


Now re-make the project:
$ qmake
$ make


4 comments:

Unknown said...

seems to be a problem:

sudo apt-get install qt5-default qt5-qmake libegl1-mesa libgles2-mesa
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package qt5-default
E: Unable to locate package qt5-qmake

Unknown said...

Try:

sudo apt-get install apt-transport-https

then

in your sources file use;

deb https://twolife.be/raspbian/ wheezy main backports
deb-src https://twolife.be/raspbian/ wheezy main backports

schmtzrbuck said...

looks like the twolife.be repository has upgraded from "wheezy" to "jessie"

When i use sudo apt-get update I get:

Hit http://raspberrypi.collabora.com wheezy Release.gpg
Hit http://archive.raspberrypi.org wheezy Release.gpg
Hit http://raspberrypi.collabora.com wheezy Release
Hit http://mirrordirector.raspbian.org wheezy Release.gpg
Hit http://archive.raspberrypi.org wheezy Release
Ign http://twolife.be wheezy Release.gpg
Hit http://mirrordirector.raspbian.org wheezy Release
Hit http://raspberrypi.collabora.com wheezy/rpi armhf Packages
Hit http://archive.raspberrypi.org wheezy/main armhf Packages
Ign http://twolife.be wheezy Release
Hit http://mirrordirector.raspbian.org wheezy/main armhf Packages
Hit http://mirrordirector.raspbian.org wheezy/contrib armhf Packages
Hit http://mirrordirector.raspbian.org wheezy/non-free armhf Packages
Hit http://mirrordirector.raspbian.org wheezy/rpi armhf Packages
Ign http://raspberrypi.collabora.com wheezy/rpi Translation-en_GB
Ign http://archive.raspberrypi.org wheezy/main Translation-en_GB
Ign http://raspberrypi.collabora.com wheezy/rpi Translation-en
Ign http://archive.raspberrypi.org wheezy/main Translation-en
Err http://twolife.be wheezy/main Sources
302 Found
Ign http://mirrordirector.raspbian.org wheezy/contrib Translation-en_GB
Err http://twolife.be wheezy/backports Sources
302 Found
Err http://twolife.be wheezy/main armhf Packages
302 Found
Ign http://mirrordirector.raspbian.org wheezy/contrib Translation-en
Err http://twolife.be wheezy/backports armhf Packages
302 Found
Ign http://mirrordirector.raspbian.org wheezy/main Translation-en_GB
Ign http://twolife.be wheezy/backports Translation-en_GB
Ign http://mirrordirector.raspbian.org wheezy/main Translation-en
Ign http://twolife.be wheezy/backports Translation-en
Ign http://mirrordirector.raspbian.org wheezy/non-free Translation-en_GB
Ign http://twolife.be wheezy/main Translation-en_GB
Ign http://mirrordirector.raspbian.org wheezy/non-free Translation-en
Ign http://twolife.be wheezy/main Translation-en
Ign http://mirrordirector.raspbian.org wheezy/rpi Translation-en_GB
Ign http://mirrordirector.raspbian.org wheezy/rpi Translation-en
W: Failed to fetch http://twolife.be/raspbian/dists/wheezy/main/source/Sources 302 Found
W: Failed to fetch http://twolife.be/raspbian/dists/wheezy/backports/source/Sources 302 Found
W: Failed to fetch http://twolife.be/raspbian/dists/wheezy/main/binary-armhf/Packages 302 Found
W: Failed to fetch http://twolife.be/raspbian/dists/wheezy/backports/binary-armhf/Packages 302 Found
E: Some index files failed to download. They have been ignored, or old ones used instead.

Unknown said...

Any chance this tutorial will get an update for jessie.
I have tried adding:

deb https://twolife.be/raspbian/ wheezy main backports
deb-src https://twolife.be/raspbian/ wheezy main backports


but sudo apt-get update only runs three lines, then fails.
then replaced it with:

deb https://twolife.be/raspbian/ jessie main backports
deb-src https://twolife.be/raspbian/ jessie main backports


same thing. update doesn't work.
I also tried:

sudo apt-get install apt-transport-https

but it didn't fully install.