The MouseArea item enables simple mouse handling. More...
Inherits Item
A MouseArea is typically used in conjunction with a visible item, where the MouseArea effectively 'proxies' mouse handling for that item. For example, we can put a MouseArea in a Rectangle that changes the Rectangle color to red when clicked:
import Qt 4.7 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 mouse event, such as the position, button, and any key modifiers.
Here is an extension of the previous example that produces a different color when the area is right clicked:
Rectangle { width: 100; height: 100 color: "green" MouseArea { anchors.fill: parent acceptedButtons: Qt.LeftButton | Qt.RightButton onClicked: { if (mouse.button == Qt.RightButton) parent.color = 'blue'; else parent.color = 'red'; } } }
For basic key handling, see the Keys attached property.
MouseArea is an invisible item: it is never painted.
See also MouseEvent and MouseArea example.
acceptedButtons : Qt::MouseButtons |
This property holds the mouse buttons that the mouse area reacts to.
The available buttons are:
To accept more than one button the flags can be combined with the "|" (or) operator:
MouseArea { acceptedButtons: Qt.LeftButton | Qt.RightButton }
The default value is Qt.LeftButton.
read-onlycontainsMouse : bool |
This property holds whether the mouse is currently inside the mouse area.
Warning: This property is not updated if the area moves under the mouse: containsMouse will not change. In addition, if hoverEnabled is false, containsMouse will only be valid when the mouse is pressed.
drag.target : Item |
read-onlydrag.active : bool |
drag.axis : enumeration |
drag.minimumX : real |
drag.maximumX : real |
drag.minimumY : real |
drag.maximumY : real |
drag.filterChildren : bool |
drag provides a convenient way to make an item draggable.
The following example displays a Rectangle that can be dragged along the X-axis. The opacity of the rectangle is reduced when it is dragged to the right.
Rectangle { id: container width: 600; height: 200 Rectangle { id: rect width: 50; height: 50 color: "red" opacity: (600.0 - rect.x) / 600 MouseArea { anchors.fill: parent drag.target: rect drag.axis: Drag.XAxis drag.minimumX: 0 drag.maximumX: container.width - rect.width } } }
Note: Items cannot be dragged if they are anchored for the requested drag.axis. For example, if anchors.left or anchors.right was set for rect in the above example, it cannot be dragged along the X-axis. This can be avoided by settng the anchor value to undefined in an onPressed handler.
If drag.filterChildren is set to true, a drag can override descendant MouseAreas. This enables a parent MouseArea to handle drags, for example, while descendants handle clicks:
import Qt 4.7 Rectangle { width: 480 height: 320 Rectangle { x: 30; y: 30 width: 300; height: 240 color: "lightsteelblue" MouseArea { anchors.fill: parent drag.target: parent; drag.axis: "XAxis" drag.minimumX: 30 drag.maximumX: 150 drag.filterChildren: true Rectangle { color: "yellow" x: 50; y : 50 width: 100; height: 100 MouseArea { anchors.fill: parent onClicked: console.log("Clicked") } } } } }
enabled : bool |
This property holds whether the item accepts mouse events.
hoverEnabled : bool |
This property holds whether hover events are handled.
By default, mouse events are only handled in response to a button event, or when a button is pressed. Hover enables handling of all mouse events even when no mouse button is pressed.
This property affects the containsMouse property and the onEntered, onExited and onPositionChanged signals.
These properties hold the coordinates of the mouse.
If the hoverEnabled property is false then these properties will only be valid while a button is pressed, and will remain valid as long as the button is held even if the mouse is moved outside the area.
If hoverEnabled is true then these properties will be valid when:
The coordinates are relative to the MouseArea.
read-onlypressed : bool |
This property holds whether the mouse area is currently pressed.
This property holds the mouse buttons currently pressed.
It contains a bitwise combination of:
The code below displays "right" when the right mouse buttons is pressed:
Text { text: mouseArea.pressedButtons & Qt.RightButton ? "right" : "" horizontalAlignment: Text.AlignHCenter verticalAlignment: Text.AlignVCenter MouseArea { id: mouseArea anchors.fill: parent acceptedButtons: Qt.LeftButton | Qt.RightButton } }
See also acceptedButtons.
This handler is called when mouse events have been canceled, either because an event was not accepted, or because another element stole the mouse 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 on the pressed signal and then start dragging, the Flickable will steal the mouse handling from the MouseArea. In these cases, to reset the logic when the MouseArea has lost the mouse handling to the Flickable, onCanceled should be used in addition to onReleased.
This handler is called 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).
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.
The accepted property of the MouseEvent parameter is ignored in this handler.
This handler is called when there is a double-click (a press followed by a release followed by a press). 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.
The accepted property of the MouseEvent parameter is ignored in this handler.
This handler is called when the mouse enters the mouse area.
By default the onEntered handler is only called while a button is pressed. Setting hoverEnabled to true enables handling of onEntered when no mouse button is pressed.
See also hoverEnabled.
This handler is called when the mouse exists the mouse area.
By default the onExited handler is only called while a button is pressed. Setting hoverEnabled to true enables handling of onExited when no mouse button is pressed.
See also hoverEnabled.
MouseArea::onPositionChanged ( MouseEvent mouse ) |
This handler is called when the mouse position changes.
The mouse parameter provides information about the mouse, including the x and y position, and any buttons currently pressed.
The accepted property of the MouseEvent parameter is ignored in this handler.
By default the onPositionChanged handler is only called while a button is pressed. Setting hoverEnabled to true enables handling of onPositionChanged when no mouse button is pressed.
This handler is called 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, and which button is pressed.
The accepted property of the MouseEvent parameter is ignored in this handler.
This handler is called when there is a press. The mouse parameter provides information about the press, including the x and y position and which button was pressed.
The accepted property of the MouseEvent parameter determines whether this MouseArea will handle the press and all future mouse events until release. The default is to accept the event and not allow other MouseArea 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.
This handler is called 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.
The accepted property of the MouseEvent parameter is ignored in this handler.