本文介绍了QQuickWindow::grabWindow:场景图已在使用中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了此处显示的代码:如何使用 ScreenShot Qt/QML

I tried the code shown here: How to take ScreenShot Qt/QML

在执行时,我收到了标题中写的错误.

On execution I am getting the error written in the title.

我的 main.cpp 是:

My main.cpp is:

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickWindow>
#include <QImage>
#include <QDebug>
#include "screenshot.h"
#include <QQuickView>
#include <QQmlContext>

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

    const char* drigUi = "DrigUI";
    qmlRegisterType <screenCapture> (drigUi, 1, 0, "ScreenShot");

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

我使用了这个捕获功能:

void screenCapture::capture(QString const &path) const
{
    QImage img = currentView_->grabWindow();
    img.save(path);
}

并在构造函数中添加以下内容:

and added the following in the constructor:

currentView_ = new QQuickView;

我的main.qml:

My main.qml :

import QtQuick 2.4
import QtQuick.Window 2.2

import DrigUI 1.0

Window
{
    visible: true
    height: 370
    width: 370

    ScreenShot { id: screenShot }

    Rectangle
    {
        id: ll
        height: 30
        width: 50
        x: 180; y: 0; color: "red"
        MouseArea
        {
            anchors.fill: parent
            onClicked: screenShot.capture ("l.png")
        }
    }
}

那个错误是什么意思?有什么方法可以解决?我还能在这里提供什么信息?

What does that error mean? What is the way to solve it? What else info can I provide here?

推荐答案

我已经在 QtCenter.org 也是,并从那里得到启示.

我问你为什么要创建一个空 QQuickView 的屏幕截图?您不想截取您在 QML 中创建的 Window 的屏幕截图吗?

我意识到我没有在 main.cpp 的任何地方使用 QQuickView.所以这意味着我使用的 QQuickView 是空的.

I realized that I wasn't using the QQuickView anywhere in main.cpp. so that meant that QQuickView, I was using, was empty.

我的程序的另一部分是使用 QQuickWindow 而不是 QQuickView,所以我用 QQuickWindow 替换了 QQuickView 作为如下:

My other part of program is using QQuickWindow instead of QQuickView, so I replaced QQuickView with QQuickWindow as follows:

ScreenCapture.h

#ifndef SCREENSHOT_H
#define SCREENSHOT_H

#include <QObject>

class QString;
class QQuickWindow;

class screenCapture : public QObject
{
    Q_OBJECT
public:
    explicit screenCapture (QQuickWindow *parent = 0);

public slots:
    void capture (QString const &path) const;

private:
    QQuickWindow *currentWindow;
};

#endif // SCREENSHOT_H

ScreenCapture.cpp

#include <QPixmap>
#include <QQuickView>
#include <QDebug>
#include "screenshot.h"

screenCapture::screenCapture(QQuickWindow *currentWindow) :
    QObject(0), currentWindow (currentWindow)
{
}

void screenCapture::capture (QString const &path) const
{
    QImage p = currentWindow->grabWindow ();
    bool kk = p.save (path);
    qDebug () << kk;
}

ma​​in.cpp

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

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    QObject      *topLevel = engine.rootObjects().value(0);
    QQuickWindow *window   = qobject_cast<QQuickWindow *>(topLevel);

    screenCapture launcher (window);

    engine.rootContext()->setContextProperty("ScreenShot", &launcher);

    window->show();

    return app.exec();
}

现在,在QML端,我们可以直接使用

Now, in QML side, we can directly use

ScreenShot.capture ("/home/*****/Desktop/l.png")

我们想要的任何地方.它有效.

anywhere we want. It works.

这篇关于QQuickWindow::grabWindow:场景图已在使用中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 15:58