Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, March 2008
Print with QSyntaxHighlighter


Message 1 in thread

I'm using Qt 4.3.2 and I have a QTextEdit with an attached 
QSyntaxHighlighter. I'm printing the document using the print routines 
shown in the Qt documentation:.

      QPrinter printer;

      QPrintDialog *dialog = new QPrintDialog(&printer, this);
      dialog->setWindowTitle(tr("Print Document"));
      if (editor->textCursor().hasSelection())
          dialog->addEnabledOption(QAbstractPrintDialog::PrintSelection);
      if (dialog->exec() != QDialog::Accepted)
          return;

      editor->print(&printer);


This works fine except that the syntax highlighting does not print. The 
text is all black and in the same font.

What additional or other steps need to be performed to include the 
syntax highlighting information in the printout?

Thanks,

Tom

--
 [ signature omitted ] 

Message 2 in thread

On Thursday 06 March 2008 01:25:38 Thomas Rock wrote:
> I'm using Qt 4.3.2 and I have a QTextEdit with an attached
> QSyntaxHighlighter. I'm printing the document using the print routines
> shown in the Qt documentation:.
>
>       QPrinter printer;
>
>       QPrintDialog *dialog = new QPrintDialog(&printer, this);
>       dialog->setWindowTitle(tr("Print Document"));
>       if (editor->textCursor().hasSelection())
>           dialog->addEnabledOption(QAbstractPrintDialog::PrintSelection);
>       if (dialog->exec() != QDialog::Accepted)
>           return;
>
>       editor->print(&printer);
>
>
> This works fine except that the syntax highlighting does not print. The
> text is all black and in the same font.
>
> What additional or other steps need to be performed to include the
> syntax highlighting information in the printout?

This is fixed in Qt 4.4. The attached patch fixes it :)


Simon
--- a/src/gui/text/qtextdocument.cpp
--- a/src/gui/text/qtextdocument.cpp
+++ b/src/gui/text/qtextdocument.cpp
@@ -1435,6 +1435,12 @@ void QTextDocument::print(QPrinter *printer) const
         doc = clone(const_cast<QTextDocument *>(this));
         clonedDoc = const_cast<QTextDocument *>(doc);
 
+        for (QTextBlock srcBlock = firstBlock(), dstBlock = clonedDoc->firstBlock();
+             srcBlock.isValid() && dstBlock.isValid();
+             srcBlock = srcBlock.next(), dstBlock = dstBlock.next()) {
+            dstBlock.layout()->setAdditionalFormats(srcBlock.layout()->additionalFormats());
+        }
+
         QAbstractTextDocumentLayout *layout = doc->documentLayout();
         layout->setPaintDevice(p.device());
 

Attachment:

Attachment: signature.asc
Description: This is a digitally signed message part.