What's wrong with my demo? Does PlasmaShellSurface only support QQuickView but not QWidget?

Hi everyone :slight_smile:

I write this demo below, but the window doesn’s go to (100, 200), and it doesn’t skip taskbar or switcher, and it’s not OnScreenDisplay.

waylandwindow.h

#ifndef WAYLANDWINDOW_H
#define WAYLANDWINDOW_H

#include <QWidget>

#include <connection_thread.h>
#include <registry.h>
#include <compositor.h>
#include <plasmashell.h>
#include <surface.h>

using namespace KWayland::Client;

class WaylandWindow : public QWidget
{
    Q_OBJECT

public:
    WaylandWindow(QWidget *parent = nullptr);
    ~WaylandWindow();

private:
    void init();

private:
    ConnectionThread *m_connection;
    Compositor *m_compositor = nullptr;
    Surface *m_surface = nullptr;
    PlasmaShell *m_plasmaShell = nullptr;
    PlasmaShellSurface *m_plasmaShellSurface = nullptr;
};
#endif // WAYLANDWINDOW_H

waylandwindow.cpp

#include "waylandwindow.h"

WaylandWindow::WaylandWindow(QWidget *parent)
    : QWidget(parent)
    , m_connection(ConnectionThread::fromApplication(this))
{
    init();
}

WaylandWindow::~WaylandWindow()
{
}

void WaylandWindow::init()
{
    Registry *registry = new Registry(this);
    registry->create(m_connection);

    connect(registry, &Registry::compositorAnnounced, this, [this, registry](quint32 name, quint32 version) {
        m_compositor = registry->createCompositor(name, version, this);
        m_surface = m_compositor->createSurface(this);
    });

    connect(registry, &Registry::plasmaShellAnnounced, this, [this, registry](quint32 name, quint32 version) {
        m_plasmaShell = registry->createPlasmaShell(name, version, this);
        m_plasmaShellSurface = m_plasmaShell->createSurface(m_surface, this);
        m_plasmaShellSurface->setPosition(QPoint(100, 200));
        m_plasmaShellSurface->setSkipTaskbar(true);
        m_plasmaShellSurface->setSkipSwitcher(true);
        m_plasmaShellSurface->setRole(PlasmaShellSurface::Role::OnScreenDisplay);
    });

    registry->setup();
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

project(wayland-client VERSION 0.1 LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)

find_package(KF5Wayland CONFIG)
# set_package_properties(KF5Wayland PROPERTIES TYPE OPTIONAL )
# add_feature_info("KF5Wayland" KF5Wayland_FOUND "Required for the awesome Wayland on Qt demo")

set(PROJECT_SOURCES
        main.cpp
        waylandwindow.cpp
        waylandwindow.h
)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(wayland-client
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
    )
# Define target properties for Android with Qt 6 as:
#    set_property(TARGET wayland-client APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
#                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
    if(ANDROID)
        add_library(wayland-client SHARED
            ${PROJECT_SOURCES}
        )
# Define properties for Android with Qt 5 after find_package() calls as:
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
    else()
        add_executable(wayland-client
            ${PROJECT_SOURCES}
        )
    endif()
endif()

target_link_libraries(wayland-client PRIVATE Qt${QT_VERSION_MAJOR}::Widgets KF5::WaylandClient)

set_target_properties(wayland-client PROPERTIES
    MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

if(QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(wayland-client)
endif()

main.cpp

#include "waylandwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    WaylandWindow w;
    w.show();

    return a.exec();
}

In this demo, WaylandWindow inherits QWidget. But if I change it to inherit QQuickView, it seems to work.

I wonder why is this happening? Does PlasmaShellSurface only support QQuickView class?

Tested on kubuntu 2304.

Thanks in advance :slight_smile:

You are creating an entirely new Surface which is different from the surface that Qt is using for your QWidget/QQuickView.
See Surface::fromWindow

1 Like

Thank you for the reply! :smiley:

But the surface would be null if I use Surface::fromWindow(this->windowHandle()). See the picture below. Am I using this wrong?

(ps, If Class WaylandWindow is derived from QQuickView, the surface returned by Surface::fromWindow() is ok.)

Again, thank you for your reply :slight_smile:

OMG!

It worked! I called show() before Surface::fromWindow(), and now it worked!

Thank you davidre :grin: I really appreciate it!

Because QQuickView is a QWindow already, a Widget might not necessarily have a QWindow right after creating yet

1 Like