QPrinter & QPainter render() problem
I have a problem when calling Qwidget::render() on a specific printer connected via a network to a PC running XP. I am quite new to using the Qprinter class so I apologise if I am missing something obvious. I get a repeatable crash when rendering to the printer in question. The only diffreence I can see between the working printers and the crashing one is the printer resolution. I seem to get the problem no matter what I render. The crash always seesm to occur at the render() call.
Here is attached example code:
Code:
void MainWindow::menuPrintTest(){
double xscale; double yscale; double scale;
double printerWidth ; double printerHeight ;
double widgetWidth; double widgetHeight ;
QFont printFont
("courier",
16);
printFont.setFixedPitch(TRUE);
qteTestPrintRequest ->setFont(printFont );
qteTestPrintRequest ->setFixedHeight(1750);
qteTestPrintRequest ->setFixedWidth(1050);
widgetWidth = qteTestPrintRequest ->width();
widgetHeight = qteTestPrintRequest ->height();
QString qsConcat
= "Test Printout For Checking XP Print Request Bug\n";
for (int i=0; i<30; i++){ qsConcat+= "Test Printout for Checking XP Print Request Bug\n" ; }
qteTestPrintRequest->append( qsConcat );
if ( !docName.isEmpty()) {
printer.setDocName (docName);
}
if ( dialog.exec() ) {
{
{
printerWidth = printer.pageRect().width();
printerHeight = printer.pageRect().height();
xscale = printerWidth/(widgetWidth);
yscale = printerHeight/(widgetHeight);
scale = qMin(xscale, yscale);
painter.scale(scale, scale);
qteTestPrintRequest ->render(&painter);
}
}
}
}
The qDebug() output for the scaling is as follows:
printerWidth = 4908.0
printerHeight = 6408.0
widgetWidth = 1050.0
widgetHeight = 1750.0
xscale = 4.674
yscale = 3.662
scale = 3.662
As a matter of interest the output on one of the working printers is
printerWidth = 4760.0
printerHeight = 6814.0
widgetWidth = 1050.0
widgetHeight = 1750.0
xscale = 4.533
yscale = 3.894
scale = 3.894
Re: QPrinter & QPainter render() problem
Try printing something small - does it still crash then?
Also, you have a memory leak - you never delete 'qteTestPrintRequest' (but that should not play a role in the problem you have).
Re: QPrinter & QPainter render() problem
Thankyou
I will try a print that is well within the pageRect() dimensions.
Re: QPrinter & QPainter render() problem
I don't want to duplicate effort. My great thanks to all who have helped so far.
I hope that this is helpful to all concerned but this thread is being discussed in more detail on this link.
http://www.qtforum.org/article/37518...tml#post117656
Re: QPrinter & QPainter render() problem
"I do need to rsolve this ASAP and would be willing to pay for a solution/resolution.":http://www.qtforum.org/article/37518...tml#post117656
The code above seems to work perfectly with all other configurations of Printer & PC tested. The problem seems to be that my simple test printout is generating a huge print document approximately a factor of 4000 times bigger (e.g. 213MB for a single A4 sheet) when sent to the specific printer involved:
Xerox Work Centre 7428 PS
The only potential leads I have are that it may be due to
1. A specific printer driver issue
2. QPrinter printer(QPrinter::HighResolution);
A guess.
3. qteTestPrintRequest->render(&painter);
Using render with a QTextEdit widget instead of using qteTestPrintRequest ->print(&printer);
4. Some other unidentified issue with the code posted above?
Re: QPrinter & QPainter render() problem
Maybe you should bother to answer high_flyer's question from post #2?
Re: QPrinter & QPainter render() problem
Well I did on the link. I had assumed that those interested would follow it. I am genuinely sorry if that has been misleading or unhelpful.
The scaling factor seemed to have no effect on the outcome even when I sent an image significantly within the pageRect bounds.
The problem is not a crash but just that a huge file is generated on a particular printer (as stated in my last post). I have to the best of my knowledge identified the current status of the problem in summarised form. If I can clarify anything I would be very happy to do so. As always I am extremely grateful for any insight gleaned from those kind enough to help.
The use of the link was merely a genuine, honest attempt to minimise the amount of work generated for those kind enough to help and to reduce any duplication. If you would prefer that I duplicate all that transpires on that link then I have no problem with that.
Please advise. I am quite new to this forum.
Thanks Again
M
I can see now in hindsight that my URGENT capitals may have come across in the wrong way. I was not intending to shout about anything other than my offer of payment. It was not in any way impatience. I can see that it seems out of context without the preceding posts on the linked to forum.
I apologise for the confusion and potential offence that my clumsy post may have caused (Embarrassed smiley!)
Added after 1 5 minutes:
Capitals now editted.
Re: QPrinter & QPainter render() problem
So does printing a smaller document and/or in a smaller resolution help or not? I read the thread behind the link twice and I didn't see the answer to this question.
Also what happens if you print to PDF instead of to the printer? How large is the file generated?
Re: QPrinter & QPainter render() problem
OK my apologies when re reading the thread it is less clear than I had thought but in the summary conclusions and in the context of my original, incorrectly posted crash query there was no crash caused by the resolution. If you are asking if the extreme print document size might be caused specifically by the dimensions of the rendered widget then that is another and interesting matter. In my last post I stated that the scaling factor seemed to have no effect on the huge print document phenomenon (that is other than a proportional change in its size).
I did not have the chance to test specifying a different a resolution setting in my rare opportunity on site as there were problems with drivers on site and security & IT etc.
Thankyou for your PDF output idea. I will test and respond. Currently the problem only happens specifically when outputting to the Xerox Work Centre 7428 PS printer.
Thanks again M
Re: QPrinter & QPainter render() problem
Quote:
Originally Posted by
Mannion
Thankyou for your PDF output idea. I will test and respond. Currently the problem only happens specifically when outputting to the Xerox Work Centre 7428 PS printer.
The point is to see how large the document is when still in postscript format. Apparently later the printer driver rasterizes the document which inflates its size. How much memory does the printer have?
Re: QPrinter & QPainter render() problem
Well even though it can print ok directly to my test printer when using:
the test application does seem to generate quite a large pdf weighing in at 4,164 KB for a single page single image renderred widget. This is, however, not in the same league as the Xerox prints which are over 50 times larger. I'm not sure how significant this is as it is my first attempt at any form of Qt PDF file generation.
I ran a test with
and it produced a file of 197KB.
Likewise with
the file size produced was 131KB
To my untrained eyes this seems quite a significant difference is that what is expected in terms of resolution quality?
I have attached the rapidly thrown together demo code and I apologise for any potentially obvious mistakes. I shall continue testing different parameters.
Code:
void MainWindow::menuPrintTest(){
double xscale; double yscale; double scale; double printerWidth ; double printerHeight ; double widgetWidth; double widgetHeight ;
QFont printFont
("courier",
16);
printFont.setFixedPitch(TRUE);
qteTestPrintRequest ->setFont(printFont );
qteTestPrintRequest ->setFixedHeight(500); //NEW
qteTestPrintRequest ->setFixedWidth(750); //NEW
widgetWidth = qteTestPrintRequest ->width() ; //+200; //NEW MOD
widgetHeight = qteTestPrintRequest ->height() ; //+300; //NEW MOD
QString qsConcat
= "Test Printout For Checking XP Print Request Bug\n";
for (int i=0; i<30; i++){ qsConcat+= "Test Printout for Checking XP Print Request Bug\n" ; }
qteTestPrintRequest->append( qsConcat );
printer.
setOutputFormat(QPrinter::PdfFormat);
//PDF ADD printer.setOutputFileName("Test.pdf"); //PDF ADD
if ( !docName.isEmpty()) {
printer.setDocName (docName);
}
//QPrintDialog dialog(&printer); //PDF REMOVE
//if ( dialog.exec() ) { //PDF REMOVE
{
painter.begin(&printer);
printerWidth = printer.pageRect().width();
printerHeight = printer.pageRect().height();
xscale = printerWidth/(widgetWidth);
yscale = printerHeight/(widgetHeight);
scale = qMin(xscale, yscale);
painter.scale(scale, scale);
qteTestPrintRequest ->render(&painter); //qteTestPrintRequest->print(&printer);
painter.end();
} //QPAINTER CTOR BRACE
}
//}else{} //PDF REMOVE
delete qteTestPrintRequest;
}
Added after 4 minutes:
Changing the size of the renderred QTextEdit widget seems to only change the file size proportionally.
http://doc.qt.nokia.com/4.7-snapshot...interMode-enum
States that for QPrinter::PrinterResolution
Quote:
This value is deprecated. Is is equivalent to ScreenResolution on Unix and HighResolution on Windows and Mac. Due do the difference between ScreenResolution and HighResolution, use of this value may lead to non-portable printer code.
and for QPrinter::HighResolution
Quote:
On Windows, sets the printer resolution to that defined for the printer in use. For PostScript printing, sets the resolution of the PostScript driver to 1200 dpi.
If it weren't for this I would be looking at substituting QPrinter::PrinterResolution for QPrinter::HighResolution.
Still my big question is if it is ok to use QTextEdit->render(&painter)
In response to your post:
Quote:
The point is to see how large the document is when still in postscript format. Apparently later the printer driver rasterizes the document which inflates its size. How much memory does the printer have?
The printer still manages to print the document it just takes a long time to send the data to the printer. In the test the 213MB was the size observed in the printer document queue on the PC running the test app.
It is a fairly modern, high spec printer serving a large-ish office. It must have a sizeable memory. All of the other printers tested were of a lower spec to the problem printer.
Re: QPrinter & QPainter render() problem
Quote:
Originally Posted by
Mannion
the test application does seem to generate quite a large pdf weighing in at 4,164 KB for a single page single image renderred widget.
Hmmm... are you printing a widget or a document? What does the document contain?
Quote:
To my untrained eyes this seems quite a significant difference is that what is expected in terms of resolution quality?
Yes, of course. Your screen has a resolution of around 100DPI, the printer's high resolution is probably around 1200DPI which means one page is 144 times larger.
Re: QPrinter & QPainter render() problem
Quote:
Hmmm... are you printing a widget or a document? What does the document contain?
A widget as per the code, more specifically:
Code:
QFont printFont
("courier",
16);
printFont.setFixedPitch(TRUE);
qteTestPrintRequest ->setFont(printFont );
qteTestPrintRequest ->setFixedHeight(500); //NEW
qteTestPrintRequest ->setFixedWidth(750); //NEW
widgetWidth = qteTestPrintRequest ->width() ; //+200; //NEW MOD
widgetHeight = qteTestPrintRequest ->height() ; //+300; //NEW MOD
QString qsConcat
= "Test Printout For Checking XP Print Request Bug\n";
for (int i=0; i<30; i++){ qsConcat+= "Test Printout for Checking XP Print Request Bug\n" ; }
qteTestPrintRequest->append( qsConcat );
Quote:
Yes, of course. Your screen has a resolution of around 100DPI, the printer's high resolution is probably around 1200DPI which means one page is 144 times larger.
Yes I agree that this point is redundant. However compression and the discrepancy between the Printer and High resolutions are not covered by this observation. Clearly something else is going on.
Added after 16 minutes:
After examining the PDF documents it seems as though the differences in PDF file size could be down to resolution and compression alone. The differences between HighResolution and PrinterResolution are a little less easy to explain. Perhaps they represent the difference between some lower printer resolution setting and the printer's high resolution setting. The resolution of the PrinterResolution PDF is quite low indeed.
The only clear link to the Xerox problem that I can identify is that the Xerox would perhaps have the potential for a very high resolution upper setting. This could perhaps partially explain the factor of 4000 in the queued printer document size.
Does this make any sense to you or am I barking up the wrong tree?
It seems like this issue would probably crop up an awful lot if this is all that is happening. Is the QTextEdit->render() call part of the issue?
Can I get high resolution scaled images without setting the highest available printer resolution?
Re: QPrinter & QPainter render() problem
Quote:
Originally Posted by
Mannion
A widget as per the code,
Then you are printing an image, a bitmap, which is large per definition. Not that I see any point in printing a widget and not a document contained in it.
Quote:
Yes I agree that this point is redundant. However compression and the discrepancy between the Printer and High resolutions are not covered by this observation. Clearly something else is going on.
I don't see anything "clear" here. You get a 200 MB printer job because you rasterize an image in high resolution. This just has to take time and memory.
Quote:
Can I get high resolution scaled images without setting the highest available printer resolution?
If your image has 100 DPI then scaling it up to 1200 DPI doesn't make sense and you can print it in 100 DPI, the quality will be the same.
But first think if you should really be printing the widget and not the document it contains (which would have much higher quality and much lower volume).
Re: QPrinter & QPainter render() problem
There is no document to print unless you mean the .text property. All the text is added via append
The widgets are renderred off screen. I got very poor results using the screen resolution setting
Added after 6 minutes:
The exact same renderring request ( in terms of code )produced a local queued printer document over 4000 times larger with the problem printer than with several printers of at least 600dpi
Re: QPrinter & QPainter render() problem
Quote:
Originally Posted by
Mannion
There is no document to print unless you mean the .text property.
So... are you printing an empty QTextEdit widget? Are you aware of the distinction between QTextEdit and QTextDocument?
Re: QPrinter & QPainter render() problem
There is no subterfuge here. I am doing exactly what is in the code at the very beginning of the thread. Nothing has changed. I'm not sure where the questions regarding what I am printing are coming from. If the code is not clear please let me know and I will respond immediately. Once again I am genuinely sorry for anything miscommunicated and I do appreciate your efforts to help.
Added after 32 minutes:
For clarity this is the constructor that I used
Here is the test example text
Code:
QString qsConcat
= "Test Printout For Checking XP Print Request Bug\n";
for (int i=0; i<30; i++){ qsConcat+= "Test Printout for Checking XP Print Request Bug\n" ; }
qteTestPrintRequest->append( qsConcat );
Here is the key focal point of the thread which involves printing a widget (There was certainly no intent to mention printing a document. I am unsure as to how I gave that impression and I apologise for the confusion)
Code:
qteTestPrintRequest ->render(&painter);
I am deliberately trying to render the widget in this way and I am aware that there may be an issue with this line and I have pointed this out in several places above. This approach, which indeed involves rendering a bitmap, worked using several PC/Printer configurations. So far in testing this approach has only caused a problem with 1 particular printer where the application produces a printer document (terminology referring to printer queue) over 4000 times larger. In the other tests for example my test printer configuration produced an equivalent document comprised of 3 full pages rendered using this widget rendering approach with a size of 158KB.
If this approach is definitely a dead end for any of the reasons I have stated previously or for any other reason then I do need to consider other options although this would obviously incur an unwanted time penalty.
I have already asked the question
Quote:
Will I have to use QTextDocument & html or QGraphicsScene to achieve this end result?
Added after 1 5 minutes:
Your information has provided me with many interesting avenues to pursue and I will endeavour to pursue any leads that you produce. For example you have raised the resolution issue. Is the resolution being rendered at the printer related to the pageRect dimensions? There certainly seems to be some correlation.
If so then the follwing is interesting. The three different resolution settings on my test printer and the HighResolution setting on the problem printer produce the following results:
Test Printer (No Problem)
===================
Using: QPrinter printer(QPrinter::PrinterResolution);
xscale,yscale,scale = 6.347, 13.628, 6.347
printer.pageRect().width() = 4760.0
printer.pageRect().height() = 6814.0
QTextEdit.width() = 750.0
QTextEdit.height() = 500.0
Printer queue document size 297 KB
Using: QPrinter printer(QPrinter::ScreenResolution);
xscale,yscale,scale = 1.016, 2.180, 1.016
printer.pageRect().width() = 762.0
printer.pageRect().height() = 1090.0
QTextEdit.width() = 750.0
QTextEdit.height() = 500.0
Printer queue document size 263 KB
Using: QPrinter printer(QPrinter::HighResolution);
xscale,yscale,scale = 6.347, 13.628, 6.347
printer.pageRect().width() = 4760.0
printer.pageRect().height() = 6814.0
QTextEdit.width() = 750.0
QTextEdit.height() = 500.0
Printer queue document size 297 KB
Problem Printer (Xerox Work Centre 7428 PS
=================================
Using: QPrinter printer(QPrinter::HighResolution);
xscale,yscale,scale = 6.544, 12.816, 6.544
printer.pageRect().width() = 4908.0
printer.pageRect().height() = 6408.0
QTextEdit.width() = 750.0
QTextEdit.height() = 500.0
Printer queue document size 213 MB
The problem printer queue document size is produced at a very similar scale factor to that of the more reasonable sized HighResolution equivalent on the test printer and yet is over 717 times larger. The ratio of the squares of the scale factors is approximately 1.06:1
Re: QPrinter & QPainter render() problem
Quote:
Originally Posted by
Mannion
There is no subterfuge here. I am doing exactly what is in the code at the very beginning of the thread. Nothing has changed. I'm not sure where the questions regarding what I am printing are coming from.
They are coming from my opinion that you are doing something you are not supposed to be doing and I'm wondering if it is on purpose or if you just didn't find a better way of obtaining your goals.
Quote:
If this approach is definitely a dead end for any of the reasons I have stated previously or for any other reason then I do need to consider other options although this would obviously incur an unwanted time penalty.
It's a matter of using the right resolution for printing. If you want to print a widget then its resolution is equal to the resolution of your screen and upscaling it to the maximum resolution supported by a printer doesn't make sense. If you keep the printout resolution in the range of your screen resolution, you should obtain reasonable print jobs.
Re: QPrinter & QPainter render() problem
Quote:
It's a matter of using the right resolution for printing. If you want to print a widget then its resolution is equal to the resolution of your screen and upscaling it to the maximum resolution supported by a printer doesn't make sense. If you keep the printout resolution in the range of your screen resolution, you should obtain reasonable print jobs.
This is absolutely not true
I have just run of a quick confirmation test using the ScreenResolution setting (from a high resolution adapter) The results were dreadful as I have already clearly stated. I am not sure why you find this impossible to believe.
If a widget is used as a means to rendering a high quality output such as multiple detailed graphs for example as one of my actual application requirements. You often want to render that widget at a much higher resolution than that of the screen. You render the widget off screen and indeed the screen resolution is largely irrelevant. It is then advisable to render at the HighResolution setting. This is exactly what the documentation recommends for high quality printouts.
Quote:
They are coming from my opinion that you are doing something you are not supposed to be doing and I'm wondering if it is on purpose or if you just didn't find a better way of obtaining your goals.
There is still no need for questions as all of the answers are in the source code. It is crystal clear what I am doing. I am either doing it wrong or not there is no lack of clarity as to what I am doing. Indeed I am actually asking if what I am doing is correct, that is the whole point of the thread. I'm not sure how our wires have become so crossed? I am sorry if it is the way that I have framed the question.
There may be a better way of achieving my goals in respect to some aspects of the problem ie in terms of complete flexibility and best practice etc. However the solution I have tried is a much quicker solution and ticks all the right boxes in terms of quality and indeed queued printer document size apart from a curious specific printer issue which you have never directly addressed. This is why it was a very deliberate approach with an awareness, that I clearly stated, that there are other potential approaches.
If indeed the only response is never render a widget directly then there should not be so much information readily available showing how to do it. Also this response could have been stated after my first post after a cursory look at my source code. From the information supplied this is not however clear. Why the difference between the test printers and the problem printer? That is surely a useful question. Why do the file sizes not match your explanation of it all being down to resolution dimensions. Look at the figures it simply is not.
Re: QPrinter & QPainter render() problem
Quote:
Originally Posted by
Mannion
This is absolutely not true
What exactly is not true? The Nyquist-Shannon sampling theorem?
Quote:
I have just run of a quick confirmation test using the ScreenResolution setting (from a high resolution adapter) The results were dreadful as I have already clearly stated.
I have no idea what you consider "dreadful".
Quote:
I am not sure why you find this impossible to believe.
Because the laws of physics are identical to everyone.
Quote:
If a widget is used as a means to rendering a high quality output such as multiple detailed graphs for example
This is just silly. "Widgets" and "high quality" are nowhere near each other. If you want a graph then render a graph, not a widget.
Quote:
You render the widget off screen and indeed the screen resolution is largely irrelevant. It is then advisable to render at the HighResolution setting. This is exactly what the documentation recommends for high quality printouts.
You mean the documentation actually advises you to render widgets? Could you point me to the relevant paragraph?
Quote:
Indeed I am actually asking if what I am doing is correct
Generally speaking yes, but when it comes to your printer then apparently no. Apparently your printer driver takes a bitmap and converts each pixel of it to post-script definition instead of taking the original vector definition and using that for printout. Since you force rasterization of your data by rendering a widget to a bitmap, you surrender the vector definition of your scene in favour of a finite resolution (whatever you choose it to be).