Search code examples
macosqtqmlmacos-high-sierra

Qt: Hover is not working after opening external link on macOS


This reproduces for QML and QWidget's, but my example is in QML.

So here is an example:

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
    }
}

Do the following:

  1. Click on the list element to open external link in the browser
  2. Go back to the example app and click outside of a list (gold area)
  3. Immediately after this, try to hover list element

Result - hover stops working for some time.

Again, it reproduces the same if is written with QWidget. My OS is MacOS 10.12.6

Is it a bug or something that can be fixed?


Solution

  • I don't think its a bug! also, its not observed on both Windows and Linux.

    In MacOS newly open apps "steal mouse focus" from your running app; also "focus does not follow mouse behavior!" this is extensively discussed in the blog Settling the OS X focus-follows-mouse debate

    By the way, regarding your observation .. namely (hover stops working for some time after pressing inside gold area) .. you might have just been tricked, because what I noticed is that hover never restored until either:

    • Press inside window (gold area) to bring it to foreground, then hover outside your window and back to regain mouse focus. or,
    • Directly bring window and focus to foreground by pressing on window bar.

    Again another blog Keep applications from stealing focus when opening in OS X confirms the issue and proposes some weak workarounds, for example to start child apps in background in different ways, but I don't think this is a choice for you because you don't want to modify the Info.plist for your browser.

    what I could do is to kill the issue by bringing the window back to foreground (hide() and show() again), which seems to work and bring back stolen mouse focus, I agree this does not sound like a solution but it rather confirms the concept of Mac behavior and that its not a bug, You can watch this by adding a mouse area to the golden area:

    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()
            }
        }
    }
    

    Now when you press inside the golden area hover works like before.

    Note: the preventStealing : property of mousearea does not seem to help because the mouse focus went outside your app, while this property works within elements of your app.