是否有 Qt/QML 函数来检查形状是否已被单击?
Is there a Qt/QML function to check if a shape has been clicked?
我正在尝试在形成六边形的 QML Shape 中注册点击。
我知道有
Shape.FillContains() https://doc.qt.io/qt-5/qml-qtquick-shapes-shape.html#containsMode-prop
属性 我正在尝试使用。
但我不知道如何进一步进行,因为我之前刚刚使用过 MouseAreas。
import QtQuick 2.15
import QtQuick.Shapes 1.15
import QtQuick.Extras 1.4
Item
{
id: root
property real srh //ScaleFactor
Shape
{
id: rootShape
width: srh * (6.5 - 0.5)
height: srh * 5.2
anchors.centerIn: parent
containsMode: Shape.FillContains
ShapePath
{
id: myHexagon
strokeWidth: 4
strokeColor: "white"
fillColor: "steelblue"
//Path
startX: 2 * srh;
startY: 0 * srh
PathLine { x: 5 * srh; y: 0 * srh }
PathLine { x: 6.5 * srh; y: 2.6 * srh }
PathLine { x: 5.0 * srh; y: 5.2 * srh }
PathLine { x: 2.0 * srh; y: 5.2 * srh }
PathLine { x: 0.5 * srh; y: 2.6 * srh }
PathLine { x: 2 * srh; y: 0 * srh }
}
}
有人知道如何使用它吗?像 onClicked()-Handler 这样的东西会很好。
谢谢
您 link 的文档似乎已经指出了答案。您是否尝试过将 containsMode
与 MouseArea
结合使用?
Shape
{
id: shape
signal clicked()
containsMode: Shape.FillContains
MouseArea
{
anchors.fill: parent
onClicked:
{
if (shape.contains(Qt.point(mouseX, mouseY)))
{
shape.clicked();
}
}
}
}
正如关于 containsMode
的文档所说:
It is useful in case you add Qt Quick Input Handlers and you want to react only when the mouse or touchpoint is fully inside the Shape.
您可以使用像 TapHandler
or HoverHandler
这样的输入处理程序,它们使用父级的 contains 方法。
Shape {
id: rootShape
width: srh * (6.5 - 0.5)
height: srh * 5.2
anchors.centerIn: parent
containsMode: Shape.FillContains
ShapePath {
id: myHexagon
strokeWidth: 4
strokeColor: "white"
fillColor: hoverHandler.hovered ? "gold" : "steelblue"
//Path
startX: 2 * srh;
startY: 0 * srh
PathLine { x: 5 * srh; y: 0 * srh }
PathLine { x: 6.5 * srh; y: 2.6 * srh }
PathLine { x: 5.0 * srh; y: 5.2 * srh }
PathLine { x: 2.0 * srh; y: 5.2 * srh }
PathLine { x: 0.5 * srh; y: 2.6 * srh }
PathLine { x: 2 * srh; y: 0 * srh }
}
HoverHandler {
id: hoverHandler
}
TapHandler {
onTapped: print("Hexagon clicked")
}
}
我正在尝试在形成六边形的 QML Shape 中注册点击。 我知道有
Shape.FillContains() https://doc.qt.io/qt-5/qml-qtquick-shapes-shape.html#containsMode-prop
属性 我正在尝试使用。 但我不知道如何进一步进行,因为我之前刚刚使用过 MouseAreas。
import QtQuick 2.15
import QtQuick.Shapes 1.15
import QtQuick.Extras 1.4
Item
{
id: root
property real srh //ScaleFactor
Shape
{
id: rootShape
width: srh * (6.5 - 0.5)
height: srh * 5.2
anchors.centerIn: parent
containsMode: Shape.FillContains
ShapePath
{
id: myHexagon
strokeWidth: 4
strokeColor: "white"
fillColor: "steelblue"
//Path
startX: 2 * srh;
startY: 0 * srh
PathLine { x: 5 * srh; y: 0 * srh }
PathLine { x: 6.5 * srh; y: 2.6 * srh }
PathLine { x: 5.0 * srh; y: 5.2 * srh }
PathLine { x: 2.0 * srh; y: 5.2 * srh }
PathLine { x: 0.5 * srh; y: 2.6 * srh }
PathLine { x: 2 * srh; y: 0 * srh }
}
}
有人知道如何使用它吗?像 onClicked()-Handler 这样的东西会很好。
谢谢
您 link 的文档似乎已经指出了答案。您是否尝试过将 containsMode
与 MouseArea
结合使用?
Shape
{
id: shape
signal clicked()
containsMode: Shape.FillContains
MouseArea
{
anchors.fill: parent
onClicked:
{
if (shape.contains(Qt.point(mouseX, mouseY)))
{
shape.clicked();
}
}
}
}
正如关于 containsMode
的文档所说:
It is useful in case you add Qt Quick Input Handlers and you want to react only when the mouse or touchpoint is fully inside the Shape.
您可以使用像 TapHandler
or HoverHandler
这样的输入处理程序,它们使用父级的 contains 方法。
Shape {
id: rootShape
width: srh * (6.5 - 0.5)
height: srh * 5.2
anchors.centerIn: parent
containsMode: Shape.FillContains
ShapePath {
id: myHexagon
strokeWidth: 4
strokeColor: "white"
fillColor: hoverHandler.hovered ? "gold" : "steelblue"
//Path
startX: 2 * srh;
startY: 0 * srh
PathLine { x: 5 * srh; y: 0 * srh }
PathLine { x: 6.5 * srh; y: 2.6 * srh }
PathLine { x: 5.0 * srh; y: 5.2 * srh }
PathLine { x: 2.0 * srh; y: 5.2 * srh }
PathLine { x: 0.5 * srh; y: 2.6 * srh }
PathLine { x: 2 * srh; y: 0 * srh }
}
HoverHandler {
id: hoverHandler
}
TapHandler {
onTapped: print("Hexagon clicked")
}
}