Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Exact page layout with rich text
Qt 6.11 is out! See what's new in the release blog

Exact page layout with rich text

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 2.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Justeco
    wrote on last edited by
    #1

    I need to generate some documents that have very precise layout. One is an invoice with a tear-off section that needs to have the addresses exactly where they need to be to be in the envelope windows. I also want to have sections of the page be rich text (multiple paragraphs in particular parts of the page).

    It seems I can ALMOST do this with QTextLayout, but it is painful to have (for example) a single bold word in a paragraph. I have tried using QTextDocument, but I can not find a way to put the document on a section of the page (and another on another section...).

    Is there not a way to have a few paragraphs of rich text laid out within a particular rectangle and placed on a page in a particular place? I can make a QTextLayout drop anywhere on a page, but I see no way to specify the beginning of a QTextDocument. I'm starting to think there isn't a way to do this easily.

    Here's some code I was playing with. Some direction would be great! Thanks for your time.

    @#include <QApplication>
    #include <QTextStream>

    #include <QFont>
    #include <QFontMetrics>
    #include <QPrinter>
    #include <QPainter>
    #include <QTextLayout>
    #include <QTextDocument>

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    QFont font("DejaVu Sans", 10, QFont::Normal, false);
    
    QPrinter printer(QPrinter::HighResolution);
    printer.setOutputFileName("bill.ps");
    printer.setPaperSize(QPrinter::Letter);
    QPainter painter;
    QFont paintFont(font, &printer);
    QFontMetrics fontMetrics(paintFont);
    
    int leading = fontMetrics.leading();
    qreal height = 0;
    
    QTextLayout textLayout("This is some text to be laid out.", paintFont);
    textLayout.beginLayout();
    
    while (1) {
        QTextLine line = textLayout.createLine();
        if (!line.isValid())
            break;
    
        line.setLineWidth(4800);
        height += leading;
        line.setPosition(QPointF(0, height));
        height += line.height();
    }
    
    textLayout.endLayout();
    
    QTextDocument textPanel;
    textPanel.setDefaultFont(paintFont);
    textPanel.setTextWidth(4800);
    textPanel.setPlainText("This is a larger chunk of text that should be turned into a nice paragraph or two at the right hand side of our first page.  However, despite reports that it can be done, I can't seem to figure out how to get a QTextDocument to display at a particular location on a page.  What gives?");
    
    painter.begin(&printer);
    textLayout.draw(&painter, QPoint(600, 600));
    //painter.translate(QPoint(100, 100));
    textPanel.drawContents(&painter, QRectF(100, 100, 4800, 6000));
    painter.end();
    

    }
    @

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      HTML (on which the [[Doc:QTextDocument]] based classes rely on) are very hard to make pixel precise - if not impossible. In that case I personally would go with a QPainter and draw the contents directly. That way you have exact control over where your data is printed.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved