No examples found on how to derive from QAbstractTextDocumentLayout

  • I've been playing with QTextDocument a lot recently, trying all the different methods of rendering the rich text data. The document's draw method is useful for a text editor, but I'm making a custom display that works for text-based games (Telnet servers). I was rendering the whole document and then translating the painter to "scroll up" the canvas. It was very slow, as you can imagine.

    I am currently using QTextLayout do draw from the last QTextBlock up the window until the coordinates are out of view. This works much, much faster, and I'm pleased with the results.
    @ QRectF r(rect());

    int y = r.height();
    QTextBlock textBlock(m_document->lastBlock());
    while (y > 0 && textBlock.isValid())
        QTextLayout textLayout(textBlock);
        QList<QTextLine> lines;
        QTextLine line(textLayout.createLine());
        while (line.isValid())
            y -= line.height();
            line = textLayout.createLine();
        int lineY = y;
        foreach (line, lines)
            line.setPosition(QPointF(0, lineY));
            lineY += line.height();
        textLayout.draw(&painter, QPoint(0, 0));
        textBlock = textBlock.previous();


    Now, I'm faced with the problem of trying to calculate coordinates for mouse events, set up selections, change formats, and all the more advanced stuff. Changing to the implementation of a proper layout derived from QAbstractTextDocumentLayout seems like the way to move forward, but this is proving to be a monumental task... because so much of the implementation used by Qt itself is made inaccessible to the end user.

    Could someone give me a helping hand or point me in the right direction before I try to re-implement all the Qt private code and adapt it to my needs?

Log in to reply

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