(solved) Screenshot in qml
-
[quote author="daljit97" date="1339960006"]but i'm getting many errors.[/quote]
Any news on what the errors actually are?
-
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)?
-
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();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(QLatin1String("qml/untitled18/main.qml")); viewer.showExpanded(); QDeclarativeView view; view->engine()->addImageProvider(QString("snapper"), new ScreenSnapper()); 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[])
{
QScopedPointer<QApplication> app(createApplication(argc, argv));QmlApplicationViewer viewer; viewer.engine()->addImageProvider(QString("snapper"), new ScreenSnapper()); viewer.setMainQmlFile(QLatin1String("qml/untitled18/main.qml")); 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.
-
Now it's angry because the compiler doesn't actually know what QDeclarativeEngine is -- you need to include the header for it:
@#include <QDeclarativeEngine>@
-
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
}