PDA

View Full Version : QTextDocument layout problem with text disappears



'milimoj
29th June 2015, 20:08
Hi,
I am making a text editor using QGraphicTextItem and QTextDocument clases and I have a problem with text that disappears.

I have inserted my custom item at the begining of the block:

I use my CharFormat for the item:


...
QTextCharFormat format = cursor->charFormat();

cursor->movePosition(QTextCursor::StartOfBlock);
MyItemFormat myItemFormat(format, listId(), level);
cursor->insertText(QString(QChar::ObjectReplacementCharact er), myItemFormat);
...

Then I have registered handler class:


MyHandler* mh = new MyHandler(this);

document()->documentLayout()->registerHandler( MyItemFormat::Type, mh);

I have reimplemented intristicSize and drawObject methods:


QSizeF MyHandler::intrinsicSize(QTextDocument * doc, int posInDocument,
const QTextFormat &format)
{
...
return QSizeF(30, 30);
}


and a draw


void MyHandler::drawObject(QPainter *painter, const QRectF &rect,
QTextDocument * doc, int posInDocument,
const QTextFormat &format)
{
....
QTextCharFormat charFormat = format.toCharFormat();
QBrush charBrush = charFormat.foreground();
QFont font(charFormat.font());
QColor charColor = charBrush.color();

QString imageText = "AA";

painter->setFont(font);
painter->setPen(charColor);

qDebug() << rect << " ; " << painter->pen() << " ; " << painter->font();

painter->drawText(rect, Qt::AlignBottom, imageText);

QRectF rect1(rect);
rect1.translate(-30, 0); // move new rect a little bit to the left to draw on left margin

imageText = "B";

qDebug() << rect1 << " ; " << painter->pen() << " ; " << painter->font();

painter->drawText(rect1, Qt::AlignBottom, imageText);

}


And this works fine. AA is drawn on start of text block and B is drawn on left margin.
But, when I select some text in the text block AA is still visible, but B disappears.

Debug print is not changed when text in block is selected (still show correct rect size and position, and pen and font colors) but text B is not visible.

Any idea??? I sopose that drawObject is not intended for drawing outsidee the QTextBlock rectangle or something like that but I am sure that there is a way to override this problem, just I do not see it.

'milimoj
5th July 2015, 09:10
No idea why text is not visible???
At least, I would like to understand what happend, eaven if there is no solution.