Home > Enterprise >  OnClicked event doesn't work in QML window
OnClicked event doesn't work in QML window

Time:01-05

I've created a simple QML window with a map and a marker on it. But there is no reaction on a mouse click. The Window is opened from C application made in Qt 6.5.

import QtQuick
import QtQuick.Window
import QtLocation
import QtPositioning

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Карта")

    Plugin {
        id: mapPlugin
        name: "osm"
    }

    Map {
        anchors.fill: parent
        plugin: mapPlugin
        center: QtPositioning.coordinate(59.91, 10.75) // Oslo
        zoomLevel: 14
        Component.onCompleted: console.log("Text onCompleted map.")

        MouseArea {
            id: m1
            anchors.fill: parent
            //propagateMouseEvent: true
            acceptedButtons: Qt.LeftButton | Qt.RightButton
            onClicked: console.log("click ")

            Rectangle {
                anchors.fill: parent
                color: "transparent"
                border.color: "black"
            }
        }
    }
}

CodePudding user response:

I've tested it on Linux and everything works. I remember someone saying that on Windows the QML output is special. It seems you need to use DebugView. Have a look at the comment on this post. This one as well.

CodePudding user response:

I just installed Qt6.5.0-beta and see "clicked " appearing in Qt Creator when I click on the map. Having said that, I have observed on some touch-screen devices, there could be a problem with gesture handling, particularly when both Map and MouseArea are both vying for mouse input.

If that is what's happening, it may worth considering TapHandler, e.g.

import QtQuick
import QtQuick.Window
import QtLocation
import QtPositioning

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Карта")

    Plugin {
        id: mapPlugin
        name: "osm"
    }

    Map {
        anchors.fill: parent
        plugin: mapPlugin
        center: QtPositioning.coordinate(59.91, 10.75) // Oslo
        zoomLevel: 14
        Component.onCompleted: console.log("Text onCompleted map.")

        TapHandler {
            onTapped: console.log("TAPPED")
        }
    }
}
  • Related