December 27, 2011

vikinglief vikinglief
Lab Rat
9 posts

QTextDocument::print ignores # of copies from QPrintDialog

 

So I have a simple print method that will print HTML content stored in a QTextDocument. It works reasonably well, but when I print it, it ignores the number of copies set by the QPrintDialog

I have the following code:

  1. QPrinter printer;
  2. QPrintDialog printDialog = QPrintDialog(&printer, this);
  3. QAbstractPrintDialog::PrintDialogOptions flags = QAbstractPrintDialog::None;
  4. printDialog.setOptions(flags);
  5.  
  6. if (printDialog.exec() == QDialog::Accepted &&
  7.     printer.isValid()) {
  8.     QTextDocument doc;
  9.     doc.setHtml(mHtml);
  10.  
  11.     doc.print(&printer);
  12. }

where mHtml is a QString that contains the HTML content.

Now, on the QPrintDialog that pops up, I set the number of copies to 2, but when I click “Print”, it only prints 1 copy.

I could find nothing in the documentation for QPrinter, QPrintDialog, QTextDocument, or the article Printing With Qt [doc.qt.nokia.com] mention anything about having to manually handle multiple copies.

So, has anyone bumped into this before? Do I need to manually handle multiple copies? If so, any suggestions on how to handle the collate option?

10 replies

December 28, 2011

vikinglief vikinglief
Lab Rat
9 posts

Someone help me out here, please.

I’ve dug through the source code for QTextDocument::print(), and the behavior I was expecting seems to only happen if the printer’s supportsMultipleCopies [doc.qt.nokia.com] function returns false.

(taken from qtextdocument.cpp, lines 1790 – 1796)

  1.     if (printer->collateCopies() == true){
  2.         docCopies = 1;
  3.         pageCopies = printer->supportsMultipleCopies() ? 1 : printer->copyCount();
  4.     } else {
  5.         docCopies = printer->supportsMultipleCopies() ? 1 : printer->copyCount();
  6.         pageCopies = 1;
  7.     }

Does this mean that it is normally expected for my application to manually handle the printing of multiple copies?

Can anyone confirm this, please?

December 28, 2011

Chris H Chris H
Lab Rat
139 posts

Well, according to the documentation [doc.qt.nokia.com] “On most systems this function will return true. However, on X11 systems that do not support CUPS, this function will return false. That means the application has to handle the number of copies by printing the same document the required number of times.” (emphasis mine)

December 28, 2011

vikinglief vikinglief
Lab Rat
9 posts

But the phrasing on that seems to imply that for the case of it returning true, then the application does not need to handle that case.

Or am I misreading this?

December 28, 2011

Chris H Chris H
Lab Rat
139 posts

Actually, what I believe the above code is doing is handling this case for you (that is, when QPrinter won’t handle multiple copies, QTextDocument will do it for you). So it would appear that the problem is something like your printer says it supports multiple copies, but when it’s told to print them it’s not doing so.

December 28, 2011

vikinglief vikinglief
Lab Rat
9 posts

However, from other applications (such as word, adobe reader, notepad, etc.), I am able to have it print multiple copies just fine, so I am confused & frustrated as to where the disconnect lies.

Any thoughts?

December 28, 2011

Chris H Chris H
Lab Rat
139 posts

Have you compiled Qt with debugging symbols enabled? You could step in and make sure the number of copies is getting set all the way through.

December 28, 2011

vikinglief vikinglief
Lab Rat
9 posts

I’m already able to, in VS 2010, “step into” qtextdocument::print(), and walk through there. Is that sufficient? If not, how would I go about ensuring that the debugging symbols are enabled? (I already have the configure -debug-and-release flag set)

December 28, 2011

Chris H Chris H
Lab Rat
139 posts

If you can step into Qt’s functions and see the code, you’re good to go. I’d start by putting a breakpoint in QWin32PrintEngine::setProperty() (the file is qprintengine_win.cpp) where it’s setting the number of copies property.

December 28, 2011

vikinglief vikinglief
Lab Rat
9 posts

I was able to confirm that it was setting the number of copies to 2 correctly, and that the property was not being changed, so I’m still no closer to solving this issue, but I thank you for your time spent helping me on this.

December 28, 2011

Chris H Chris H
Lab Rat
139 posts

Well, I wouldn’t say “no” closer: you know where the problem isn’t now, that’s a start. I’d start putting in some more breakpoints in between the call to print() and the call to setProperty(), in places where you expect the code to go, and try to figure out at what point it’s deciding not to pass the copy count onto the printer driver.

 
  ‹‹ How can i get the text written in the QTextEdit during run time?      [Solved] QNetworkAccessManager/QThread/Signals & Slots ››

You must log in to post a reply. Not a member yet? Register here!