Results 1 to 2 of 2

Thread: How to take a Print in QML?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default How to take a Print in QML?

    Hi
    I want to take a print a page in QML. I checked this link : https://forum.qt.io/topic/57861/solv...a-print-in-qml , but I have some errors.


    Error


    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. QQmlApplicationEngine engine;
    6. QtWebEngine::initialize();
    7. //For QML
    8. pr print;
    9. engine.rootContext()->setContextProperty("PRINT", &print);
    10.  
    11.  
    12. engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    To copy to clipboard, switch view to plain text mode 

    pr.h:
    Qt Code:
    1. #ifndef PR_H
    2. #define PR_H
    3.  
    4. #include <QObject>
    5. #include <QVariant>
    6.  
    7. class pr : public QObject
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. pr();
    13.  
    14. public:
    15.  
    16. Q_INVOKABLE void print(QVariant data);
    17.  
    18.  
    19. };
    20.  
    21. #endif // PR_H
    To copy to clipboard, switch view to plain text mode 

    pr.cpp:
    Qt Code:
    1. #include "pr.h"
    2. #include <QtPrintSupport/QPrinter>
    3. #include <QPainter>
    4. #include <QtPrintSupport/QPrintDialog>
    5. #include <QPixmap>
    6. #include <QImage>
    7. #include <qDebug>
    8.  
    9. pr::pr()
    10. {
    11.  
    12. }
    13.  
    14. void pr::print(QVariant data)
    15.  
    16. {
    17.  
    18.  
    19. QImage img = qvariant_cast<QImage>(data);
    20. QPrinter printer;
    21. QPrintDialog *dlg = new QPrintDialog(&printer,0);
    22. if(dlg->exec() == QDialog::Accepted) {
    23. QPainter painter(&printer);
    24. painter.drawImage(QPoint(0,0),img);
    25. painter.end();
    26. }
    27.  
    28.  
    29. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to take a Print in QML?

    Have you added the printsupport module to your .pro file?

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    neda (28th April 2016)

Similar Threads

  1. how to print a value in qt
    By qt_user in forum Qt Programming
    Replies: 5
    Last Post: 6th August 2010, 21:17
  2. print
    By d4rc in forum Newbie
    Replies: 5
    Last Post: 29th June 2010, 10:28
  3. qwt print
    By manmohan in forum Qwt
    Replies: 0
    Last Post: 5th May 2010, 07:16
  4. print value
    By valy12 in forum Qt Programming
    Replies: 3
    Last Post: 21st April 2010, 16:32
  5. Cancelling Long-running Print/Print Preview
    By ChrisW67 in forum Newbie
    Replies: 4
    Last Post: 16th June 2009, 23:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.