1. 程式人生 > >使用QML製作超連結並開啟超連結

使用QML製作超連結並開啟超連結

使用QML中的Text來實現超連結並開啟

程式碼如下:

import QtQuick 2.3
import QtQuick.Window 2.2

Window {
    visible: true

    MouseArea {
        anchors.fill: parent
        onClicked: {
            Qt.quit();
        }
    }



    MouseArea{
        anchors.centerIn: parent
        width: hp.width
        height: hp.height
        hoverEnabled: true
        cursorShape: containsMouse ? (pressed ? Qt.ClosedHandCursor : Qt.OpenHandCursor) : Qt.ArrowCursor

        Text {
            id: hp
            text: "<a href='http://www.baidu.com'><h1>點選進入首頁</h1></a>"
            anchors.centerIn: parent
            onLinkActivated: Qt.openUrlExternally(link)
        }
    }

}

只要為text屬性複製上HTML中的<a>元素就可變成超連結,使用Qt.openUrlExternally來開啟它

利用MouseArea來實現滑鼠放上超連結時候的樣式。

看看效果圖:


點選之後就會使用預設瀏覽器開啟http://www.baidu.com

是不是很簡單? over.