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:

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):

Thanks for reading.

Leave a Reply