The Ultimate Qt Community site
Home News Forum Wiki Contest FAQ Links

Go Back   Qt Centre Forum > Qt > Qt Programming

Qt Programming General Qt programming issues.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 28th June 2006, 20:49
darkadept darkadept is offline
Intermediate user
 
Join Date: May 2006
Qt products used: Qt4
Qt platforms used: Unix/X11 , Windows
Posts: 66
Thanks: 11
Thanked 1 Time in 1 Post
Default Printing QTextDocument (by painting, not the .print() func)

I am attempting to print out a QTextDocument to the printer with more
control then the QTextDocument.print function. I've searched high
and low for any documentation pertaining to the Qt4 print system in
regards to rich text and have found very little information. See below for the code
I have so far.

For now I am only attempting to print page 1 and page 2 but it doesn't
seem to work quite right. On page 1 it prints properly except an outline
box that prints on the very bottom that seems like it's from page 2.
On page 2 it prints an outline box around where page 1 should be and then
it starts printing page 2 below that but page 2 is cut off.

It seems that the draw() function isn't obeying the clip context completely.

Can anyone shed some light on this. Or is there a better way to print QTextDocuments.
(besides the .print() function).

Thanks in advance for the help!

-Mike


Qt 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
38
39
40
41
42
void ReportPreview::on_printButton_clicked( )
{
    QPrinter prn(QPrinter::HighResolution);
    prn.setOutputFormat(QPrinter::PdfFormat);
    prn.setOutputFileName("test.pdf");
    prn.setPageSize(QPrinter::Letter);
    prn.setColorMode(QPrinter::GrayScale);
    prn.setOrientation(QPrinter::Landscape);
    
    QPrintDialog prnDiag(&prn, this);
    if (prnDiag.exec() == QDialog::Accepted)
    {
        QPainter p;
        QAbstractTextDocumentLayout::PaintContext ctx;
        QTextDocument *t = textBrowser->document()->clone(0); //Create a copy of the TextDocument
 
        qreal w = (qreal)prn.width();   //Printer's page width
        qreal h = (qreal)prn.height()//Printer's page height
 
        //Set the TextDocument's page size
        t->documentLayout()->setPaintDevice(&prn);
        t->setPageSize(QSizeF(w,h));
 
        p.begin(&prn); //Start printing
 
        //Page 1
        ctx.clip = QRectF(0,0,w,h);     //Set the context clip to the first page
        t->documentLayout()->draw(&p,ctx);
 
        //Page 2
        prn.newPage();
        ctx.clip = QRectF(0,h+1,w,h);       //Set the context clip to the second page
        t->documentLayout()->draw(&p,ctx);
 
        //Eventually there will be a FOR loop printing the pages specified in prnDiag
        
        p.end(); //End printing
 
        //Clean up
        delete t;
    }
}
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 00:51.


Powered by vBulletin Version 3.7.4 Copyright ©2000 - 2009, Jelsoft Enterprises Ltd., vRewrite 1.5 SEOed URLs completed by Tech Help Forum and Chalo Na.
© 2006–2009 Qt Centre - The Ultimate Qt Community site
Nokia, Qt and their respective logos are trademarks of Nokia Corporation in Finland and/or other countries worldwide.