QQuickWidget grab image

大憨熊 提交于 2019-12-02 02:06:21

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

Create a class Viewer:

viewer.h

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()
}

Viewer.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();
Mido

I think your issue is that the capture screen is done before the rendering of the QML object. In order to make it work you should connect the grab of the signal after rendering signal:

 connect(this, SIGNAL(beforeRendering()), this, SLOT(sltBeforeRendering()));
 connect(this, SIGNAL(afterRendering()), this, SLOT(sltAfterRendering()));

do the grab in sltAfterRendering slot.

To grab screen I use the grabWindow() function and I call it from QML. It depends on the behaviour that you want from your software.

Try this:

grabber.h

#ifndef GRABBER_H
#define GRABBER_H

#include <QObject>
#include <QImage>
#include <QQuickView>

class Grabber : public QObject
{
    Q_OBJECT
public:
     explicit Grabber(QObject *parent = 0);
     Grabber(QQuickView *view);
     ~Grabber();
     Q_INVOKABLE void capture(QString const &path) const;
signals:

public slots:
private:
   QQuickView*  view_;

};

#endif // GRABBER_H

grabber.cpp

#include "grabber.h"

Grabber::Grabber(QObject *parent) :
    QObject(parent)
{
}
Grabber::Grabber(QQuickView* view) :
    view_(view)
{
}
Grabber::~Grabber()
{
    if(view_ != NULL)
    {
        delete view_;
        view_ = NULL;
    }
}
void Grabber::capture(QString const &path) const
{
    QImage img = view_->grabWindow();
    img.save(path);
}

main.cpp

#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include <QQmlContext>
#include <QQmlEngine>
#include "grabber.h"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QtQuick2ApplicationViewer *viewer = new QtQuick2ApplicationViewer;
    Grabber * grab = new Grabber(viewer);
    viewer->setHeight(480);
    viewer->setWidth(800);

    viewer->rootContext()->setContextProperty("grab", grab);
    viewer->setMainQmlFile(QStringLiteral("qml/main.qml"));

    viewer->showExpanded();


    return app.exec();
}

Call it from QML with:

grab.capture(path + "imageName.png")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!