翻譯|使用教程|編輯:鮑佳佳|2021-02-20 10:12:56.480|閱讀 246 次
概述:一個(gè)適用于觸摸設(shè)備的QML應(yīng)用程序,它使用一個(gè)帶有FolderListModel的Repeater來(lái)訪問(wèn)文件夾中的內(nèi)容,以及一個(gè)包含MouseArea的PinchArea來(lái)處理獲取內(nèi)容上的捏合手勢(shì)。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Qt相關(guān)組件:
Qt快速演示-Photo Surface
一個(gè)適用于觸摸設(shè)備的QML應(yīng)用程序,它使用一個(gè)帶有FolderListModel的Repeater來(lái)訪問(wèn)文件夾中的內(nèi)容,以及一個(gè)包含MouseArea的PinchArea來(lái)處理獲取內(nèi)容上的捏合手勢(shì)。
		 
 
	
Photo Surface演示了如何使用帶有FolderListModel和FileDialog的Repeater來(lái)訪問(wèn)用戶選擇的文件夾中的圖像,以及如何使用包含MouseArea的PinchArea處理同一項(xiàng)目?jī)?nèi)的拖動(dòng),旋轉(zhuǎn)和收縮縮放。
所有應(yīng)用程序代碼都包含在一個(gè)QML文件photosurface.qml中。內(nèi)聯(lián)JavaScript代碼用于在照片表面上放置,旋轉(zhuǎn)和縮放圖像。
運(yùn)行示例
要從Qt Creator運(yùn)行示例,請(qǐng)打開“歡迎”模式,然后從“示例”中選擇示例。
創(chuàng)建主窗口
要為Photo Surface應(yīng)用創(chuàng)建主窗口,我們使用Window QML類型作為根項(xiàng)目。它會(huì)自動(dòng)設(shè)置與Qt Quick圖形類型一起使用的窗口:
Window {
    id: root
    visible: true
    width: 1024; height: 600
    color: "black"
    property int highestZ: 0
    property real defaultSize: 200
    property var currentFrame: undefined
要使用Window類型,我們必須導(dǎo)入:
import QtQuick.Window 2.1
訪問(wèn)文件夾內(nèi)容
我們將Repeater QML類型與FolderListModel一起使用,以顯示位于文件夾中的GIF,JPG和PNG圖像:
        Repeater {
            model: FolderListModel {
                id: folderModel
                objectName: "folderModel"
                showDirs: false
                nameFilters: imageNameFilters
            }
要使用FolderListModel類型,我們必須導(dǎo)入:
import Qt.labs.folderlistmodel 1.0
我們使用FileDialog使用戶能夠選擇包含圖像的文件夾:
    FileDialog {
        id: fileDialog
        title: "Choose a folder with some images"
        selectFolder: true
        folder: picturesLocation
        onAccepted: folderModel.folder = fileUrl + "/"
    }
要使用FileDialog類型,我們必須導(dǎo)入Qt快速對(duì)話框:
import QtQuick.Dialogs 1.0
fileDialog.open()當(dāng)應(yīng)用啟動(dòng)時(shí),我們使用該功能打開文件對(duì)話框:
Component.onCompleted: fileDialog.open()
用戶還可以單擊文件對(duì)話框圖標(biāo)以打開文件對(duì)話框。我們使用Image QML類型來(lái)顯示圖標(biāo)。在Image類型內(nèi)部,我們使用帶有信號(hào)處理程序的MouseAreaonClicked來(lái)調(diào)用該fileDialog.open()函數(shù):
在照片表面上顯示圖像
我們使用Rectangle作為Repeater的委托,為FolderListModel在選定文件夾中找到的每個(gè)圖像提供框架。我們使用JavaScriptMath()方法將框架隨機(jī)放置在照片表面上,并以任意角度旋轉(zhuǎn)它們,以及縮放圖像:
  Image {
        anchors.top: parent.top
        anchors.left: parent.left
        anchors.margins: 10
        source: "resources/folder.png"
        MouseArea {
            anchors.fill: parent
            anchors.margins: -10
            onClicked: fileDialog.open()
            hoverEnabled: true
            onPositionChanged: {
                tooltip.visible = false
                hoverTimer.start()
            }
            onExited: {
                tooltip.visible = false
                hoverTimer.stop()
            }
處理捏合手勢(shì)
我們?cè)谙嗫蛑惺褂靡粋€(gè)包含MouseArea的PinchArea來(lái)處理相框的拖動(dòng)、旋轉(zhuǎn)和捏合縮放。  Rectangle {
                id: photoFrame
                width: image.width * (1 + 0.10 * image.height / image.width)
                height: image.height * 1.10
                scale: defaultSize / Math.max(image.sourceSize.width, image.sourceSize.height)
                Behavior on scale { NumberAnimation { duration: 200 } }
                Behavior on x { NumberAnimation { duration: 200 } }
                Behavior on y { NumberAnimation { duration: 200 } }
                border.color: "black"
                border.width: 2
                smooth: true
                antialiasing: true
                Component.onCompleted: {
                    x = Math.random() * root.width - width / 2
                    y = Math.random() * root.height - height / 2
                    rotation = Math.random() * 13 - 6
                }
我們使用pinchgroup屬性來(lái)控制相框?qū)δ蠛鲜謩?shì)的反應(yīng)。該pinch.target組photoFrame的項(xiàng)目來(lái)操作。旋轉(zhuǎn)屬性指定可以在所有角度旋轉(zhuǎn)框架,而縮放屬性指定可以在0.1和之間縮放它們10。
在MouseArea的onPressed信號(hào)處理程序中,我們通過(guò)增加其z屬性的值來(lái)將所選相框提升到頂部。根項(xiàng)存儲(chǔ)最上面一幀的z值。在onEntered信號(hào)處理程序中控制相框的邊框顏色以突出顯示所選圖像:
  PinchArea {
                    anchors.fill: parent
                    pinch.target: photoFrame
                    pinch.minimumRotation: -360
                    pinch.maximumRotation: 360
                    pinch.minimumScale: 0.1
                    pinch.maximumScale: 10
                    pinch.dragAxis: Pinch.XAndYAxis
                    onPinchStarted: setFrameColor();
為了使您能夠在桌面上測(cè)試示例,我們使用MouseArea的onWheel信號(hào)處理程序通過(guò)使用鼠標(biāo)來(lái)模擬捏手勢(shì):
          MouseArea {
                        id: dragArea
                        hoverEnabled: true
                        anchors.fill: parent
                        drag.target: photoFrame
                        scrollGestureEnabled: false  // 2-finger-flick gesture should pass through to the Flickable
                        onPressed: {
                            photoFrame.z = ++root.highestZ;
                            parent.setFrameColor();
                        }
                        onEntered: parent.setFrameColor();
onWheel信號(hào)處理程序在響應(yīng)鼠標(biāo)滾輪手勢(shì)時(shí)被調(diào)用。使用垂直滾輪來(lái)縮放和Ctrl鍵以及垂直滾輪來(lái)旋轉(zhuǎn)幀。如果鼠標(biāo)有一個(gè)水平滾輪,則使用它來(lái)旋轉(zhuǎn)幀。
 onWheel: {
                            if (wheel.modifiers & Qt.ControlModifier) {
                                photoFrame.rotation += wheel.angleDelta.y / 120 * 5;
                                if (Math.abs(photoFrame.rotation) < 4)
                                    photoFrame.rotation = 0;
                            } else {
                                photoFrame.rotation += wheel.angleDelta.x / 120;
                                if (Math.abs(photoFrame.rotation) < 0.6)
                                    photoFrame.rotation = 0;
                                var scaleBefore = photoFrame.scale;
                                photoFrame.scale += photoFrame.scale * wheel.angleDelta.y / 120 / 10;
                            }
                        }
					本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@ke049m.cn
文章轉(zhuǎn)載自: