(solved) Screenshot in qml



  • Hello, i'm new to qt and i'm trying to experiment screen capturing in qml, but i'm getting many errors. Can anyone help? Here's the code.
    Update: I've correct some errors but doesn't work yet.
    Main.cpp

    @#include <QtGui/QApplication>
    #include "qmlapplicationviewer.h"
    #include "QPixmap"
    #include "QDeclarativeView"
    #include "QDeclarativeContext"
    class Snap : public QObject
    {
    Q_OBJECT
    public:
    Q_INVOKABLE void shoot() {
    QPixmap screenShot;
    QPainter p(&screenShot);
    [view]->render(&p);
    }
    };
    int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

         QDeclarativeView view;
    
         Snap data;
         view.rootContext()->setContextProperty("snap", &data);
    
         view.setSource(QUrl::fromLocalFile&#40;"main.qml"&#41;;
         view.show(&#41;;
    
         return app.exec&#40;&#41;;
     }@
    

    main.qml

    @import QtQuick 1.1
    import com.nokia.symbian 1.1

    Page {
    id: mainPage
    Button {
    id: button1
    x: 119
    y: 187
    text: "Button"
    onClicked: snap.shoot()
    }
    }@


  • Moderators

    [quote author="daljit97" date="1339960006"]but i'm getting many errors.[/quote]

    Any news on what the errors actually are?


  • Moderators

    Line 11. is very, very strange. Why do you pass the QDeclarativeView to ::grabWidget()? Should it not be a real object? And why do you return QPixMap and then not do anything with it (line 11 of QML file)?



  • @bool QPixmap::save(QString file);@ returns bool, not QPixmap.



  • Thanks. I have done many errors because i'm new to c++ and qt. I've tried Peppy code but it anyhow shows these errors: invalid use of QPixmap::save; expected primary expressions before 'bool'.



  • I would suggest you to look firstly at pure C++ basics of OOP, because this is not good way how to teach you, how to use C++ neither Qt/QML...

    We could teach you here basics of language C++, but it's not good idea, because there are tons of tutorials and basics of C++ on the Internet...you should google it first.

    I think, tutorials will be better than "try and hope" programming.

    The whole function written above is logically wrong... that's all what I would say to that. And to that error. You can not use QDeclarativeView as stand-alone name for something - that's the reason why it was expecting something more (name of variable) - that is one of the basics for C++.

    Best regards,
    Peppy.



  • I was trying it because i need it in qml app, but thanks anyhow.



  • --Update: I’ve corrected some errors but doesn’t work yet.



  • how does it look like after you have rewritten it?



  • I wrote a class to acquire a screenshot in QML once. It's designed for QtQuick 1.1, I haven't updated it for 2.0. Here's the code:

    screensnapper.h
    @
    #ifndef SCREENSNAPPER_H
    #define SCREENSNAPPER_H

    #include <QDeclarativeImageProvider>

    class ScreenSnapper : public QDeclarativeImageProvider
    {
    public:
    ScreenSnapper();

    QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize);
    

    };

    #endif // SCREENSNAPPER_H
    @

    screensnapper.cpp
    @

    #include "screensnapper.h"

    #include "QApplication"
    #include "QDesktopWidget"

    ScreenSnapper::ScreenSnapper()
    : QDeclarativeImageProvider(QDeclarativeImageProvider::Pixmap)
    {
    }

    QPixmap ScreenSnapper::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
    {
    *size = QApplication::desktop()->size();
    return QPixmap::grabWindow(QApplication::desktop()->winId());
    }
    @

    Use it in QML like so:
    @
    Image {
    id: desktopSnapshot
    source: "image://snapper/snapshot"
    anchors.fill: parent
    }
    @

    See how that does for you. :)

    EDIT: Oops, I forgot the code to register it... Assuming your QDeclarativeView is called view, you'll need to do this before you load the QML:
    @view->engine()->addImageProvider(QString("snapper"), new ScreenSnapper());@



  • thank you very much, but i'm getting this little error(I'm getting errors and errors also with little things these days): base operand of '->' has non-pointer type 'QDeclarativeView'
    Here's code.

    @#include <QtGui/QApplication>
    #include "qmlapplicationviewer.h"
    #include "screensnapper.h"
    Q_DECL_EXPORT int main(int argc, char *argv[])
    {
    QScopedPointer<QApplication> app(createApplication(argc, argv));

    QmlApplicationViewer viewer;
    viewer.setMainQmlFile&#40;QLatin1String("qml/untitled18/main.qml"&#41;);
    viewer.showExpanded();
    QDeclarativeView view;
    view->engine()->addImageProvider(QString("snapper"), new ScreenSnapper());
    return app->exec&#40;&#41;;
    

    }@



  • OK, that's not going to work; you're creating a worthless QDeclarativeView. You need to get the engine from the actual QML viewer and set the image provider before loading the QML, like so:

    @
    #include <QtGui/QApplication>
    #include "qmlapplicationviewer.h"
    #include "screensnapper.h"
    Q_DECL_EXPORT int main(int argc, char *argv[])
    {
    QScopedPointer<QApplication> app(createApplication(argc, argv));

    QmlApplicationViewer viewer;
    viewer.engine()->addImageProvider(QString("snapper"), new ScreenSnapper());
    viewer.setMainQmlFile&#40;QLatin1String("qml/untitled18/main.qml"&#41;);
    viewer.showExpanded();
    
    return app->exec&#40;&#41;;
    

    }
    @

    The error you received was simply pointing out that you were using the dereferencing member selection operator (->) when you didn't have a pointer to an object, you just had the object. In that case, you would need to use the normal member selection operator, which is a dot.



  • I've already tried it before your post but it was showing this errors(it's a malediction): -invalid use of incomplete type 'structQDeclarativeEngine'(line 9) & -forward declaration of 'structQDeclarativeEngine'



  • Now it's angry because the compiler doesn't actually know what QDeclarativeEngine is -- you need to include the header for it:

    @#include <QDeclarativeEngine>@



  • Error: QML Image: Failed to get image from provider: image://snapper/snapshot.
    P.S. : thanks to be so patient.
    EDIT: It works perfectly on a symbian phone. It doesn't work in Qtsimulator.



  • Any way to save it to the phone memory?



  • You can look into the QtMobility stuff, but I would just write that in C++ personally.



  • this is simple code from which i get snapshot

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    QDeclarativeView *view = new QDeclarativeView();
    

    view->setSource(QUrl::fromLocalFile("inputQMLFile.qml")); //inputQMLFile.qml instead your QML file that will generate Image

    QPixmap::grabWidget(view).save("outputFile.png"); //Output file where you want to store the image
    }


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.