图片浏览器

import QtQuick 2.2
import QtQuick.Window 2.2
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2
import QtQuick.Dialogs 1.1
Window {
    visible: true
    width:600;
    height:480;
    minimumWidth: 480;
    minimumHeight: 380;

    BusyIndicator{
           id:busy;
           running:true;
           anchors.centerIn: parent;
           z:2;
       }

    Text {
        id:stateLable;
        visible: false;
        anchors.centerIn: parent;
        z:3;
    }
    Image{
        id:imageViewer;
        asynchronous: true;//只有想要异步加载本地资源时才需要设置它
        cache:false;//告诉Image不用缓存图片
        anchors.fill: parent;
        fillMode:Image.PreserveAspectFit;//等比缩放模式
        //信号处理器,Image的status属性变化时会发射satusChanged()信号
        onStatusChanged: {
            if(imageViewer.states ===Image.Loading){
                busy.running = true;
                stateLabel.visible = false;
            }
            else if(imageViewer.status === Image.Ready){
                busy.running = false;
            }
            else if(imageViewer.status === Image.Error){
                busy.running = false;
                stateLabel.visible = true;
                stateLabel.text = "ERROR";
            }
        }
    }
    Button{
        id:openFile;
        text:"Open";
        anchors.left: parent.left;
        anchors.leftMargin: 8;
        anchors.bottom: parent.bottom;
        anchors.bottomMargin: 8;
        style:ButtonStyle{
            background: Rectangle{
                implicitHeight: 25;
                implicitWidth: 70;
                color:control.hovered?"#c0c0c0":"#a0a0a0";
                border.width: control.pressed ? 2 : 1;
                border.color: (control.hovered||control.pressed)
                              ?"green":"#888888";
            }
        }
        onClicked: fileDialog.open();
        z:4;
    }
    Text{
        id:imagePath;
        anchors.left: openFile.right;
        anchors.leftMargin: 8;
        anchors.verticalCenter: openFile.verticalCenter;
        font.pixelSize: 18;
    }
    FileDialog{
        id:fileDialog;
        title:"Please choose a file";
        nameFilters: ["Image Files(*.jpg *.png *.gif)"];
        onAccepted: {
            imageViewer.source = fileDialog.fileUrl;
            var imageFile = new String(fileDialog.fileUrl);
            imagePath.text = imageFile.slice(8);
        }
    }
}

盛世清平~Qt quick学习笔记_05

相关文章: