在Web浏览器中,当您将鼠标悬停在链接上时,它会变为指向的手.我想在QML中实现同样的目标.
以下是一个QML Text
元素,其中包含一个使用HTML标记的文本块,其中包含几个链接:
Text { id: bodyMessage
anchors.fill: parent
textFormat: Text.RichText
wrapMode: Text.WordWrap
text:
'<p>' + qsTr('Here is my first paragraph.') + '</p>' +
'<p>' + qsTr('My second paragraph contains the <a href="link1">first link</a>.') + '</p>' +
'<p>' + qsTr('My third paragraph contains the <a href="link2">second link</a> and that is it.') + '</p>'
onLinkActivated:
(link) =>
console.log('Link activated to: `' + link + '`')
}
问题是,当鼠标光标位于任一链接上时,它不会更改为指向手.
我try 了一个 children MouseArea
,如下所示:
MouseArea {
anchors.fill: parent
hoverEnabled: true
onPositionChanged:
(mouse) =>
cursorShape = bodyMessage.linkAt(mouse.x, mouse.y) ? Qt.PointingHandCursor : Qt.ArrowCursor
}
这确实会使鼠标光标按需更改,但它会阻止鼠标信号变为Text
,因此onLinkActivated
不再起作用.
为了解决这个新问题,我在MouseArea
的基础上添加了以下内容:
onClicked:
(mouse) =>
{
let link = bodyMessage.linkAt(mouse.x, mouse.y)
if (link.length > 0)
console.log('Link activated to: `' + link + '`')
}
虽然这确实有效,但它使Text.linkActivated
变得过时,这在我看来是错误的方法,代码在我看来也是臃肿的.