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:

    QPrinter printer;
    QPrintDialog printDialog = QPrintDialog(&printer, this);
    QAbstractPrintDialog::PrintDialogOptions flags = QAbstractPrintDialog::None;

    if (printDialog.exec() == QDialog::Accepted &&
    printer.isValid()) {
    QTextDocument doc;



    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 ":http://doc.qt.nokia.com/4.7/printing.html 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?

  • 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":http://doc.qt.nokia.com/4.7/qprinter.html#supportsMultipleCopies function returns false.

    (taken from qtextdocument.cpp, lines 1790 - 1796)
    if (printer->collateCopies() == true){
    docCopies = 1;
    pageCopies = printer->supportsMultipleCopies() ? 1 : printer->copyCount();
    } else {
    docCopies = printer->supportsMultipleCopies() ? 1 : printer->copyCount();
    pageCopies = 1;

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

    Can anyone confirm this, please?

  • Well, according to the "documentation":http://doc.qt.nokia.com/4.7-snapshot/qprinter.html#supportsMultipleCopies "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)

  • 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?

  • 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.

  • 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?

  • 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.

  • 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)

  • 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.

  • 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.

  • 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.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.