June 17, 2012

daljit97 daljit97
Ant Farmer
68 posts

(solved) Screenshot in qml

Page  
1

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

  1. #include <QtGui/QApplication>
  2. #include "qmlapplicationviewer.h"
  3. #include "QPixmap"
  4. #include "QDeclarativeView"
  5. #include "QDeclarativeContext"
  6.     class Snap : public QObject
  7.     {
  8.         Q_OBJECT
  9.     public:
  10.         Q_INVOKABLE void shoot() {
  11.             QPixmap screenShot;
  12.                     QPainter p(&screenShot);
  13.                     [view]->render(&p);
  14.         }
  15.     };
  16.     int main(int argc, char *argv[]) {
  17.          QApplication app(argc, argv);
  18.  
  19.          QDeclarativeView view;
  20.  
  21.          Snap data;
  22.          view.rootContext()->setContextProperty("snap", &data);
  23.  
  24.          view.setSource(QUrl::fromLocalFile("main.qml");
  25.          view.show();
  26.  
  27.          return app.exec();
  28.      }

main.qml

  1. import QtQuick 1.1
  2. import com.nokia.symbian 1.1
  3.  
  4. Page {
  5.     id: mainPage
  6.     Button {
  7.         id: button1
  8.         x: 119
  9.         y: 187
  10.         text: "Button"
  11.         onClicked: snap.shoot()
  12. }
  13. }

16 replies

June 17, 2012

sierdzio sierdzio
Area 51 Engineer
2312 posts
daljit97 wrote:
but i’m getting many errors.

Any news on what the errors actually are?

 Signature 

(Z(:^

June 17, 2012

sierdzio sierdzio
Area 51 Engineer
2312 posts

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)?

 Signature 

(Z(:^

June 17, 2012

Peppy Peppy
Hobby Entomologist
389 posts

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

June 17, 2012

daljit97 daljit97
Ant Farmer
68 posts

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’.

June 18, 2012

Peppy Peppy
Hobby Entomologist
389 posts

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.

June 18, 2012

daljit97 daljit97
Ant Farmer
68 posts
I was trying it because i need it in qml app, but thanks anyhow.

June 18, 2012

daljit97 daljit97
Ant Farmer
68 posts

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

June 18, 2012

Peppy Peppy
Hobby Entomologist
389 posts

how does it look like after you have rewritten it?

June 18, 2012

DigitalPioneer DigitalPione..
Lab Rat
27 posts

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

  1. #ifndef SCREENSNAPPER_H
  2. #define SCREENSNAPPER_H
  3.  
  4. #include <QDeclarativeImageProvider>
  5.  
  6. class ScreenSnapper : public QDeclarativeImageProvider
  7. {
  8. public:
  9.     ScreenSnapper();
  10.  
  11.     QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize);
  12. };
  13.  
  14. #endif // SCREENSNAPPER_H

screensnapper.cpp

  1. #include "screensnapper.h"
  2.  
  3. #include "QApplication"
  4. #include "QDesktopWidget"
  5.  
  6. ScreenSnapper::ScreenSnapper()
  7.     : QDeclarativeImageProvider(QDeclarativeImageProvider::Pixmap)
  8. {
  9. }
  10.  
  11. QPixmap ScreenSnapper::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
  12. {
  13.     *size = QApplication::desktop()->size();
  14.     return QPixmap::grabWindow(QApplication::desktop()->winId());
  15. }

Use it in QML like so:

  1.     Image {
  2.         id: desktopSnapshot
  3.         source: "image://snapper/snapshot"
  4.         anchors.fill: parent
  5.     }

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:

  1. view->engine()->addImageProvider(QString("snapper"), new ScreenSnapper());

June 18, 2012

daljit97 daljit97
Ant Farmer
68 posts

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.

  1. #include <QtGui/QApplication>
  2. #include "qmlapplicationviewer.h"
  3. #include "screensnapper.h"
  4. Q_DECL_EXPORT int main(int argc, char *argv[])
  5. {
  6.     QScopedPointer<QApplication> app(createApplication(argc, argv));
  7.  
  8.     QmlApplicationViewer viewer;
  9.     viewer.setMainQmlFile(QLatin1String("qml/untitled18/main.qml"));
  10.     viewer.showExpanded();
  11.     QDeclarativeView view;
  12.     view->engine()->addImageProvider(QString("snapper"), new ScreenSnapper());
  13.     return app->exec();
  14. }

June 18, 2012

DigitalPioneer DigitalPione..
Lab Rat
27 posts

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:

  1. #include <QtGui/QApplication>
  2. #include "qmlapplicationviewer.h"
  3. #include "screensnapper.h"
  4. Q_DECL_EXPORT int main(int argc, char *argv[])
  5. {
  6.     QScopedPointer<QApplication> app(createApplication(argc, argv));
  7.  
  8.     QmlApplicationViewer viewer;
  9.     viewer.engine()->addImageProvider(QString("snapper"), new ScreenSnapper());
  10.     viewer.setMainQmlFile(QLatin1String("qml/untitled18/main.qml"));
  11.     viewer.showExpanded();
  12.  
  13.     return app->exec();
  14. }

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.

June 18, 2012

daljit97 daljit97
Ant Farmer
68 posts

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’

June 18, 2012

DigitalPioneer DigitalPione..
Lab Rat
27 posts

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

  1. #include <QDeclarativeEngine>

June 18, 2012

daljit97 daljit97
Ant Farmer
68 posts

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.

June 18, 2012

daljit97 daljit97
Ant Farmer
68 posts

Any way to save it to the phone memory?

Page  
1

  ‹‹ ModelContact removeContact      Passing values from C++ to QML... ››

You must log in to post a reply. Not a member yet? Register here!