Qt:在macOS上打开外部链接后,Hover无法正常工作

时间:2018-03-16 15:51:40

标签: macos qt qml macos-high-sierra

这适用于QML和QWidget,但我的例子是QML。

所以这是一个例子:

import QtQuick 2.7
import QtQuick.Controls 1.4

ApplicationWindow {
    id: rootWindow

    visible: true
    width: 640
    height: 480
    color: "gold"

    ListView {
        width: parent.width
        height: parent.height / 2 * 3
        model: 5
        spacing: 1
        delegate: Rectangle {
            width: parent.width
            height: 50
            color: ma.containsMouse ? "mediumvioletred" : "mintcream"
            border.color: "black"
            border.width: 1

            Text {
                anchors.centerIn: parent
                text: "Click on me to open google.com"
                font.bold: true
            }

            MouseArea {
                id: ma
                anchors.fill: parent
                onClicked: Qt.openUrlExternally("https://www.google.com/");
                hoverEnabled: true
            }
        }
    }

    Text {
        width: parent.width
        height: 200
        anchors.bottom: parent.bottom
        color: "black"
        text: "1. Click on any list element (note color when hovered)\n2. Re-gain focus by click outside of the list (gold color area)\n3. Hover list element";
        font.bold: true
        horizontalAlignment: Qt.AlignHCenter
        verticalAlignment: Qt.AlignVCenter
    }
}

执行以下操作:

  1. 单击列表元素以在浏览器中打开外部链接
  2. 返回示例应用并点击列表中的外部(黄金区域)
  3. 在此之后,立即尝试悬停列表元素
  4. 结果 - 悬停停止工作一段时间。

    再次,如果使用QWidget编写,它会再现相同的内容。 我的操作系统 MacOS 10.12.6

    这是一个可以修复的错误还是什么?

1 个答案:

答案 0 :(得分:1)

我不认为这是一个错误!此外,它在Windows和Linux上均未被观察到。

在MacOS新开的应用程序" 窃取鼠标焦点"从您正在运行的应用同样"焦点不遵循鼠标行为!" 在博客Settling the OS X focus-follows-mouse debate

中广泛讨论了这一点

顺便说一句,关于你的观察..即(在按下黄金区域后,悬停停止工作一段时间)..你可能刚被欺骗,因为我注意到的是悬停从不恢复到以下任何一个:

  • 按内窗(黄金区域)将其置于前景,然后将悬停在窗外并返回以重新获得鼠标焦点。或者,
  • 按窗口栏直接将窗口和焦点对准前景。

另一篇博客Keep applications from stealing focus when opening in OS X再次证实了这个问题并提出了一些解决方法,例如以不同的方式在背景中启动子应用,但我没有&# 39;认为这是您的选择,因为您不想修改浏览器的Info.plist

我可以做的是通过将窗口恢复到前台(hide()show()再次)来杀死这个问题,这似乎有效并且可以恢复被盗的鼠标焦点,我同意这听起来不像是一个解决方案,但它确实证实了Mac行为的概念并且它不是一个bug, 您可以通过在黄金区域添加鼠标区域来观察:

Text {
    width: parent.width
    height: 200
    anchors.bottom: parent.bottom
    color: "black"
    text: "1. Click on any list element (note color when hovered)\n2. Re-gain focus by click outside of the list (gold color area)\n3. Hover list element";
    font.bold: true
    horizontalAlignment: Qt.AlignHCenter
    verticalAlignment: Qt.AlignVCenter
    MouseArea{
        anchors.fill: parent
        onClicked: {
            rootWindow.hide()
            rootWindow.show()
        }
    }
}

现在当你按下黄金区域时,悬停就像以前一样。

注意:preventStealing :的{​​{1}}属性似乎没有帮助,因为鼠标焦点超出了您的应用,而此属性适用于您应用的元素。

相关问题