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:
...
MyItemFormat myItemFormat(format, listId(), level);
cursor
->insertText
(QString(QChar::ObjectReplacementCharacter), myItemFormat
);
...
...
QTextCharFormat format = cursor->charFormat();
cursor->movePosition(QTextCursor::StartOfBlock);
MyItemFormat myItemFormat(format, listId(), level);
cursor->insertText(QString(QChar::ObjectReplacementCharacter), myItemFormat);
...
To copy to clipboard, switch view to plain text mode
Then I have registered handler class:
MyHandler* mh = new MyHandler(this);
document()->documentLayout()->registerHandler( MyItemFormat::Type, mh);
MyHandler* mh = new MyHandler(this);
document()->documentLayout()->registerHandler( MyItemFormat::Type, mh);
To copy to clipboard, switch view to plain text mode
I have reimplemented intristicSize and drawObject methods:
{
...
}
QSizeF MyHandler::intrinsicSize(QTextDocument * doc, int posInDocument,
const QTextFormat &format)
{
...
return QSizeF(30, 30);
}
To copy to clipboard, switch view to plain text mode
and a draw
{
....
QBrush charBrush
= charFormat.
foreground();
QFont font
(charFormat.
font());
QColor charColor
= charBrush.
color();
painter->setFont(font);
painter->setPen(charColor);
qDebug() << rect << " ; " << painter->pen() << " ; " << painter->font();
painter->drawText(rect, Qt::AlignBottom, imageText);
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);
}
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);
}
To copy to clipboard, switch view to plain text mode
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.
Bookmarks