PDA

View Full Version : Contoured text in editable QGraphicsTextItem



stampede
1st December 2009, 20:56
Hello everyone,

does anybody know how to make the text in QGraphicsTextItem contoured ?
Is it even possible ?

In order to display contoured text on graphics scene I'm using solution that i've found on this forum ( painter path :: addText and set proper brush and pen for QPainter ), but i have no idea how to draw contoured text inside text edit area in QGraphicsTextItem.
As far as I know, QGraphicsTextItem's api does not provide any methods for that.

Thanks for any help and suggestions.

Lykurg
1st December 2009, 21:50
A simple pragmatic solution would be: use an outline font.

stampede
2nd December 2009, 07:34
Thank you for suggestion.

I should've been more specific - I need to control outline width and color, and probably use some popular font like Arial or sth.

Maybe I'll try to subclass QTextObjectInterface, reimplement drawObject() and use it in text edit's document.

stampede
2nd December 2009, 19:28
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,
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;
}

then I've difined an enum value for object type, sth like:


enum{
MyFormat = QTextFormat::UserFormat + 1
};


Register a handler for this format for document:


QObject * handler = new MyTextObject();
document()->documentLayout()->registerHandler( MyFormat , handler );


Example of insertion to document ( as in richtext/textobject example ):

QTextCharFormat format;
format.setObjectType( MyFormat );
QTextCursor cursor = this->textCursor();
cursor.insertText( QString(QChar::ObjectReplacementCharacter ), format );
this->setTextCursor(cursor);


And this way I have contoured text in editable QGraphicsTextItem

If you know easier way to do this, please post it.

jichi
8th November 2011, 23:22
Thx for the code snip. It works, but the rendered text looked horrible with small font... still missing a good solution for this issue.

I am using QGraphicsTextItem to render subtitles for movies in white color. It would look much better with a contoured font. Wish Qt could consider adding such support.