Garbatoymange sev te delgate pathavyu Kamal

grabToImage save the delagate pathview qml

你好我的问题是以下我正在尝试遍历 pathview 委托但是我收到一个错误,它在保存路径后找不到我想要保存的图像。 我已经使用 grabToImage 来保存 canvas、图形和其他一些东西,但没有使用 pathview 委托。

我找到的关于 grabToImage 的所有内容都与找到解决这个特定问题的方法无关。

如果您能指导我或帮助我,我将不胜感激

import QtQuick 2.15
import QtQuick.Controls 2.12
import QtQuick.Window 2.2
import Qt.labs.platform 1.1

Window {
    width: 600
    height: 600
    
    visible: true
    
    ListModel {      
        id: modelcon
        ListElement { img: "test.jpg" }
        ListElement { img: "test.jpg" }
        ListElement { img: "test.jpg" }
        ListElement { img: "test.jpg" }
        ListElement { img: "test.jpg" }
        ListElement { img: "test.jpg" }
        ListElement { img: "test.jpg" }
    }
    
    PathView {
        id: imgView
        
        clip: true
        focus: true
        interactive: true
        pathItemCount: 3
        
        model: modelcon
        
        preferredHighlightEnd: 0.5
        preferredHighlightBegin: 0.5
        
        anchors.fill: parent
        
        function nextItem() {
            imgView.incrementCurrentIndex();
        }
        
        function preItem() {
            imgView.decrementCurrentIndex();
        }
        
        function toItem(index) {
            if (index <= imgView.itemCount && imgView.currentIndex !== index) {
                imgView.currentIndex = index;
            }
        }
        
        delegate: Image {
            x: (parent.width - width)/ 2
            y: (parent.height - height)/ 2
            z: PathView.iconOrder
            
            width: parent.width* 0.9
            height: parent.width * 0.4
            
            property bool rounded: true
            property bool adapt: true
            
            clip: true
            
            source: img
            
            scale: PathView.iconScale
            opacity: PathView.iconOpacity
        }

        path: Path {
            startX: 0
            startY: imgView.height * 0.5
            
            PathAttribute { name: "iconScale"; value: 0.2 }
            PathAttribute { name: "iconOpacity"; value: 10.2 }
            PathAttribute { name: "iconOrder"; value: 0 }
            PathLine {x: imgView.width / 2; y: imgView.height/2 }
            PathAttribute { name: "iconScale"; value: 1 }
            PathAttribute { name: "iconOpacity"; value: 1 }
            PathAttribute { name: "iconOrder"; value: 8 }
            PathLine {x: imgView.width; y: imgView.height/2 }
        }
        Button {
            id: btnOpen
            
            visible :true
            
            width: 80
            height: 80
            
            text: "Save"
            
            anchors.verticalCenterOffset: 0
            
            background: Rectangle {
                color: "#00000000"
            }
            onClicked:{
                filedialogSave.open()
            }
        }
    }
    
    FileDialog {
        id: filedialogSave;
        
        title: "Save image";
        
        nameFilters: ["Image Files (*.png)", "Image Files(*.jpg)"];
        
        visible: false
        fileMode: FileDialog.SaveFile
        
        onAccepted: {
            let ruta = currentFile.toString().replace('file: ///', '')
            
            imgView.grabToImage(function(result) {
                result.saveToFile(ruta)
            });
            
            filedialogSave.close()
        }
    }
}

如果您想访问代表图像,请将您的代表放入项目中,然后 断言 属性 指向您的代表的模型数据。

    delegate:
        Item {
        property var modelData:model
        Image {
            x: (parent.width - width)/ 2
            y: (parent.height - height)/ 2
            z: PathView.iconOrder

            width: parent.width* 0.9
            height: parent.width * 0.4

            property bool rounded: true
            property bool adapt: true

            clip: true

            source: img

            scale: PathView.iconScale
            opacity: PathView.iconOpacity
        }
    }

像这样保存时访问您的图像:

imgView.currentItem.modelData.img

我找到了解决方案

    function grabToImage(item, fileName) {
    item.grabToImage(function(image) {
        image.saveToFile(fileName);
    });
   }

包括迭代委托外部的项目及其父“项目”,后者构成所有 QtQuick.Controls 组件。

grabToImage( id , "filename.extension") 

它被放置在主父级中,然后通常会调用 grabToImage( id , "filename.extension")。