Editiable QGraphicsTextItem problem when setting pageSize and documentMargin
-
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.
-
Which version of Qt are you using ? On which OS ?
-
Qt5.6 (from Qt.io) on Debian Jessie.
-
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. -
Can you share your implementation ?
-
from textedit.cpp
in the constructor:
scene = new QGraphicsScene; view = new QGraphicsView(scene); textEdit = new TextItem; scene->addItem(textEdit); setCentralWidget(view);
in the Load function:
bool TextEdit::load(const QString &f) { if (!QFile::exists(f)) return false; QFile file(f); if (!file.open(QFile::ReadOnly)) return false; QByteArray data = file.readAll(); QTextCodec *codec = Qt::codecForHtml(data); QString str = codec->toUnicode(data); if (Qt::mightBeRichText(str)) { textEdit->setHtml(str); } else { str = QString::fromLocal8Bit(data); textEdit->setPlainText(str); } setCurrentFileName(f); textEdit->setTextWidth(500); QTextDocument *doc = textEdit->document(); doc->setPageSize(QSizeF(500, 500)); doc->setDocumentMargin(20); return true; }
textitem.h:
class TextItem : public QGraphicsTextItem { public: TextItem(QGraphicsItem *parent = 0); QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); };
textitem.cpp
TextItem::TextItem(QGraphicsItem *parent) : QGraphicsTextItem(parent) { setCursor(QCursor(Qt::IBeamCursor)); setFlags(ItemSendsGeometryChanges); setTextInteractionFlags(Qt::TextEditorInteraction); } 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:
<br/>
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?
-
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) return;
It looks like the
pageSize.height() != -1
part is the problem. I have removed it and it seems to be working!