You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Friday, March 2nd 2012, 7:13pm

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 difference 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 seem to occur at the render() call.


Here is attached example 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);
QTextEdit *qteTestPrintRequest = new QTextEdit("",w);
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 );

QPrinter printer(QPrinter::HighResolution);
QString docName = "Test Plot";
if ( !docName.isEmpty()) {
docName.replace (QRegExp (QString::fromLatin1 ("\n")), tr (" -- "));
printer.setDocName (docName);
}
QPrintDialog dialog(&printer);
if ( dialog.exec() ) {
{
{
QPainter painter(&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);
}
}
}
}


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

2

Friday, March 2nd 2012, 7:15pm

I am running Qt 4.7 on an XP machine and using Creator. The code has worked fine on 4 printers from several different PC configurations.

3

Friday, March 2nd 2012, 10:07pm

code tags
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

4

Wednesday, March 7th 2012, 11:12am

Thank you for your reply.

I am unsure as to how I have given the impression that the code that I provided was anything other than cut and pasted code taken from the application in question and I apologise for the confusion. The code was, however, cut and paste directly from the application causing the problem, Nothing has been removed or added.

Clearly this is not from an actual application but I isolated the problem to a more reduced version of the original functionality removing any irrelevant content from the original code. I then recompiled the application in the reduced form and then tested that exactly the same problem occurred in the reduced code format, which it did.

I had hoped that the code was isolated enough to identify the issue from the code that I have shown. If it is required to provide that code in a compilable form with additional project file, header & main.cpp then I will do so. The example test application does nothing other than request to print this test example though.

5

Wednesday, March 7th 2012, 12:41pm

I think you were replying to my sig, which was not necessarily meant here.

Having said that, I don't know why render is crashing - you haven't given any kind of error message or said what kind of debugging you have done.

if possible, I would try printing to the troublesome printer when it is directly connected to the same machine as where the code is running.

** Have you actually used the debugger and found what is causing the crash? **
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

6

Wednesday, March 7th 2012, 2:21pm

Ahh sorry about responding to your sig. My mistake I didn't read your post correctly.

Regarding the code tags they kept formatting my code into an illegible form. I must be doing something wrong or is it from using Google Chrome?

Unfortunately the error is reported by a remote tester and only occurs on their printer when testing a deployed release of the application. I therefore cannot use the debugger to determine the cause. I have used QMessageBoxes to identify that the error only occurs when the code reaches the render statement.

Unfortunately there is no discernable error message. The application simply freezes and the tester reports having to use Ctrl-Alt-Delete to continue.

It may be difficult to test by correcting directly to the printer in question due to the proximity of the network printer to the tester. I may be able to arrange by pulling a few strings. Is there a potential problem with addressing networked printers?

7

Wednesday, March 7th 2012, 5:49pm

"Is there a potential problem with addressing networked printers?"
Not that I know of. Have you tried printing to that printer? What happens?
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

8

Wednesday, March 7th 2012, 10:34pm

Have you read my post?

9

Wednesday, March 7th 2012, 10:44pm

I cannot personally test the printer as I am not on the site where the printer network is. The tester's experience is as posted above. I hope that this clarifies things. I appreciate that it is a difficult situation.

10

Wednesday, March 7th 2012, 11:37pm

Well then there is no way to reasonably debug unless you can vpn and attach + remote debug. Personally, I wouldn't entertain being asked to fix something with the caveat that no logical or methodical diagnoistics tools are allowed...

Also, there's nothing in your code to suggest that the crash has to come from render and not some point afterwards. If you dont have access to test, how do you know the problem is at render()?
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

11

Thursday, March 8th 2012, 10:01am

I appreciate that this is a difficult situation. Unfortunately I am left in the position where I have to solve the problem. The bottom line is that the application has to work with any existing printer set up that is currently working for other print requirements. The problem came after fairly extensive testing of a print function that has been added to an existing application. The problem has only occurred when accessing 1 specific networked printer. All of my personal tests have worked fine.

I can't be certain that the problem occurs at render() but 2 things are true.

1. If the user clicks cancel at the print dialog stage there is no crash.
2. In a deployment using QMessageBox es the crash does not occur before the render button and does seem to occur before the line after render().

There is a also no direct logic occurring in the printTest() method after the render() function so this limits the possibilities a little. Do you think it could be some sort of background issue e.g. with memory management? Sorry for poor terminology.
I wll try a deployment option that is identical except that it is without the render function to see if this causes a crash.This would at least identify if the render() call was likely to at least indirectly cause the issue.

