简体   繁体   English

QML窗口防止焦点窃取

[英]QML window prevent focus stealing

I'm developing hybrid qml/qwidget application with qml-based popup notification system. 我正在使用基于qml的弹出通知系统开发混合qml / qwidget应用程序。 Notifications are stored in list model and I'm using QML Instantiator to create and handle popups. 通知存储在列表模型中,我正在使用QML Instantiator创建和处理弹出窗口。

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. 注意,我使用QMainWindow作为应用程序的主窗口。

Here is QML code snippet: 这是QML代码段:

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: 变量notificationCenterQAbstractListModel派生的对象,它包含所有活动的通知和一些设置,包括通知列表:

QList <iNotification *> m_notifications;

It also contains QQmlEngine and QQmlComponent to load QML code with notification interface. 它还包含QQmlEngineQQmlComponent以通过通知界面加载QML代码。

Popup is implemented using animation of QML window Y coordinate. 使用QML窗口Y坐标的动画实现弹出窗口。 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: 通知对象的创建默认为y = QApplication::desktop()->availableGeometry().height() - 10在将新通知添加到列表后y = QApplication::desktop()->availableGeometry().height() - 10 ,调用recalculateGeometry函数,该函数将重新计算所有通知的Y坐标并为所有通知窗口设置动画:

Behavior on y {
    NumberAnimation {
        duration: 300
    }
}

So the popup itself is handled by Instantiator 因此,弹出窗口本身是由Instantiator处理的

Adding Qt.WA_ShowWithoutActivating to window flags has no effect. Qt.WA_ShowWithoutActivating添加到窗口标志无效。

UPD: I've managed to fix this. UPD:我设法解决了这个问题。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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