QQuickWidget:grabToImage:项目的窗口不可见 [英] QQuickWidget: grabToImage: item's window is not visible

查看:243
本文介绍了QQuickWidget:grabToImage:项目的窗口不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用 Item::grabToImage() qml 方法时遇到问题.无论我指向哪个项目,它总是提示以下错误:

I'm having a problem when I try to use Item::grabToImage() qml method. No matter which item I point to, it always says the following error:

grabToImage:项目的窗口不可见

我也尝试使用名为 rect 的根/顶级项目,但没有奏效.

I tried using the root/toplevel Item named rect too, but it didnt work.

我的目标:我想捕获一个矩形大小的图像,并在其上绘制地图块和多边形

My goal: I want to capture a rectangle sized image with the map tile and polygon draw on it

下面是一个可重现的最小示例

Below there's a minimal reproducible example

import QtQml 2.2
import QtLocation 5.9
import QtPositioning 5.9
import QtQuick 2.0
import QtQuick.Controls 2.4

Item {
    id: rect
    width: 1024
    height: 768

    visible: true

    Plugin {
        id: mapPlugin
        name: "osm"
    }

    Map {
        id: map
        enabled: true
        visible: true
        parent: rect
        gesture.enabled: true
        anchors.fill: parent
        plugin: mapPlugin
        zoomLevel: 14
        activeMapType: supportedMapTypes[3]
    }

    Item {
        anchors.bottom: parent.bottom
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.margins: 10
        height: 40

        Button {
            id: saveToDisk
            text: qsTr("Pick")
            onClicked: {
                map.grabToImage(function (result) {
                    console.log('saving to disk..')
                    result.saveToFile("pick.png")
                })
            }
        }
    }
}

#include <QApplication>
#include <QQmlApplicationEngine>
#include <QtQuickWidgets/QQuickWidget>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QApplication app(argc, argv);

    QQuickWidget *q = new QQuickWidget;
    q->setResizeMode(QQuickWidget::SizeRootObjectToView);
    q->setSource(QUrl("main.qml"));
    q->show();

    return app.exec();
}

推荐答案

QQuickWidget 的绘画策略是创建一个屏幕外的 QQuickWindow,它从截取屏幕截图的位置渲染 QML 并将其绘制到小部件上.以上限制了 grabToImage() 的使用,因为此方法要求项目的 QQuickWindow 可见.

The strategy of QQuickWidget for painting is to create an off-screen QQuickWindow that renders the QML from where a screenshot is taken and drawn onto the widget. The above limits the use of grabToImage() since this method requires that the QQuickWindow of the items be visible.

解决办法是使用QQuickView+ QWidget::createWindowContainer():

The solution is to use QQuickView + QWidget::createWindowContainer():

#include <QApplication>
#include <QWidget>
#include <QQuickView>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QApplication app(argc, argv);

    QQuickView *q = new QQuickView;
    q->setResizeMode(QQuickView::SizeRootObjectToView);
    q->setSource(QUrl("main.qml"));

    QWidget * container = QWidget::createWindowContainer(q);
    container->show();

    return app.exec();
}

这篇关于QQuickWidget:grabToImage:项目的窗口不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