MouseArea QML 类型

Enables simple touch handling. 更多...

导入语句: import QtQuick .
Since: Qt Quick Ultralite 1.0
继承:

Item

特性

信号

详细描述

A MouseArea is an invisible item that is typically used in conjunction with a visible item in order to provide touch handling for that item. By effectively acting as a proxy, the logic for touch handling can be contained within a MouseArea item.

enabled property is used to enable and disable touch handling for the proxied item. When disabled, the area becomes transparent to touch events.

MouseArea is an invisible Item, but it has a visible property. When set to false, the area becomes transparent to touch events.

pressed read-only property indicates whether or not the user is pressing down over the mouse area. This property is often used in bindings between properties in a user interface.

Information about the touch position and pressed status are provided via signals for which event handler properties are defined. The most commonly used involve handling touch presses: onClicked, onPressed, onReleased and onPressAndHold.

If a MouseArea overlaps with the area of other MouseArea items, only the top area will receive events.

用法范例

The following example uses a MouseArea in a Rectangle that changes the Rectangle color to red when tapped:

import QtQuick 2.15
Rectangle {
    width: 100; height: 100
    color: "green"
    MouseArea {
        anchors.fill: parent
        onClicked: { parent.color = 'red' }
    }
}
						

Many MouseArea signals pass a mouse parameter that contains additional information about the touch event, such as the position.

Here is an extension of the previous example that produces a different color when the press is long enough:

Rectangle {
    width: 100; height: 100
    color: "green"
    MouseArea {
        anchors.fill: parent
        onClicked: {
            if (mouse.wasHeld)
                parent.color = 'blue';
            else
                parent.color = 'red';
        }
    }
}
						

另请参阅 MouseEvent .

特性文档编制

mouseX : real

mouseY : real

These properties hold the coordinates of the touch.

These properties will only be valid while there is a press, and will remain valid as long as the touch persists, even if it moves outside the area.

The coordinates are relative to the MouseArea .


pressAndHoldInterval : int

This property overrides the elapsed time in milliseconds before pressAndHold is emitted.

By default, the value is 800ms.

另请参阅 pressAndHold .


pressed : bool

This property holds whether any touch is currently happening.

This property was introduced in Qt Quick Ultralite 1.1.


信号文档编制

canceled ()

This signal is emitted when touch events have been canceled, because another item stole the touch event handling.

This signal is for advanced use: it is useful when there is more than one MouseArea that is handling input, or when there is a MouseArea inside a Flickable . In the latter case, if you execute some logic in the onPressed signal handler and then start dragging, the Flickable will steal the touch handling from the MouseArea . In these cases, to reset the logic when the MouseArea has lost the touch handling to the Flickable , canceled should be handled in addition to released .

注意: 相应处理程序是 onCanceled .

This signal was introduced in Qt Quick Ultralite 1.1.


clicked ( MouseEvent mouse )

This signal is emitted when there is a click. A click is defined as a press followed by a release, both inside the MouseArea (pressing, moving outside the MouseArea , and then moving back inside and releasing is also considered a click).

mouse parameter provides information about the click, including the x and y position of the release of the click, and whether the click was held.

When handling this signal, changing the accepted 特性为 mouse parameter has no effect.

注意: 相应处理程序是 onClicked .


positionChanged ( MouseEvent mouse )

This signal is emitted when the touch position changes.

mouse parameter provides information about the touch, including the x and y position.

This signal is only emitted if there is a press.

When handling this signal, changing the accepted 特性为 mouse parameter has no effect.

注意: 相应处理程序是 onPositionChanged .


pressAndHold ( MouseEvent mouse )

This signal is emitted when there is a long press (currently 800ms). The mouse parameter provides information about the press, including the x and y position of the press.

When handling this signal, changing the accepted 特性为 mouse parameter has no effect.

注意: 相应处理程序是 onPressAndHold .

This signal was introduced in Qt Quick Ultralite 1.1.


pressed ( MouseEvent mouse )

This signal is emitted when there is a press. The mouse parameter provides information about the press, including the x and y position.

When handling this signal, use the accepted 特性为 mouse parameter to control whether this MouseArea handles the press and all future touch events until release. The default is to accept the event and not allow other MouseAreas beneath this one to handle the event. If accepted is set to false, no further events will be sent to this MouseArea until the button is next pressed.

注意: 相应处理程序是 onPressed .

This signal was introduced in Qt Quick Ultralite 1.1.


released ( MouseEvent mouse )

This signal is emitted when there is a release. The mouse parameter provides information about the click, including the x and y position of the release of the click, and whether the click was held.

When handling this signal, changing the accepted 特性为 mouse parameter has no effect.

注意: 相应处理程序是 onReleased .

This signal was introduced in Qt Quick Ultralite 1.1.

另请参阅 canceled .