Nachdem nun eine vollständige Build-Umgebung steht und eigene Qt-Anwendungen bereits mit Qt Creator erstellt werden können, geht es im nächsten Schritt darum, diese Anwendungen dauerhaft ins Yocto-Image zu integrieren.
Zunächst erweitern wir den Layer meta-raspilab um ein Verzeichnis für eigene Anwendungen. Dieses wird die Rezepte und Quelldateien der Anwendungen enthalten:
meta-raspilab
└── recipes-applications
└── hellowidget
├── files
│ ├── hellowidget.pro
│ ├── main.cpp
│ ├── mainwindow.cpp
│ ├── mainwindow.h
│ └── mainwindow.ui
└── hellowidget_0.1.bb
Die Datei hellowidget_0.1.bb definiert den Buildprozess der Anwendung sowie das Zielverzeichnis im Root-Filesystem.
Wichtig: Achte auf konsequente Kleinschreibung, insbesondere im Rezeptnamen – CamelCase kann zu BitBake-Warnungen führen.
# ~/yocto/poky-kirstone/meta-raspilab/recipes-applications/hellowidget/hellowidget_0.1.bb
SUMMARY = "Sample Qt6 Application"
DESCRIPTION = "This is a sample Qt6 application."
LICENSE = "CLOSED"
SRC_URI = " \
file://hellowidget.pro \
file://main.cpp \
file://mainwindow.cpp \
file://mainwindow.h \
file://mainwindow.ui \
"
S = "${WORKDIR}"
DEPENDS += "qtbase wayland"
RDEPENDS:${PN} += "qtwayland"
do_install:append() {
install -d ${D}${bindir}
install -m 0775 hellowidget ${D}${bindir}
}
FILES:${PN} += "${bindir}/hellowidget"
inherit qt6-qmake
Damit die Anwendung auch tatsächlich ins Root-Filesystem gelangt, muss sie im Image-Rezept referenziert werden.
# ~/yocto/poky-kirstone/meta-raspilab/recipes-core/images/raspilab-image.bb
...
APPS = " \
hellowidget \
"
...
IMAGE_INSTALL += " \
...
${APPS} \
"
In dem Unterverzeichnis files werden alle Dateien die zum bauen der Anwendung benötigt werden abgelegt.
Wird das Projekt mit Qt Creator erstellt, sollte die .pro-Datei von Deployment- oder Run-Konfigurationen bereinigt werden. Diese erzeugen in der Regel Pfade oder Befehle, die im Yocto-Buildprozess zu Fehlern führen – insbesondere wenn dort INSTALLS +=-Anweisungen auftauchen, die nicht zum Yocto-Kontext passen.
Nachdem alle unten aufgeführten Dateien erstellt wurden, lässt sich das Image wie gewohnt bauen:
bitbake raspilab-image
Anschließend das Image auf eine SD-Karte schreiben, das Zielsystem booten und anmelden. Die Anwendung sollte direkt aus der Konsole startbar sein.
hellowidget
Die folgenden Quelltexte können auch auf meinem Git-Repository gefunden werden.
# ~/yocto/poky-kirstone/meta-raspilab/recipes-applications/hellowidget/files/hellowidget.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
# ~/yocto/poky-kirstone/meta-raspilab/recipes-applications/hellowidget/files/main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
# ~/yocto/poky-kirstone/meta-raspilab/recipes-applications/hellowidget/files/mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
# ~/yocto/poky-kirstone/meta-raspilab/recipes-applications/hellowidget/files/mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
# ~/yocto/poky-kirstone/meta-raspilab/recipes-applications/hellowidget/files/mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Hello Raspberry Pi!</string>
</property>
<property name="textFormat">
<enum>Qt::PlainText</enum>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>Exit on Click</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections>
<connection>
<sender>pushButton</sender>
<signal>clicked()</signal>
<receiver>MainWindow</receiver>
<slot>close()</slot>
<hints>
<hint type="sourcelabel">
<x>399</x>
<y>556</y>
</hint>
<hint type="destinationlabel">
<x>399</x>
<y>299</y>
</hint>
</hints>
</connection>
</connections>
</ui>
