【问题标题】:QML Text element hyperlinkQML 文本元素超链接
【发布时间】:2012-09-14 05:22:09
【问题描述】:

在我的 QML Text 元素中,我希望有一个指向网站的超链接,并设法让它看起来像一个等等。但是当我点击或触摸它时没有任何反应,该链接应该在默认浏览器中打开.

Text {
    id: link_Text
    text: '<html><style type="text/css"></style><a href="http://google.com">google</a></html>'
}

知道我做错了什么吗?

【问题讨论】:

  • 我不知道你做错了什么,但如果一切都失败了,请在 C++ 中创建一个调用 system("xdg-open google.com") 的 QML 组件。这将适用于桌面 linux 发行版. 我不确定 symbian。include stdlib 代表 system()
  • 我花了几个小时调试我的链接为什么不起作用。事实证明点击区域总是左对齐的,所以如果你使用horizontalAlignment: Text.AlignHCenter,那么蓝色下划线的文本和实际的链接在不同的位置。

标签: qt text hyperlink symbian qml


【解决方案1】:

好的,我刚刚发现我必须添加这个:

onLinkActivated: Qt.openUrlExternally(link)

我最初没有考虑过这样的事情,因为我认为如果字符串格式正确,它会自行打开链接。

【讨论】:

  • 如果单个文本块中有多个锚标签,我们如何区分它们?以某种方式使用ID?编辑:哎呀,我们可以看看传入的link 参数来找出激活了什么链接!
  • 链接到关于在链接悬停时更改鼠标指针的小补充:blog.shantanu.io/2015/02/15/…
【解决方案2】:

如果你还想改变 Hover 上的光标,你可以这样组合:

    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
            cursorShape: Qt.PointingHandCursor
        }
    }

【讨论】:

    【解决方案3】:

    我面临一个模仿超链接的任务:当用户将鼠标悬停在它上面时,文本应该看起来像一个超链接。但是当用户点击链接时,应该调用客户处理程序而不是打开 URL。也许这对某人有用。

    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
    } 
    

    【讨论】:

      猜你喜欢
      • 2022-01-15
      • 2018-07-25
      • 1970-01-01
      • 1970-01-01
      • 2017-07-23
      • 1970-01-01
      • 2021-05-12
      • 1970-01-01
      • 2014-01-04
      相关资源
      最近更新 更多