问题描述
我正在尝试以形成六边形的QML形状注册点击。 我知道有
我正在尝试使用的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这样的东西会很好。
谢谢
解决方法
如containsMode
的文档所述:
如果添加Qt Quick Input Handlers并且仅当鼠标或接触点完全位于Shape内时才做出反应,这很有用。
您可以使用TapHandler
或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")
}
}
,
您链接到的文档似乎已经指向了答案。您是否尝试过将containsMode
与MouseArea
组合在一起?
Shape
{
id: shape
signal clicked()
containsMode: Shape.FillContains
MouseArea
{
anchors.fill: parent
onClicked:
{
if (shape.contains(Qt.point(mouseX,mouseY)))
{
shape.clicked();
}
}
}
}