简体   繁体   中英

QML window prevent focus stealing

I'm developing hybrid qml/qwidget application with qml-based popup notification system. Notifications are stored in list model and I'm using QML Instantiator to create and handle popups.

My problem is that every time new notification pops up it steals focus from the main widow. Note, that I'm using QMainWindow as a main window for the application.

Here is QML code snippet:

Instantiator {
id: instantiator
model: notificationCenter
delegate:
    Window {
    id: notificationWindow
    color: "transparent"
...

Variable notificationCenter is QAbstractListModel -derived object that contains all active notifications and some settings, including list of notifications:

QList <iNotification *> m_notifications;

It also contains QQmlEngine and QQmlComponent to load QML code with notification interface.

Popup is implemented using animation of QML window Y coordinate. Notification object is created with default y = QApplication::desktop()->availableGeometry().height() - 10 after adding new notification to the list recalculateGeometry function is called, which recalculates Y coordinates of all notifications and animates all notification windows:

Behavior on y {
    NumberAnimation {
        duration: 300
    }
}

So the popup itself is handled by Instantiator

Adding Qt.WA_ShowWithoutActivating to window flags has no effect.

UPD: I've managed to fix this. Window steals focus with the following flags:

flags: Qt.FramelessWindowHint | Qt.WA_ShowWithoutActivating |  Qt.WindowStaysOnTopHint

But (surprisingly) does not steal focus with the following flags:

flags: Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.Popup

Solution found, use

flags: Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.Popup

instead of

flags: Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.WA_ShowWithoutActivating

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM