QQuickWidget 抓图 [英] QQuickWidget grab image

查看:11
本文介绍了QQuickWidget 抓图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在保存带有多个 QML 子级的 QQuickWidget 的图像,但我只有一张空白图像.

I am saving an image of a QQuickWidget with several QML children but all I have is a blank image.

C++ 方面:

QQuickWidget* content..
content->setSource(QUrl("qml:/main.qml"));
QPixmap *pm = content->grab(QRect(QPoint(0,0),QSize(-1,-1));
pm->save("someFilename.png", 0, 100);

QML 方面:

Rectangle{  width: 5; height: 5; color: "yellow"; objectname: "rootobj"}

在 QML 中,我希望动态添加子元素并能够在图像中显示它们.我尝试了 QQuickWindow grabWindow 方法与插槽的连接,它可以工作,但它只捕获窗口可见区域,我需要捕获整个 QML.

In the QML I wish to dynamically add children and be able to show them in the image. I have tried QQuickWindow grabWindow method with a connection to a slot and it works but it captures only the window visible area and I need to capture the whole QML.

我相信这不是火箭科学,只是我没有得到它.感谢您的回复!

I believe this is not rocket science just that I am not getting it somewhere. Thanks for your replies!

附录:

好的,我不认为这是渲染前后的问题,因为在调用图片抓取器之前我可以看到所有 qml 子项.很抱歉不准确.

Ok, I do not think its the issue of before/after rendering since I can see all the qml children before I call the picture grabber. So sorry for not being precise.

c++端:

QQuickWidget* content..
content->setSource(QUrl("qml:/main.qml"));
//do all my dynamic qml children adding

在我可以直观地看到我所有的 qml 之后:

After I can visually see all my qml:

QPixmap *pm = content->grab(QRect(QPoint(0,0),QSize(-1,-1));
pm->save(....

除非我错了,否则我不认为它的渲染问题.谢谢!

Unless I am wrong, I dont think its rendering issue. Thank you!

推荐答案

问题就像美度说的.你可以像下面这样解决它.

Issue is like Mido said. You can solve it like follows.

创建一个类Viewer:

class Viewer : public QQuickView{
    Q_OBJECT
public:
    explicit Viewer(QWindow *parent = 0);
    Viewer(bool showBar);
    virtual ~Viewer();
    void setMainQmlFile(const QString file);
    void addImportPath(const QString path);

public slots:
    void beforeRendering();
    void afterRendering()
}

查看器.cpp

#include "viewer.h"

Viewer::Viewer(QWindow *parent)
    : QQuickView(parent)
{
    setWidth(800);
    setHeight(480);
    connect(this, SIGNAL(beforeRendering()), this, SLOT(beforeRendering()));
    connect(this, SIGNAL(afterRendering()), this, SLOT(afterRendering()));
}

void Viewer::setMainQmlFile(const QString file)
{
    setSource(QUrl::fromLocalFile(file));
}

void Viewer::addImportPath(const QString path)
{
    engine()->addImportPath(path);
}
void Viewer::beforeRendering()
{
//
}

void Viewer::afterRendering()
{
//grab window
  QImage img = this->grabWindow();
    img.save(path);
//or your code
}

main.cpp

Viewer *viewer = new Viewer;
//
///
//
viewer->setMainQmlFile(QStringLiteral("qml/main.qml"));
viewer->show();

这篇关于QQuickWidget 抓图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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