Qt is another significant GUI framework for Linux. It is used by KDE and many applications. Like GTK, handling of Wayland is done almost transparently in the backend.
Writing Qt5 applications needs the RPM packages
qt5-devel
and qt5-qtwayland-devel
under Fedora.
To demonstrate Qt 5 and Wayland, we don't need a complicated example. The simplest of Qt 5 applications just creates a button with some text. The code is simple.cpp:
// From https://wiki.qt.io/Qt_for_beginners #include <QApplication> #include <QPushButton> int main(int argc, char *argv[]) { QApplication app(argc, argv); QPushButton button; button.setText("Press me"); button.setToolTip("A tooltip"); button.resize(250, 150); button.setWindowTitle("Simple example"); button.show(); return app.exec(); }
Along with that we write a simple.pro file
SOURCES += simple.cpp QT += widgets
This is then used to create a Makefile
and built
to the executable simple
by
qmake-qt5 -o Makefile simple.pro
make
Ubuntu requires package qtbase5-dev and qtwayland5
The default is to run under X:
./simple
runs as an X application with an X window.
./simple -platform wayland
runs as a Wayland application, native on Fedora, in Weston
on Ubuntu.
If you need to get the Wayland display, that is done by
QPlatformNativeInterface *native =
QGuiApplication::platformNativeInterface();
struct wl_display *wl_dpy = (struct wl_display *)
native->nativeResourceForWindow("display", NULL);
If you need to get the Wayland surface, this is done by
QPlatformNativeInterface *native =
QGuiApplication::platformNativeInterface();
struct wl_surface *surface = static_cast<struct wl_surface *>(
native->nativeResourceForWindow("surface", this->windowHandle()));
Building Qt5 applications for Wayland is straightforward:
without any changes to the source code, run the executable
with a command line flag -platform wayland
.
Access to the Wayland display and applications's Wayland surface
is straightforward.
Copyright © Jan Newmarch, jan@newmarch.name
If you like this book, please contribute using Flattr
or donate using PayPal