Sunday, August 25, 2013

qml example: load image from internet and handle status changed


import QtQuick 2.0
import QtQuick.Window 2.0

Window {

    title: "Qteveloper(qteveloper.blogspot.com)";

    width: 360
    height: 360
    Text {
        id: myHello
        anchors.horizontalCenter: parent.horizontalCenter
        text: "Hello World"
    }

    Image {
        id: myImage
        source: "http://goo.gl/kdbgF8"
        anchors {
            left: parent.left
            top: myHello.bottom
        }

        onStatusChanged:
            switch(myImage.status){
            case Image.Null:
                myState.text = "no image has been set";
                break;
            case Image.Ready:
                myState.text = "the image has been loaded\n"
                        + myImage.width + " x " + myImage.height;
                break;
            case Image.Loading:
                myState.text = "the image is currently being loaded";
                break;
            case Image.Error:
                myState.text = "an error occurred while loading the image";
                break;
            }
    }

    Text {
        id: myState
        anchors.horizontalCenter: parent.horizontalCenter
        anchors {
            left: myImage.left
            top: myImage.bottom
            leftMargin: 20
            topMargin: 10
        }
    }

}