Open links from Text-elements in QtQuick

From day one, QtQuick’s Text-element sports opening URL:s in an external application, typically a web page. It’s very easy to use this feature and probably most of you have already been close to or have used it.

The only thing that is required is that you set the Text.textFormat property to Text.RichText. Default, textFormat is set to Text.AutoText which means that the element will try to identify if the content is rich text or not. This doesn’t always work perfectly so to be sure that the link will actually be clickable, set textFormat to Text.RichText. There’s also Text.StyledText which can parse tags but will not make the links clickable.

To ‘catch’ when someone clicks on a link, you simply connect to the Text.onLinkActivated signal.

The following snippet shows how you can open a URL in an external application:

Text {
    // Make the code more readable by using a property for the url.
    property string blogUrl: "" +

    property string title: "QtQuick - From Bling Bling To Blink Blink"

    anchors.verticalCenter: parent.verticalCenter
    width: parent.width

    text: "<a href='" + blogUrl + "'>" + title + "</a>"
    textFormat: Text.RichText
    wrapMode: Text.WrapAnywhere
    horizontalAlignment: Text.AlignHCenter

    font.pixelSize: 26

    onLinkActivated: Qt.openUrlExternally(blogUrl)

The onLinkActivated ‘slot’ (which is invoked when the link is clicked) simply calls the Qt.openUrlExternally() function to invoke an external application which can handle the URL, in this case a web browser.

If you are a lucky owner to a Nokia N9 or a N950 you can for example use the following snippet to start the maps application and show a location (this might also work on Symbian^3 phones):

import QtQuick 1.1
import 1.0

Page {

    Button {
        anchors.centerIn: parent

        text: "Click here!"

        onClicked: Qt.openUrlExternally("geo:55.6124,12.9996")


Thanks for reading.

This Post Has One Comment

  1. Thanks for a marvelous posting! I actually enjoyed reading it, you are a great author.I will
    always bookmark your blog and will come back sometime soon. I want to encourage one to continue your great work,
    have a nice holiday weekend!

Leave a Reply