12

Thursday, March 8th 2012, 10:12am

well, the simplest thing to do is to recompile the app with subsystem = console. Then if there are any qt debug messages on the console, the user can view them and report back.

If the problem is a bad alloc (I doubt it), you can add a try{}catch (std::exception){} and output to the console as well.

However, app freezing and not giving any information to user, as well as o/s not reporting a crash sounds more like program has hung - this would lead me to first investigate how well your app/qt communicates on the network
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

13

Thursday, March 8th 2012, 10:50am

Thank you for such quick and informative responses.

I will produce a version with subsystem = console and the "try{} catch" output.

How would you best advise investigating the app's ability to communicate on the network, other than trying to arrange a test with a PC directly connected to the printer in question?

14

Thursday, March 8th 2012, 12:15pm

I think you really have to be on that network somewhere - there are just too many variables to consider otherwise.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

15

Thursday, March 8th 2012, 1:33pm

As a result of your welcome advice I am trying to arrange a visit to the site where the printer issue has been identified.

Do you have any key network related debug information that I should look at generating / analysing? I am new to this sort of problem.

16

Thursday, March 8th 2012, 2:52pm

You could run some simple printer tests, generally making sure the printer works from that machine anyway.

But ultimately, if you can get on site, then you can take a debug build and step through the code line by line - that is likely to be the most helpful work.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

17

Friday, March 9th 2012, 5:15pm

Slightly surprising result from on site testing today. The problem seems to be down to the size of the print document for this particular printer. It was over 210 MB for the single page of the QTextEdit->render() example above. This is approximately 4,000 times the size of the document sent to other test printers.

Any ideas on why such a large file is produced when sending the rendered data to a particular printer. The printer in question is a

Xerox Work Centre 7428 PS.

Is this likely to be a driver issue? Word documents and indeed any other print requests seem unaffected.

18

Friday, March 9th 2012, 5:47pm

The resolutions from the scaling used today with the 213MB document were as follows:

Printer: 4908, 6408 QTextEdit widget: 750, 500
scalex: 6.544, scaley: 12.816, scale: 6.544

There were no debug messages and no error messages and the file size was the same using the printer locally or over the network. Everything seems to point to the extreme document size being the only issue.

Am I somehow introducing the issue into my code?
Is there an easy way to force a smaller print document format?
Could it be anything to do with the font code? (I feel like I'm clutching at straws here)

RECAP OF MAIN CODE

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
void MainWindow::menuPrintTest(){
    double xscale;  double yscale;  double scale;   double printerWidth ;   double printerHeight ;  double widgetWidth; double widgetHeight ;
    
    QFont printFont("courier",  16);
    printFont.setFixedPitch(TRUE);
    QTextEdit *qteTestPrintRequest = new QTextEdit("",w);
    qteTestPrintRequest ->setFont(printFont );
    qteTestPrintRequest ->setFixedHeight(500); 
    qteTestPrintRequest ->setFixedWidth(750);  
    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 );

    QPrinter printer(QPrinter::HighResolution);
    QString docName =   "Test Plot";
    if ( !docName.isEmpty())  {
        docName.replace (QRegExp (QString::fromLatin1 ("\n")), tr (" -- "));
        printer.setDocName (docName);
     }
     QPrintDialog dialog(&printer);
     if ( dialog.exec() )  {
               QPainter painter(&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);
              } //QPAINTER CTOR BRACE
    }else{}
    delete qteTestPrintRequest; 
}

19

Friday, March 9th 2012, 6:05pm

Could it be caused by the method for rendering QTextEdit?

http://doc.qt.nokia.com/4.7-snapshot/printing.html



Widget, Printing function, Accepts

QGraphicsView, QGraphicsView::render(), QPainter

QSvgWidget, QSvgRenderer::render(), QPainter

QTextEdit, QTextDocument::print(), QPrinter

QTextLayout, QTextLayout::draw(), QPainter

QTextLine, QTextLine::draw(), QPainter

20

Friday, March 9th 2012, 6:19pm

If this is the case then what is the equivalent code apart from the obvious substitution of:

Source code

1
2
3
qteTestPrintRequest->print(&printer);
//for
//qteTestPrintRequest->render(&painter);


How does this effect custom composite widgets that contain QTextEdits as well as other widgets?

Can they still use

Source code

1
customWidgetPtr->render(&painter)


?