Results 1 to 9 of 9

Thread: draw Image to QPrinter in QThread

  1. #1
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default draw Image to QPrinter in QThread

    with the code below, i cannot print my image (not shown up). how to solve this? thank in advance.

    Qt Code:
    1. #include <QPrintDialog>
    2. #include <QPrinter>
    3. #include <QPainter>
    4. #include <QThread>
    5. #include <QDebug>
    6.  
    7. #include "documentprinter.h"
    8. #include "document.h"
    9.  
    10. DocumentPrinter::DocumentPrinter(Document *document, QObject *parent) :
    11. QObject(parent),
    12. m_printer(0),
    13. m_document(document)
    14. {
    15. }
    16.  
    17. void DocumentPrinter::setDocument(Document *document)
    18. {
    19. m_document = document;
    20. }
    21.  
    22. bool DocumentPrinter::exec()
    23. {
    24. QThread printerThread;
    25. DocumentPrinterWorker docPrinterWorker;
    26. docPrinterWorker.moveToThread(&printerThread);
    27. connect(this, SIGNAL(printStarted(QPrinter*,Document*,int,int)),
    28. &docPrinterWorker, SLOT(printDocument(QPrinter*,Document*,int,int)));
    29.  
    30. QPrintDialog dialog((QWidget *)parent());
    31. dialog.setMinMax(1, m_document->numPages());
    32.  
    33. if (dialog.exec()) {
    34. m_printer = dialog.printer();
    35. printerThread.start();
    36. if (dialog.printRange() == QAbstractPrintDialog::AllPages)
    37. emit printStarted(m_printer, m_document, 1, m_document->numPages());
    38. else if (dialog.printRange() == QAbstractPrintDialog::PageRange)
    39. emit printStarted(m_printer, m_document, dialog.fromPage(), dialog.toPage());
    40. printerThread.quit();
    41. printerThread.wait();
    42. }
    43.  
    44. return true;
    45. }
    46.  
    47. void DocumentPrinterWorker::printDocument(QPrinter *printer, Document *document, int fromPage, int toPage)
    48. {
    49. if (!printer && !document)
    50. return;
    51.  
    52. printer->setResolution(300);
    53. printer->setPageMargins(0,0,0,0,QPrinter::Point);
    54.  
    55. QPainter painter(printer);
    56. for (int i=fromPage; i<=toPage; i++) {
    57. QImage image("./images/logo.png");
    58. painter.drawImage(0,0, image);
    59. document->render(&painter, i, QRectF(0, 0, printer->width(), printer->height()));
    60. if (i < toPage)
    61. printer->newPage();
    62. }
    63. painter.end();
    64. }
    To copy to clipboard, switch view to plain text mode 

    sorry, i'm not good at english.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: draw Image to QPrinter in QThread

    Why are you trying to print from within a thread?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: draw Image to QPrinter in QThread

    when i print a lot of page, does it freeze the gui?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: draw Image to QPrinter in QThread

    No, why would it do that?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: draw Image to QPrinter in QThread

    render function i implement also load pdf using muPDF and render it to QPainter pointer that pass through it. is it blocking the main thread? if no, then i miss understood something.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: draw Image to QPrinter in QThread

    Yes, it's blocking the main thread but nobody said you have to do that in one big loop working for 10 minutes.

    Read this: [wiki]Keeping the GUI Responsive[/wiki]
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    qt-nubie (28th March 2012)

  8. #7
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: draw Image to QPrinter in QThread

    Thank you for the advise...

    btw about that QImage, it's not possible to to use drawImage not in main thread? there is a warning say about "QPixmap: It is not safe to use pixmaps outside the GUI thread", but i'm using QImage not QPixmap.

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: draw Image to QPrinter in QThread

    It is possible to draw on images in non-gui thread. But pixmaps are a completely different issue. It's possible the library you are using is drawing on pixmaps. Also accessing the same object (QPrinter instance) from more than one thread is a straight way to trouble. Using threads here is out of the question, at least for printing itself. You can try rendering the document in an external thread (e.g. using QtConcurrent) and once you have that, send everything to the printer.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #9
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: draw Image to QPrinter in QThread

    ok, got it....

Similar Threads

  1. Draw on QPrinter: color gets lost
    By FelixB in forum Newbie
    Replies: 1
    Last Post: 3rd February 2011, 09:26
  2. Draw Image into Pixmap/Image
    By ToddAtWSU in forum Qt Programming
    Replies: 1
    Last Post: 19th May 2010, 06:56
  3. Replies: 6
    Last Post: 21st September 2009, 10:55
  4. How to draw a diagram in a QPrinter?
    By nifei in forum Qt Programming
    Replies: 5
    Last Post: 24th February 2009, 03:41
  5. add(draw) an icon(or image) to a QPixmap?
    By ascii in forum Qt Programming
    Replies: 4
    Last Post: 20th November 2008, 12:44

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.