在我的 QML Text 元素中,我想要一个指向网站的超链接,我设法使它看起来像一个超链接等等。但是当我点击或触摸它时,没有任何反应,这个链接应该会在默认浏览器中打开。
Text {
id: link_Text
text: '<html><style type="text/css"></style><a href="http://google.com">google</a></html>'
}
你有任何想法我做错了什么吗?
好的,我刚刚发现我需要添加这个:
onLinkActivated: Qt.openUrlExternally(link)
我最初没有考虑过这样的情况,因为我认为如果字符串格式正确,它会自动打开链接。
Text {
id: link_Text
text: '<html><style type="text/css"></style><a href="http://google.com">google</a></html>'
onLinkActivated: Qt.openUrlExternally(link)
MouseArea {
id: mouseArea
anchors.fill: parent
acceptedButtons: Qt.NoButton // Don't eat the mouse clicks
cursorShape: Qt.PointingHandCursor
}
}
Text{
id: hyperlinkButtonText
text: "Hyperlink button"
color: application.primaryColor
font.pixelSize: 12
font.bold:true
MouseArea{
id: mouseHyperlinkArea
anchors.fill: parent
hoverEnabled: true
cursorShape: Qt.PointingHandCursor
onClicked: {
// to do something on clicking the link
}
}
}
Rectangle{ /*Here is an underline item for the text above*/
visible: mouseHyperlinkArea.containsMouse
anchors.top:hyperlinkButtonText.bottom
anchors.topMargin: -1
width:hyperlinkButtonText.width
height: 0.5
color: application.primaryColor
}
horizontalAlignment: Text.AlignHCenter
,那么蓝色下划线文本和实际链接就会处于不同的位置。 - nwp