C++ QQuickWindow::grabWindow:场景图已在使用中
我尝试了下面显示的代码: 执行时,我会在标题中写入错误 我的main.cpp是:C++ QQuickWindow::grabWindow:场景图已在使用中,c++,qt,qml,qt5,qtquick2,C++,Qt,Qml,Qt5,Qtquick2,我尝试了下面显示的代码: 执行时,我会在标题中写入错误 我的main.cpp是: #include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQuickWindow> #include <QImage> #include <QDebug> #include "screenshot.h" #include <QQuickView> #includ
#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();
}
并在构造函数中添加了以下内容:
currentView_ = new QQuickView;
Mymain.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")
}
}
}
这个错误是什么意思?解决这个问题的办法是什么?我还可以在这里提供什么信息?我已经将这个问题发布在 我的问题是,为什么要创建空QQuickView的屏幕截图? 您不想拍摄在QML中创建的窗口的屏幕截图吗 我意识到我没有在
main.cpp
的任何地方使用QQuickView
。这意味着我使用的QQuickView
是空的
我的程序的另一部分是使用QQuickWindow
而不是QQuickView
,因此我将QQuickView
替换为QQuickWindow
,如下所示:
屏幕截图.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
\ifndef屏幕截图
#定义屏幕截图
#包括
类QString;
类QQuickWindow;
类截图:公共QObject
{
Q_对象
公众:
显式屏幕截图(QQuickWindow*父项=0);
公众时段:
无效捕获(QString常量和路径)常量;
私人:
QQuickWindow*当前窗口;
};
#endif//屏幕截图
screenscapture.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;
}
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();
}
#包括
#包括
#包括
#包括“screenshot.h”
截屏::截屏(QQuickWindow*currentWindow):
QObject(0),currentWindow(currentWindow)
{
}
无效屏幕捕获::捕获(QString常量和路径)常量
{
QImage p=currentWindow->grabWindow();
bool kk=p.save(路径);
qDebug()setContextProperty(“屏幕截图”和启动器);
窗口->显示();
返回app.exec();
}
现在,在QML端,我们可以直接使用
ScreenShot.capture(“/home/*****/Desktop/l.png”)
我们想去哪里就去哪里。它是有效的。我已经把这个问题贴在了 我的问题是,为什么要创建空QQuickView的屏幕截图? 您不想拍摄在QML中创建的窗口的屏幕截图吗 我意识到我没有在
main.cpp
的任何地方使用QQuickView
。这意味着我使用的QQuickView
是空的
我的程序的另一部分是使用QQuickWindow
而不是QQuickView
,因此我将QQuickView
替换为QQuickWindow
,如下所示:
屏幕截图.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
\ifndef屏幕截图
#定义屏幕截图
#包括
类QString;
类QQuickWindow;
类截图:公共QObject
{
Q_对象
公众:
显式屏幕截图(QQuickWindow*父项=0);
公众时段:
无效捕获(QString常量和路径)常量;
私人:
QQuickWindow*当前窗口;
};
#endif//屏幕截图
screenscapture.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;
}
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();
}
#包括
#包括
#包括
#包括“screenshot.h”
截屏::截屏(QQuickWindow*currentWindow):
QObject(0),currentWindow(currentWindow)
{
}
无效屏幕捕获::捕获(QString常量和路径)常量
{
QImage p=currentWindow->grabWindow();
bool kk=p.save(路径);
qDebug()setContextProperty(“屏幕截图”和启动器);
窗口->显示();
返回app.exec();
}
现在,在QML端,我们可以直接使用
ScreenShot.capture(“/home/*****/Desktop/l.png”)
我们想去哪里就去哪里。它可以工作。您可以提供main.qml。最小化到一个SSCCE,这重现了那个问题。@SaZ我没有提供。它没有包含任何非常重要的内容。“现在,我会照你说的在这里完成的。”萨兹说。什么是SSCCE?我不确定,但是试着给你的
screenscapture::capture
方法加一个断言:Q_断言(QThread::currentThread()==qApp->thread())代码>。文档中有一些注释:@SaZ如何在捕获方法中获取qApp对象。该对象位于main.cpp中。不是吗?您可以提供main.qml。最小化到一个SSCCE,这重现了那个问题。@SaZ我没有提供。它没有包含任何非常重要的内容。“现在,我会照你说的在这里完成的。”萨兹说。什么是SSCCE?我不确定,但是试着给你的screenscapture::capture
方法加一个断言:Q_断言(QThread::currentThread()==qApp->thread())代码>。文档中有一些注释:@SaZ如何在捕获方法中获取qApp对象。该对象位于main.cpp中。不是吗?