【发布时间】:2015-10-16 08:24:39
【问题描述】:
我尝试了这里显示的代码:How to take ScreenShot Qt/QML
在执行时,我收到标题中写的错误。
我的 main.cpp 是:
#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);
}
并在构造函数中添加以下内容:
currentView_ = new QQuickView;
我的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")
}
}
}
这个错误是什么意思?有什么方法可以解决?我还可以在这里提供什么信息?
【问题讨论】:
-
你可以提供你的 main.qml。最小化为 SSCCE,重现该问题。
-
@SaZ 我特意没有提供。它不包含任何非常重要的东西。现在,我会照你说的在这里。
-
@SaZ 完成。什么是 SSCCE?
-
我不确定,但请尝试对您的
screenCapture::capture方法进行断言:Q_ASSERT( QThread::currentThread() == qApp->thread() );。文档中有一些 cmets:doc.qt.io/qt-5/qquickwindow.html#grabWindow -
@SaZ 如何在捕获方法中获取 qApp 对象。该对象在 main.cpp 中。不是吗?