Qt:在 macOS 上打开外部 link 后悬停不起作用
Qt: Hover is not working after opening external link on macOS
这是针对 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
}
}
执行以下操作:
- 点击列表元素在浏览器中打开外部link
- 返回示例应用并单击列表(黄金区域)外部
- 在此之后,立即尝试悬停列表元素
结果 - 悬停停止工作了一段时间。
同样,如果是用 QWidget 编写的,它会重现相同的内容。
我的 OS 是 MacOS 10.12.6
这是一个错误还是可以修复的问题?
我不认为这是一个错误!此外,它在 Windows 和 Linux.
上均未观察到
在 MacOS 新打开的应用程序中,从您的 运行 应用程序“窃取鼠标焦点”;还有 "focus does not follow mouse behavior!" 这在博客 Settling the OS X focus-follows-mouse debate
中有广泛讨论
顺便说一句,关于你的观察..即(在金色区域内按下后悬停停止工作一段时间)..你可能只是被骗了,因为我注意到悬停从未恢复,直到:
- 按内部 window(金色区域)将其置于前台,然后将鼠标悬停在 外部 您的 window 并返回以重新获得鼠标焦点。或者,
- 按下 window 栏直接将 window 和焦点置于前景。
另一个博客 Keep applications from stealing focus when opening in OS X 再次证实了这个问题并提出了一些 弱 解决方法,例如在 background 中启动子应用程序以不同的方式,但我认为这不是您的选择,因为您不想为您的浏览器修改 Info.plist
。
我能做的是 通过将 window 带回前台(再次 hide()
和 show()
来解决 问题,这似乎有效并带回了被盗的鼠标焦点,我同意这听起来不像是一个解决方案,但它证实了 Mac 行为的概念并且它不是错误,
可以在黄金区域添加鼠标区域观看:
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()
}
}
}
现在,当您在黄金区域内按下时,悬停会像以前一样工作。
注意:mousearea
的 preventStealing :
属性 似乎没有帮助,因为鼠标焦点超出了您的应用程序,而此 属性 在您的元素内有效应用
这是针对 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
}
}
执行以下操作:
- 点击列表元素在浏览器中打开外部link
- 返回示例应用并单击列表(黄金区域)外部
- 在此之后,立即尝试悬停列表元素
结果 - 悬停停止工作了一段时间。
同样,如果是用 QWidget 编写的,它会重现相同的内容。 我的 OS 是 MacOS 10.12.6
这是一个错误还是可以修复的问题?
我不认为这是一个错误!此外,它在 Windows 和 Linux.
上均未观察到在 MacOS 新打开的应用程序中,从您的 运行 应用程序“窃取鼠标焦点”;还有 "focus does not follow mouse behavior!" 这在博客 Settling the OS X focus-follows-mouse debate
中有广泛讨论顺便说一句,关于你的观察..即(在金色区域内按下后悬停停止工作一段时间)..你可能只是被骗了,因为我注意到悬停从未恢复,直到:
- 按内部 window(金色区域)将其置于前台,然后将鼠标悬停在 外部 您的 window 并返回以重新获得鼠标焦点。或者,
- 按下 window 栏直接将 window 和焦点置于前景。
另一个博客 Keep applications from stealing focus when opening in OS X 再次证实了这个问题并提出了一些 弱 解决方法,例如在 background 中启动子应用程序以不同的方式,但我认为这不是您的选择,因为您不想为您的浏览器修改 Info.plist
。
我能做的是 通过将 window 带回前台(再次 hide()
和 show()
来解决 问题,这似乎有效并带回了被盗的鼠标焦点,我同意这听起来不像是一个解决方案,但它证实了 Mac 行为的概念并且它不是错误,
可以在黄金区域添加鼠标区域观看:
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()
}
}
}
现在,当您在黄金区域内按下时,悬停会像以前一样工作。
注意:mousearea
的 preventStealing :
属性 似乎没有帮助,因为鼠标焦点超出了您的应用程序,而此 属性 在您的元素内有效应用