Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    [Solved] How to take a Print in QML?

    QML and Qt Quick
    3
    15
    16367
    Loading More Posts
    • 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.
    • p3c0
      p3c0 Moderators @IT MAN 2015 last edited by

      @IT-MAN-2015 Not need of QVariant pointer. It is implicily shared. Afterwards use qvariant_cast to cast it to QImage and then as usual of printing. So

      void pr::print(QVariant data) { //note: it sends a complete image and not just url
           QImage img = qvariant_cast<QImage>(data);
      }
      

      157

      I 1 Reply Last reply Reply Quote 1
      • I
        IT MAN 2015 @p3c0 last edited by p3c0

        @p3c0 said:

        @IT-MAN-2015 Not need of QVariant pointer. It is implicily shared. Afterwards use qvariant_cast to cast it to QImage and then as usual of printing. So

        void pr::print(QVariant data) { //note: it sends a complete image and not just url
             QImage img = qvariant_cast<QImage>(data);
        }
        

        Well... I understand about QVariant but now i get error !

        error: C2664: 'void pr::print(QVariant)' : cannot convert argument 1 from 'QVariant' to 'QVariant'
        Source or target has incomplete type

        .h file :

        #ifndef PR_H
        #define PR_H
        #include <QObject>
        class pr : public QObject
        {
            Q_OBJECT
        public:
            pr();
        public:
        Q_INVOKABLE void print(QVariant data);
        };
        #endif // PR_H
        

        and .cpp file :

        pr::pr()
        {
        }
        void pr::print(QVariant data)
        {
           QImage img = qvariant_cast<QImage>(data);
           QPrinter printer;
                  QPrintDialog *dlg = new QPrintDialog(&printer,0);
                  if(dlg->exec() == QDialog::Accepted) {
                          QPainter painter(&printer);
                          painter.drawImage(QPoint(0,0),img);
                          painter.end();
                  }
        }
        
        p3c0 1 Reply Last reply Reply Quote 0
        • T
          timday last edited by

          It's worth mentioning this is one of the few things the old Qt4.8-era QDeclarativeView could do better, due to being QPainter based instead of OpenGL; see http://stackoverflow.com/questions/20825233/how-to-print-a-qquickviews-contents-to-pdf . (Well in theory; in practice the PDFs seemed to have some flaws.)

          The possibility of using the new (commerical license) QtQuick software rendering tech for improving print capabilities gets a brief mention in the thread under https://blog.qt.io/blog/2015/01/22/introducing-the-qt-quick-2d-renderer/ too.

          p3c0 1 Reply Last reply Reply Quote 0
          • p3c0
            p3c0 Moderators @IT MAN 2015 last edited by

            @IT-MAN-2015 do #include <QVariant>

            157

            I 1 Reply Last reply Reply Quote 1
            • I
              IT MAN 2015 @p3c0 last edited by

              @p3c0

              Thank you p30c0 :-) I understand and I can take a print in QML right now.
              Thank you for your helping my friend.

              p3c0 1 Reply Last reply Reply Quote 0
              • p3c0
                p3c0 Moderators @IT MAN 2015 last edited by

                @IT-MAN-2015 You're Welcome :) Also please surround you code with ``` (3 backticks) while posting here so that it gets formatted nicely and is more readable.

                157

                I 1 Reply Last reply Reply Quote 2
                • p3c0
                  p3c0 Moderators @timday last edited by

                  @timday

                  It's worth mentioning this is one of the few things the old Qt4.8-era QDeclarativeView could do better, due to being QPainter based instead of OpenGL; see http://stackoverflow.com/questions/20825233/how-to-print-a-qquickviews-contents-to-pdf . (Well in theory; in practice the PDFs seemed to have some flaws.)

                  Yes indeed. Miss them for these features including WebView. I guess in future we may have a direct print function in QtQuick 2.x too.

                  157

                  1 Reply Last reply Reply Quote 1
                  • I
                    IT MAN 2015 @p3c0 last edited by IT MAN 2015

                    @p3c0 said:

                    @IT-MAN-2015 You're Welcome :) Also please surround you code with ``` (3 backticks) while posting here so that it gets formatted nicely and is more readable.

                    Here you are :

                    .h file

                    #ifndef PR_H
                    #define PR_H
                    
                    #include <QObject>
                    #include <QVariant>
                    
                    class pr : public QObject
                    {
                        Q_OBJECT
                    
                    public:
                        pr();
                    
                    public:
                    
                    Q_INVOKABLE void print(QVariant data);
                    
                    
                    };
                    
                    #endif // PR_H
                    

                    .cpp file :

                    #include "pr.h"
                    #include <QPrinter>
                    #include <QPainter>
                    #include <QPrintDialog>
                    #include <QPixmap>
                    #include <QImage>
                    #include <qDebug>
                    
                    pr::pr()
                    {
                    
                    }
                    
                    void pr::print(QVariant data)
                    
                    {
                    
                    
                        QImage img = qvariant_cast<QImage>(data);
                        QPrinter printer;
                              QPrintDialog *dlg = new QPrintDialog(&printer,0);
                              if(dlg->exec() == QDialog::Accepted) {
                                      QPainter painter(&printer);
                                      painter.drawImage(QPoint(0,0),img);
                                      painter.end();
                              }
                    
                    
                    }
                    
                    

                    QML file :

                                WebView {
                                    id: result_view
                                    x: 154
                                    y: 22
                                    url:"http://forum.qt.io/logo.png"
                                    enabled: false
                                    antialiasing: true
                    
                                }
                    
                    
                            Button {
                                id: button1
                                x: 437
                                y: 137
                                width: 150
                                height: 36
                                text: qsTr("Print")
                                onClicked: {
                                        var stat = result_view.grabToImage(function(result) {
                                            //result.saveToFile("/home/user/someimage.png"); //saves to a file
                                            PRINT.print(result.image); //result.image holds the QVariant
                                        });
                                        console.log("Success: ", stat);
                                    }
                            }
                    
                    

                    main.cpp

                    #include <QApplication>
                    #include <QQmlApplicationEngine>
                    #include <QtWebEngine/QtWebEngine>
                    #include <pr.h>
                    
                    int main(int argc, char *argv[])
                    {
                        QApplication app(argc, argv);
                    
                        QQmlApplicationEngine engine;
                        QtWebEngine::initialize();
                    
                        //For QML
                        pr  print;
                        engine.rootContext()->setContextProperty("PRINT", &print);
                        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                    
                        return app.exec();
                    }
                    
                    p3c0 1 Reply Last reply Reply Quote 4
                    • p3c0
                      p3c0 Moderators @IT MAN 2015 last edited by

                      @IT-MAN-2015 Thanks for sharing :)

                      157

                      I 1 Reply Last reply Reply Quote 1
                      • I
                        IT MAN 2015 @p3c0 last edited by

                        @p3c0 said:

                        @IT-MAN-2015 Thanks for sharing :)

                        You're welcome :)
                        Have a nice time.

                        1 Reply Last reply Reply Quote 1
                        • First post
                          Last post