Gerharddc
Gerharddc

Reputation: 4089

QML Text element hyperlink

In my QML Text element I want to have a hyperlink to a website and managed to do so with it looking like one etc. but when I click or touch it nothing happens, the link is supposed to open in a the default browser.

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

Any idea what I'm doing wrong?

Upvotes: 35

Views: 17803

Answers (3)

Timothy C. Quinn
Timothy C. Quinn

Reputation: 4485

If you also want to change the cursor on Hover, you can do this combination:

    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
        }
    }

Upvotes: 2

Sergiy Stepanyuk
Sergiy Stepanyuk

Reputation: 1

I was faced with a task of imitating a hyperlink: When a user hovers on it, the text should look like a hyperlink. But when a user clicks on the link, a customer handler should be called instead of opening URL. Maybe this will be useful for someone.

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
} 

Upvotes: 0

Gerharddc
Gerharddc

Reputation: 4089

Ok I just found that I have to add this:

onLinkActivated: Qt.openUrlExternally(link)

I did not originally consider something like this because I thought if the string was correctly formatted it would open the link on its own.

Upvotes: 62

Related Questions