Ok, QTextObjectInterface did the trick for me.
Maybe someone will be interested in how I've done this so here it is:
first, two functions in subclass of QTextObjectInterface ( MyTextObject is a subclass of QObject and QTextObjectInterface )
void MyTextObject
::drawObject( QPainter *painter,
int posInDocument,
{
painter->setPen( outlinePen );
painter->setBrush( textColor );
path.addText( rect.bottomLeft(), doc->defaultFont(), characterToRender );
painter->drawPath( path );
}
{
const QSize size
= metrics.
size( Qt
::TextSingleLine, characterToRender
);
return size;
}
void MyTextObject::drawObject( QPainter *painter,
const QRectF &rect,
QTextDocument *doc,
int posInDocument,
const QTextFormat &format )
{
painter->setPen( outlinePen );
painter->setBrush( textColor );
QPainterPath path;
path.addText( rect.bottomLeft(), doc->defaultFont(), characterToRender );
painter->drawPath( path );
}
QSizeF MyTextObject::intrinsicSize ( QTextDocument * doc, int posInDocument, const QTextFormat & format )
{
const QFontMetrics metrics( doc->defaultFont() );
const QSize size = metrics.size( Qt::TextSingleLine, characterToRender );
return size;
}
To copy to clipboard, switch view to plain text mode
then I've difined an enum value for object type, sth like:
enum{
};
enum{
MyFormat = QTextFormat::UserFormat + 1
};
To copy to clipboard, switch view to plain text mode
Register a handler for this format for document:
QObject * handler
= new MyTextObject
();
document()->documentLayout()->registerHandler( MyFormat , handler );
QObject * handler = new MyTextObject();
document()->documentLayout()->registerHandler( MyFormat , handler );
To copy to clipboard, switch view to plain text mode
Example of insertion to document ( as in richtext/textobject example ):
format.setObjectType( MyFormat );
cursor.
insertText( QString(QChar::ObjectReplacementCharacter ), format
);
this->setTextCursor(cursor);
QTextCharFormat format;
format.setObjectType( MyFormat );
QTextCursor cursor = this->textCursor();
cursor.insertText( QString(QChar::ObjectReplacementCharacter ), format );
this->setTextCursor(cursor);
To copy to clipboard, switch view to plain text mode
And this way I have contoured text in editable QGraphicsTextItem
If you know easier way to do this, please post it.
Bookmarks