Manage focus between multiple displays on Linux Wayland
-
Hi,
I am trying to understand the right way to manage focus when creating QQuickView across multiple monitors.
I have created a minimal app to reproduce what I am trying to do.
main.cpp
:#include <QGuiApplication> #include <QQuickView> #include <QScreen> #include <QQmlContext> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); const QList<QScreen *> screens = QGuiApplication::screens(); // Create a QQuickView for each screen QList<QQuickView *> views; QQuickView *primaryScreenView; for (QScreen *screen : screens) { QQuickView *view = new QQuickView; views.append(view); view->setScreen(screen); view->setResizeMode(QQuickView::SizeRootObjectToView); view->setGeometry(screen->geometry()); view->setFlags(Qt::FramelessWindowHint); bool isPrimaryScreen = QGuiApplication::primaryScreen() == screen; view->rootContext()->setContextProperty(QStringLiteral("primaryScreen"), QVariant::fromValue(isPrimaryScreen)); if (isPrimaryScreen) { primaryScreenView = view; } view->setSource(QUrl("qrc:/Main.qml")); qInfo() << "Adding view for" << screen->name() << screen->geometry() << " primary: " << isPrimaryScreen; view->showFullScreen(); } primaryScreenView->requestActivate(); return app.exec(); }
Main.qml
:import QtQuick 2.15 Rectangle { id: root width: 1920 height: 1080 color: "lightblue" Component.onCompleted: { console.log("Screen ", root.Screen.name, " primary: ", primaryScreen); } Column { anchors.centerIn: parent Text { anchors.horizontalCenter: parent.horizontalCenter text: "Hello, World!" font.pixelSize: 48 } TextInput { id: input visible: primaryScreen focus: primaryScreen anchors.horizontalCenter: parent.horizontalCenter text: "Type here" font.pixelSize: 24 } } }
On my Linux machine when running the graphical session on X11 this works as expected having the TextInput on the primary screen focused. But on Wayland the TextInput is not focused, instead the QQuickView on the last monitor to be added is active, and I need to click on the input to focus it.
I found multiple sources claiming that
requestActivate()
does nothing on Wayland, as the rules governing which window is active are stricter compared to X11, is this true? If so, how can I achieve to have focus on my primary display instead of the last added one? -
Hi and welcome to devnet,
Just a quick that came to mind: what about adding the primary screen widget as last ?