(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
- #include <QtGui/QApplication>
- #include "qmlapplicationviewer.h"
- #include "QPixmap"
- #include "QDeclarativeView"
- #include "QDeclarativeContext"
- {
- Q_OBJECT
- public:
- Q_INVOKABLE void shoot() {
- QPixmap screenShot;
- [view]->render(&p);
- }
- };
- int main(int argc, char *argv[]) {
- QDeclarativeView view;
- Snap data;
- view.rootContext()->setContextProperty("snap", &data);
- view.show();
- return app.exec();
- }
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()
- }
- }
16 replies
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 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();
- };
- #endif // SCREENSNAPPER_H
screensnapper.cpp
- #include "screensnapper.h"
- #include "QApplication"
- #include "QDesktopWidget"
- ScreenSnapper::ScreenSnapper()
- : QDeclarativeImageProvider(QDeclarativeImageProvider::Pixmap)
- {
- }
- {
- }
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:
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[])
- {
- QmlApplicationViewer viewer;
- viewer.showExpanded();
- QDeclarativeView view;
- return app->exec();
- }
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[])
- {
- QmlApplicationViewer viewer;
- viewer.showExpanded();
- return app->exec();
- }
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.
You must log in to post a reply. Not a member yet? Register here!


