Upto: Table of Contents of full book "Programming Wayland Clients"

This project hasn't been updated since mid-2017. Wayland has moved on since then, so the information here may be out of date, and there is no guarantee the programs still work. You are recommended to look at The Wayland Protocol for more up-todate information, or at A better way to read Wayland documentation . .

Qt 5

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.

Resources

Installing

Writing Qt5 applications needs the RPM packages qt5-devel and qt5-qtwayland-devel under Fedora.

A simple Qt 5 application

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

Running under X or Wayland

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.

Getting a Wayland surface

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()));
	
      

Conclusion

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