Exact page layout with rich text



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

    }
    @



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


Log in to reply
 

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