2015-10-16 105 views
1

我想这里显示的代码:How to take ScreenShot Qt/QMLQQuickWindow :: grabWindow:场景图已经在使用

在执行时我正在写在标题中的错误。

的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") 
     } 
    } 
} 

是什么错误呢?有什么办法解决它?我可以在这里提供什么信息?

+0

你可以提供你的main.qml。最小化为SSCCE,重现该问题。 –

+0

@SaZ我可能没有提供。它不包含任何非常重要的内容。现在,我会像你说的那样在这里。 –

+0

@SaZ完成。什么是SSCCE? –

回答

0

我已经张贴在QtCenter.org too, and got the enlightenment from there.

My I ask why you want to create a screenshot of an empty QQuickView? Wouldn't you want to take the screenshot of the Window you create in QML?

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

我的计划的另一部分是利用QQuickWindow代替QQuickView,所以我换成QQuickViewQQuickWindow如下:

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; 
} 

的main.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侧,我们可以直接使用

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

任何地方,我们想要的。有用。