Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. (solved) Screenshot in qml
Forum Updated to NodeBB v4.3 + New Features

(solved) Screenshot in qml

Scheduled Pinned Locked Moved QML and Qt Quick
18 Posts 5 Posters 10.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    daljit97
    wrote on last edited by
    #5

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

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Peppy
      wrote on last edited by
      #6

      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.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        daljit97
        wrote on last edited by
        #7

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

        1 Reply Last reply
        0
        • D Offline
          D Offline
          daljit97
          wrote on last edited by
          #8

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

          1 Reply Last reply
          0
          • P Offline
            P Offline
            Peppy
            wrote on last edited by
            #9

            how does it look like after you have rewritten it?

            1 Reply Last reply
            0
            • D Offline
              D Offline
              DigitalPioneer
              wrote on last edited by
              #10

              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());@

              1 Reply Last reply
              0
              • D Offline
                D Offline
                daljit97
                wrote on last edited by
                #11

                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;;
                

                }@

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  DigitalPioneer
                  wrote on last edited by
                  #12

                  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.

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    daljit97
                    wrote on last edited by
                    #13

                    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'

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      DigitalPioneer
                      wrote on last edited by
                      #14

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

                      @#include <QDeclarativeEngine>@

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        daljit97
                        wrote on last edited by
                        #15

                        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.

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          daljit97
                          wrote on last edited by
                          #16

                          Any way to save it to the phone memory?

                          1 Reply Last reply
                          0
                          • D Offline
                            D Offline
                            DigitalPioneer
                            wrote on last edited by
                            #17

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

                            1 Reply Last reply
                            0
                            • D Offline
                              D Offline
                              Djay96
                              wrote on last edited by
                              #18

                              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
                              }

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved