Results 1 to 2 of 2

Thread: print several pages

  1. #1

    Question print several pages

    Hi.

    Down is my print function. As advised by the answers to my previous question I am using qtextdocument with html to print my text.
    I have made some progress.
    I have a query to database to grab some text and create a few paragraphs to print.
    Until here its ok, but if my blocks of text are several and if they are large, its natural that I need more pages to print.
    I know that I can use printer->newPage() to create a new printing page. But the text will be all written to the first page and INCOMPLETE if larger than the page!
    It should go to the next page...
    How can I solve this problem?
    Can someone point me to the right direction please?

    Thanks a lot...

    daniel



    void Janela:rint( QPrinter* printer )
    {
    // create painter for drawing print page
    QPainter painter( printer );
    int w = printer->pageRect().width();
    int h = printer->pageRect().height();
    QRect page( 0, 0, w, h );
    // create a font appropriate to page size
    QFont font = painter.font();
    font.setPixelSize( (w+h) / 120 );

    QTextDocument doc;
    QTextCursor cursor(&doc);
    doc.setDefaultFont(font);

    doc.setHtml( "<html>"
    "<body>"
    "<h2>bla bla bla bla bla</h2>");
    cursor.insertBlock();
    cursor.insertBlock();
    cursor.insertHtml("<h3>bla bla</h3>");
    cursor.insertBlock();

    db.setDatabaseName(NomeBD);
    db.open();
    QSqlQuery query;
    query.exec("SELECT * FROM table WHERE date >= #10-29-2010#");
    while (query.next())
    {
    cursor.insertHtml("<h4>" + query.value(0).toString().right(8) + " - " + query.value(1).toString() + " - " + query.value(2).toString() + ":</h4>");
    cursor.insertBlock();
    cursor.insertHtml("<p>" + query.value(3).toString() + "</p>");
    cursor.insertBlock();
    }
    db.close();
    cursor.insertHtml("</body>");
    cursor.insertHtml("</html>");

    doc.setTextWidth(page.width());
    doc.setDocumentMargin(w/10);
    doc.drawContents(&painter, page);

    }

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: print several pages

    Why not use QTextDocument::print () directly? Set up the printer, pass it to QTextDocument, and let Qt do the pagination work for you.

    A tip: use [code] tags around your code. See the # symbol on the tool bar of the forum editor.

Similar Threads

  1. QTableView with pages
    By sysmaniac in forum Newbie
    Replies: 1
    Last Post: 4th November 2010, 18:11
  2. Not print the range of pages
    By estanisgeyer in forum Qt Programming
    Replies: 2
    Last Post: 3rd December 2009, 17:18
  3. Cancelling Long-running Print/Print Preview
    By ChrisW67 in forum Newbie
    Replies: 4
    Last Post: 16th June 2009, 23:05
  4. Creating pages
    By hgedek in forum Qt Programming
    Replies: 1
    Last Post: 19th July 2007, 15:11
  5. switching between pages
    By Kapil in forum Newbie
    Replies: 9
    Last Post: 6th March 2006, 10:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.