首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > QT开发 >

抓下qml2的screen shot,该怎么解决

2013-07-09 
抓下qml2的screen shot过去的做法http://www.developer.nokia.com/Community/Wiki/How_to_take_ScreenShot

抓下qml2的screen shot
过去的做法

http://www.developer.nokia.com/Community/Wiki/How_to_take_ScreenShot_Qt/QML

很遗憾,这招在qml2中是行不通的
我的试验

screenCapture.hpp


    #include <QObject>
     
    class QString;
    class QQuickView;
     
    class screenCapture : public QObject
    {
        Q_OBJECT
    public:    
        explicit screenCapture(QQuickView *parent = 0);
       
    public slots:
        void capture(QString const &path) const;
     
    private:
        QQuickView *currentView_;    
    };

screenCapture.cpp

    #include <QPixmap>
    #include <QQuickView>
    #include <QString>
     
    #include "screenCapture.hpp"
     
    screenCapture::screenCapture(QQuickView *currentView) :
        QObject(0), currentView_(currentView)
    {
    }
     
    void screenCapture::capture(QString const &path) const
    {
        QPixmap::grabWidget(currentView_).save(path);
    }

main.cpp

    #include <QGuiApplication>
    #include <QQuickPaintedItem>
    #include <QQuickView>
    #include <QQmlContext>
     
    #include "screenCapture.hpp"
     
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
     
        qmlRegisterType<screenCapture>("Image", 1, 0, "ScreenCapture");
        qmlRegisterType<saveAbleImage>("Image", 1, 0, "SaveAbleImage");
     
        QQuickView view;
        view.setResizeMode(QQuickView::SizeRootObjectToView);
        view.setSource(QStringLiteral("/Users/Qt/program/experiment_apps_and_libs/funnyCamera/qml/funnyCamera/main.qml"));
        view.show();
     
        screenCapture screenClass(&view);
        view.rootContext()->setContextProperty("screenObject", &screenClass);
     
        return app.exec();
    }

main.qml


感谢楼主分享 

热点排行