Can work QPrinter in a thread?
-
wrote on 6 Apr 2011, 23:22 last edited by
Hello guys,
I need to generate pdf files but i need to generate it in background because it may freeze the application. I have used the following functions in a thread:
@
editor = new QTextDocument();
printer = new QPrinter(QPrinter::ScreenResolution);
printer->setOutputFormat(QPrinter::PdfFormat);editor->clear(); editor->setHtml(dataHtml); QDir path("./Reports/"); printer->setOutputFileName(path.absolutePath() + QString("/")+fileName); editor->print(printer);
@
All the objects were created in the thread instance. When i try to execute the code, i have always the following error: QPainter::begin(): Returned false. So, i was wondering if QPrinter can't work in a thread, if it is so, does someone have any suggestions to achieve the same result?
Thanks in advance...
-
wrote on 6 Apr 2011, 23:47 last edited by
As far as i can see, if all those operations were made in the run() function it works, but if it is made in another function doesnt works...
I mean..This kind of code works:
@
void run()
{
//Create the objects
//Generates the PDF file...
}
@But this doesnt:
@
void run()
{
//create the objects
generatePDFs();
}void generatePDfs() // <--- this function is part of the class..
{
//generates the pdf file <-- here i get the error of the QPainter...
}
@I can't see where is the problem, maybe i am using wrong the QThread class...
-
wrote on 7 Apr 2011, 04:32 last edited by
That sounds strange. So it's not a problem of doing it inside a thread, it#s a problem of function separation?
Then please give aus more code. a small, (not) working example with all needed code. What exactly is done in run, what in generatePdfs?
-
wrote on 7 Apr 2011, 09:11 last edited by
This could be an instance of the "GUI only in the main thread" problem (painting on a QPrinter is GUI, as far as I know).
Is there any output on the console? As far as I know, QPainter prints some debugging messages when it fails.
-
wrote on 7 Apr 2011, 09:40 last edited by
What iritated me is, he said uif everything is in run, it works....
run is the thread function... -
wrote on 7 Apr 2011, 10:55 last edited by
[quote author="Gerolf" date="1302169254"]What iritated me is, he said uif everything is in run, it works....
run is the thread function...[/quote]An instance of the "run in the context where the method is called" problem?
-
wrote on 7 Apr 2011, 11:50 last edited by
That's why I asked for more code...
-
wrote on 7 Apr 2011, 12:48 last edited by
[quote author="gronerth" date="1302133641"]As far as i can see, if all those operations were made in the run() function it works, but if it is made in another function doesnt works...[/quote]
do you use the constructor to initialize objects for this functions (parent thread for this objects will be thread where class was created)? -
wrote on 7 Apr 2011, 14:13 last edited by
To give an small example, this kind of code doesn't work:
@
class reportGeneratorThread : public QThread
{
Q_OBJECT
public:
void run();
private:
QPrinter *printer;
QTextDocument *editor;
public slots:
void makeReports();
}reportGeneratorThread::reportGeneratorThread(QObject *parent) :
QThread(parent)
{}
void reportGeneratorThread::run()
{
editor = new QTextDocument();
editor->setHtml("<h1>hola</h1>");
printer = new QPrinter(QPrinter::HighResolution);
printer->setOutputFormat(QPrinter::PdfFormat);
printer->setOutputFileName("report.pdf");
makeReports();
}void reportGeneratorThread::makeReports()
{
editor->print(printer);
}
@[quote author="Volker" date="1302167511"]This could be an instance of the "GUI only in the main thread" problem (painting on a QPrinter is GUI, as far as I know).
Is there any output on the console? As far as I know, QPainter prints some debugging messages when it fails.[/quote]
The debug message is: QPainter::begin(): Returned false
but if i put the code editor->print(printer) inside the run function it works....
-
wrote on 7 Apr 2011, 18:02 last edited by
I believe that Volker is correct, aa QPrinter inherits from QPaintDevice, though I have not found anything that explicitly says it is always "thunked" back into the GUI thread. This may also be a stupid question, but when you call the thread you call it by using "start()" and not "run()" correct? As this will also cause strange behavior.
-
wrote on 7 Apr 2011, 18:12 last edited by
[quote author="webmaster.skelton" date="1302199366"]I believe that Volker is correct, aa QPrinter inherits from QPaintDevice, though I have not found anything that explicitly says it is always "thunked" back into the GUI thread. This may also be a stupid question, but when you call the thread you call it by using "start()" and not "run()" correct? As this will also cause strange behavior. [/quote]
yes, i call it with "start()"...
8/11