PDA

View Full Version : draw Image to QPrinter in QThread



qt-nubie
27th March 2012, 09:43
with the code below, i cannot print my image (not shown up). how to solve this? thank in advance.


#include <QPrintDialog>
#include <QPrinter>
#include <QPainter>
#include <QThread>
#include <QDebug>

#include "documentprinter.h"
#include "document.h"

DocumentPrinter::DocumentPrinter(Document *document, QObject *parent) :
QObject(parent),
m_printer(0),
m_document(document)
{
}

void DocumentPrinter::setDocument(Document *document)
{
m_document = document;
}

bool DocumentPrinter::exec()
{
QThread printerThread;
DocumentPrinterWorker docPrinterWorker;
docPrinterWorker.moveToThread(&printerThread);
connect(this, SIGNAL(printStarted(QPrinter*,Document*,int,int)),
&docPrinterWorker, SLOT(printDocument(QPrinter*,Document*,int,int)));

QPrintDialog dialog((QWidget *)parent());
dialog.setMinMax(1, m_document->numPages());

if (dialog.exec()) {
m_printer = dialog.printer();
printerThread.start();
if (dialog.printRange() == QAbstractPrintDialog::AllPages)
emit printStarted(m_printer, m_document, 1, m_document->numPages());
else if (dialog.printRange() == QAbstractPrintDialog::PageRange)
emit printStarted(m_printer, m_document, dialog.fromPage(), dialog.toPage());
printerThread.quit();
printerThread.wait();
}

return true;
}

void DocumentPrinterWorker::printDocument(QPrinter *printer, Document *document, int fromPage, int toPage)
{
if (!printer && !document)
return;

printer->setResolution(300);
printer->setPageMargins(0,0,0,0,QPrinter::Point);

QPainter painter(printer);
for (int i=fromPage; i<=toPage; i++) {
QImage image("./images/logo.png");
painter.drawImage(0,0, image);
document->render(&painter, i, QRectF(0, 0, printer->width(), printer->height()));
if (i < toPage)
printer->newPage();
}
painter.end();
}

sorry, i'm not good at english.

wysota
27th March 2012, 10:18
Why are you trying to print from within a thread?

qt-nubie
27th March 2012, 10:39
when i print a lot of page, does it freeze the gui?

wysota
27th March 2012, 11:14
No, why would it do that?

qt-nubie
27th March 2012, 11:30
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. :D

wysota
27th March 2012, 11:40
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: Keeping the GUI Responsive

qt-nubie
27th March 2012, 11:56
Thank you for the advise... :D

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.

wysota
27th March 2012, 13:48
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.

qt-nubie
28th March 2012, 09:22
ok, got it....