【问题标题】:Error saving QML item as image to file, using grabToImage使用grabToImage将QML项目作为图像保存到文件时出错
【发布时间】:2016-10-09 03:00:28
【问题描述】:

我有一个 QML 项目,我正在尝试 grab as an image。像这样使用 QML……

Item {
    id: content
    ...
}

FileDialog {
    folder: shortcuts.documents
    selectExisting: false
    onAccepted: {
        content.grabToImage(function(result){
            if (!result.saveToFile(fileUrl)){
              console.error('Unknown error');
            }
        });
    }
}

…我总是收到“未知错误”文本,即使我输入像“Test.png”这样的文件名。

怎么了?如何将我的文件保存到图像中?

【问题讨论】:

    标签: qt qml qt-quick


    【解决方案1】:

    问题是FileDialog 提供的url 作为saveToFile 的参数无效。

    虽然saveToFile method 没有记录所需的参数类型,但the source code 表明它需要QString

    然而,这还不够。您还必须从文件 URL 中删除协议才能使其正常工作。例如,fileUrl.toString() 可能会导致
    "file:///Users/phrogz/Document/Test.png"
    saveToFile 方法需要一个字符串,例如
    "/Users/phrogz/Document/Test.png"

    因此:

    FileDialog {
        folder: shortcuts.documents
        selectExisting: false
        onAccepted: {
            var urlNoProtocol = (fileUrl+"").replace('file://', '');
            content.grabToImage(function(result){
                if (!result.saveToFile(urlNoProtocol)){
                  console.error('Unknown error saving to',urlNoProtocol);
                }
            });
        }
    }
    

    【讨论】:

    • 我已经打开Qt bug #44450 来跟踪这个问题。显然,saveToFile 应该直接接受 URL。
    • 哇,digia 通常不会修复错误好几年,但他们会在你发现它之前整整一年修复你的错误:D
    • @ddriver 哈哈。哎呀。我的意思是bug #56436
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    • 1970-01-01
    • 2020-12-29
    • 1970-01-01
    • 2018-08-10
    • 2022-01-16
    • 1970-01-01
    相关资源
    最近更新 更多