Important: Please read the Qt Code of Conduct -

Editiable QGraphicsTextItem problem when setting pageSize and documentMargin

  • I'm creating an editor using a QGraphicsTextItem. Everything works, but I ran into a problem when setting the pageSize and the documentMarging. When the pageSize and the documentMargin are set, the contents of the QGraphicsTextItem is shown as in a documentview. However, for some reason the last lines in the document become visualy uneditable. In other words, I cannot place the cursor or select that text with the mouse.

    Although I've tried to narrow the problem down to the pageSize or the documentMargin, so far I've not found a solution. Also, extending the height of the boundingRect did not solved the problem.

    Someone any idea how to solve this?


    When setting only the documentMargin, all is well. However, the problem occurs when setting the pageSize. For some reason the last lines become visual uneditable. My guess is that is has something to do with the viewport settings in the private QTextDocumentLayout.

    Someone any idea?

  • Lifetime Qt Champion


    Is there a specific size that triggers that or any size does it ?

  • Hi,

    I've checked it with different sizes and it seems be happening with all of them.

    Initial I used QSizeF(500, 500) and changed the height to 600 and 700. While the exact line from where it becomes uneditable changes, the basic problem remains. After that I've tried changing to totaly different sizes with more or less the same outcome.

    However, when setting the pageSize for example to QSizeF(540, 500) and the QGraphicsTextItem::textWidth to 500, the whole text from top to bottom becomes editable (I can click with the mouse and place the textCursor there). Problem is that the text on the right becomes visualy uneditable, probably because of the difference in textWidth and pageSize.width.

    When also giving a documentMargin of 20, the textWidth had to become 440 to achieve the same result.

  • Lifetime Qt Champion

    Which version of Qt are you using ? On which OS ?

  • Qt5.6 (from on Debian Jessie.

  • Lifetime Qt Champion

    Can you check against the latest version to see if it still happening ?

  • I just installed 5.7 and ran the program. Still the same problem.

    Perhaps something wrong with my implementation?
    I've adjusted the TextEdit example as in using a QGraphicsTextItem instead of a QTextEdit. In the Load function, after setting the contents of the QGraphicsTextItem, I set the pageSize and the documentMargin.

  • Lifetime Qt Champion

    Can you share your implementation ?

  • from textedit.cpp

    in the constructor:

    scene = new QGraphicsScene;
    view = new QGraphicsView(scene);
    textEdit = new TextItem;

    in the Load function:

    bool TextEdit::load(const QString &f)
        if (!QFile::exists(f))
            return false;
        QFile file(f);
        if (!
            return false;
        QByteArray data = file.readAll();
        QTextCodec *codec = Qt::codecForHtml(data);
        QString str = codec->toUnicode(data);
        if (Qt::mightBeRichText(str)) {
        } else {
            str = QString::fromLocal8Bit(data);
        QTextDocument *doc = textEdit->document();
        doc->setPageSize(QSizeF(500, 500));
        return true;


    class TextItem : public QGraphicsTextItem
        TextItem(QGraphicsItem *parent = 0);
        QRectF boundingRect() const;
        void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);


    TextItem::TextItem(QGraphicsItem *parent)
        : QGraphicsTextItem(parent)
    QRectF TextItem::boundingRect() const
        return QRectF (this->pos(), QSizeF(540, 8000));
    void TextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
        QRectF r1(0, 0, 500, 500);
        QRectF r2(0, 500, 500, 500);
        QRectF r3(0, 1000, 500, 500);
        painter->fillRect(r1, QBrush(Qt::white));
        painter->fillRect(r2, QBrush(Qt::red));
        painter->fillRect(r3, QBrush(Qt::blue));
        QGraphicsTextItem::paint(painter, option, widget);

    As you can see I have kept the implementation quite simple for testing purposes.

  • Some observations I made when going through the source code of QGraphicsTextItem, QTextDocument and QWidgetTextControl:

    • setting the textWidth on a QGraphicsTextItem will in the end result in setting a pageSize QSizeF(textWidth, -1) on the QTextDocument, so it should not be necessary to setting a textWidth and a pageSize;
    • in the function QGraphicsTextItem::paint the option->exposedRect is used for the drawing of the contents from the QWidgetTextControl. The controlOffset is used for setting the position of the exposedRect, which is equal to the pagenumber * pageheight;
    • the size of the viewportRect in the documentLayout is set through setViewport. This is a setting in the private QTextDocumentlayout, so not for us to use. Since the result of the viewportRect seems to be only used if the textWidth/pageSize.width == 0, it is probably of no concern;

  • I've added some blank lines to the end of the example html file with:


    the whole text became editable, except of course for the lines at the end.

    After that I added some blank lines in the text (with the enter key).
    Lets say there are about 222 visible lines of text. From line 219 the text becomes visually uneditable. After entering five more line, the text below moves down accordingly.
    But from line 219 the text remains visually uneditable.

    Somehow the 'viewport' is not adjusted. While, when the documentMargin and the textWidth are set, everything behaves like it is expected to. But when setting the pageSize, the problems arise.

    Since textWidth sets the pageSize.width while the pageSize.heigth remains -1, it seems that there is something with the pageSize.height which is screwing up things. Or something else I forgot to do when setting the pageSize, but what?

  • After a lot of tinkering I still haven't found the cause of the problem, let alone a solution.
    Of course I can add a QTextEdit to a QGraphicsScene, but that still doesn't explain what's happening with the QGraphicsTextItem.

    Someone an idea?

  • Lifetime Qt Champion

    You may have found a bug. Did you check the bug report system ?

  • I have checked it and created a new bug request: QTBUG-55527

  • I think I have found the problem. I went through the source code of the QGraphicsTextItem and came across the private _q_updateBoundingRect. In there there is a line:

    if (size == qq->m_boundingRect.size() || pageSize.height() != -1)

    It looks like the pageSize.height() != -1 part is the problem. I have removed it and it seems to be working!