Qt+Quick實現(xiàn)圖片演示器的開發(fā)
介紹
一個適用于觸摸設(shè)備的QML應(yīng)用程序,它使用一個帶有FolderListModel的Repeater來訪問文件夾中的內(nèi)容,以及一個包含MouseArea的PinchArea來處理獲取內(nèi)容上的捏合手勢。
Photo Surface
演示了如何使用帶有FolderListModel和FileDialog的Repeater來訪問用戶選擇的文件夾中的圖像,以及如何使用包含MouseArea的PinchArea處理同一項目內(nèi)的拖動,旋轉(zhuǎn)和收縮縮放。
所有應(yīng)用程序代碼都包含在一個QML文件photosurface.qml中。內(nèi)聯(lián)JavaScript代碼用于在照片表面上放置,旋轉(zhuǎn)和縮放圖像。
運行示例
要從Qt Creator運行示例,請打開“歡迎”模式,然后從“示例”中選擇示例。
創(chuàng)建主窗口
要為Photo Surface應(yīng)用創(chuàng)建主窗口,我們使用Window QML類型作為根項目。它會自動設(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
訪問文件夾內(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快速對話框:
import QtQuick.Dialogs 1.0
fileDialog.open()當(dāng)應(yīng)用啟動時,我們使用該功能打開文件對話框:
Component.onCompleted: fileDialog.open()
用戶還可以單擊文件對話框圖標(biāo)以打開文件對話框。我們使用Image QML類型來顯示圖標(biāo)。在Image類型內(nèi)部,我們使用帶有信號處理程序的MouseAreaonClicked來調(diào)用該fileDialog.open()函數(shù):
在照片表面上顯示圖像
我們使用Rectangle作為Repeater的委托,為FolderListModel在選定文件夾中找到的每個圖像提供框架。我們使用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() }
處理捏合手勢
我們在相框中使用一個包含MouseArea的PinchArea來處理相框的拖動、旋轉(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屬性來控制相框?qū)δ蠛鲜謩莸姆磻?yīng)。該pinch.target組photoFrame的項目來操作。旋轉(zhuǎn)屬性指定可以在所有角度旋轉(zhuǎn)框架,而縮放屬性指定可以在0.1和之間縮放它們10。
在MouseArea的onPressed信號處理程序中,我們通過增加其z屬性的值來將所選相框提升到頂部。根項存儲最上面一幀的z值。在onEntered信號處理程序中控制相框的邊框顏色以突出顯示所選圖像:
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();
為了使您能夠在桌面上測試示例,我們使用MouseArea的onWheel信號處理程序通過使用鼠標(biāo)來模擬捏手勢:
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信號處理程序在響應(yīng)鼠標(biāo)滾輪手勢時被調(diào)用。使用垂直滾輪來縮放和Ctrl鍵以及垂直滾輪來旋轉(zhuǎn)幀。如果鼠標(biāo)有一個水平滾輪,則使用它來旋轉(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; } }
Qt相關(guān)組件:
- QtitanRibbon: 遵循Microsoft Ribbon UI Paradigm for Qt技術(shù)的Ribbon UI組件,致力于為Windows、Linux和Mac OS X提供功能完整的Ribbon組件。
- QtitanChart :是一個C ++庫,代表一組控件,這些控件使您可以快速地為應(yīng)用程序提供漂亮而豐富的圖表。并且支持所有主要的桌面操作系統(tǒng)。
- QtitanDataGrid :這個Qt數(shù)據(jù)網(wǎng)格組件使用純C++創(chuàng)建,運行速度極快,處理大數(shù)據(jù)和超大數(shù)據(jù)集的效果突出。QtitanDataGrid完全集成了QtDesigner,因而極易適應(yīng)其他相似的開發(fā)環(huán)境,保證100%兼容Qt GUI。
到此這篇關(guān)于Qt+Quick實現(xiàn)圖片演示器的開發(fā)的文章就介紹到這了,更多相關(guān)Qt Quick圖片演示器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++ 內(nèi)存分配處理函數(shù)set_new_handler的使用
這篇文章主要介紹了C++ 內(nèi)存分配處理函數(shù)set_new_handler的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02C++函數(shù)的嵌套調(diào)用和遞歸調(diào)用學(xué)習(xí)教程
這篇文章主要介紹了C++函數(shù)的嵌套調(diào)用和遞歸調(diào)用學(xué)習(xí)教程,是C++入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-09-09C++編程之CString、string與、char數(shù)組的轉(zhuǎn)換
這篇文章主要介紹了C++編程之CString、string與、char數(shù)組的轉(zhuǎn)換的相關(guān)資料,希望通過本文能幫助到大家,讓大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下2017-10-10概述C++中的 public protected private friend關(guān)鍵字的用法
這篇文章簡要概述了C++中的 public protected private friend關(guān)鍵字的用法,非常不錯,具有參考借鑒價值,感興趣的朋友一起學(xué)習(xí)吧2016-08-08