+ Reply to Thread
Results 1 to 2 of 2

Thread: How to take ScreenShot Qt/QML

  1. #1
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to take ScreenShot Qt/QML

    This is the example of QtQuick1
    http://www.developer.nokia.com/Commu...eenShot_Qt/QML
    I can’t make it work on QtQuick2

    screenCapture.hpp
    Qt Code:
    1. #include <QObject>
    2.  
    3. class QString;
    4. class QQuickView;
    5.  
    6. class screenCapture : public QObject
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit screenCapture(QQuickView *parent = 0);
    11.  
    12. public slots:
    13. void capture(QString const &path) const;
    14.  
    15. private:
    16. QQuickView *currentView_;
    17. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. screenCapture.cpp
    2.  
    3. #include <QPixmap>
    4. #include <QQuickView>
    5. #include <QString>
    6.  
    7. #include "screenCapture.hpp"
    8.  
    9. screenCapture::screenCapture(QQuickView *currentView) :
    10. QObject(0), currentView_(currentView)
    11. {
    12. }
    13.  
    14. void screenCapture::capture(QString const &path) const
    15. {
    16. QPixmap::grabWidget(currentView_).save(path);
    17. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. main.cpp
    2.  
    3. #include <QGuiApplication>
    4. #include <QQuickPaintedItem>
    5. #include <QQuickView>
    6. #include <QQmlContext>
    7.  
    8. #include "screenCapture.hpp"
    9.  
    10. int main(int argc, char *argv[])
    11. {
    12. QGuiApplication app(argc, argv);
    13.  
    14. qmlRegisterType<screenCapture>("Image", 1, 0, "ScreenCapture");
    15. qmlRegisterType<saveAbleImage>("Image", 1, 0, "SaveAbleImage");
    16.  
    17. QQuickView view;
    18. view.setResizeMode(QQuickView::SizeRootObjectToView);
    19. view.setSource(QStringLiteral("/Users/Qt/program/experiment_apps_and_libs/funnyCamera/qml/funnyCamera/main.qml"));
    20. view.show();
    21.  
    22. screenCapture screenClass(&view);
    23. view.rootContext()->setContextProperty("screenObject", &screenClass);
    24.  
    25. return app.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. main.qml
    2.  
    3. import QtQuick 2.0
    4.  
    5. Rectangle{
    6. id : root
    7. width : 1024
    8. height : 768
    9.  
    10. MouseArea{
    11. anchors.fill: root
    12.  
    13. onClicked: {
    14. console.log("save image")
    15. screenObject.capture("Pictures/saveTest.jpg")
    16. }
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    error message

    QPixmap::grabWidget is deprecated, use QWidget::grab() instead
    QMetaObject::invokeMethod: No such method QQuickView::grab(QRect)


    Added after 1 2 minutes:


    I found a solution, pretty simple, change the function “capture” to
    Qt Code:
    1. void screenCapture::capture(QString const &path) const
    2. {
    3. QImage img = currentView_->grabWindow();
    4. img.save(path);
    5. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by stereoMatching; 13th June 2013 at 17:08.

  2. #2
    Join Date
    Feb 2013
    Location
    India
    Posts
    153
    Thanks
    27
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to take ScreenShot Qt/QML

    Have a look at grabWindow function of QPixmap class.

    CHEERS

  3. The following user says thank you to karankumar1609 for this useful post:

    stereoMatching (17th June 2013)

+ Reply to Thread

Similar Threads

  1. How to save screenshot?
    By xleniz in forum Qt Programming
    Replies: 1
    Last Post: 4th April 2013, 19:36
  2. Screenshot with no show
    By mpi in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2011, 11:12
  3. Screenshot example doesn't work on Mac OSX
    By Damiano in forum Qt Programming
    Replies: 0
    Last Post: 19th January 2011, 11:17
  4. Take a screenshot of a QGraphicsRectItem
    By yazwas in forum Qt Programming
    Replies: 1
    Last Post: 12th October 2009, 13:14
  5. Screenshot
    By graciano in forum Qt Programming
    Replies: 3
    Last Post: 19th April 2009, 16:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts