Results 1 to 5 of 5

Thread: Contoured text in editable QGraphicsTextItem

  1. #1
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Question Contoured text in editable QGraphicsTextItem

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Contoured text in editable QGraphicsTextItem

    A simple pragmatic solution would be: use an outline font.

  3. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Contoured text in editable QGraphicsTextItem

    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.

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Contoured text in editable QGraphicsTextItem

    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 )

    Qt Code:
    1. void MyTextObject::drawObject( QPainter *painter,
    2. const QRectF &rect,
    3. int posInDocument,
    4. const QTextFormat &format )
    5. {
    6. painter->setPen( outlinePen );
    7. painter->setBrush( textColor );
    8.  
    9. path.addText( rect.bottomLeft(), doc->defaultFont(), characterToRender );
    10.  
    11. painter->drawPath( path );
    12. }
    13.  
    14. QSizeF MyTextObject::intrinsicSize ( QTextDocument * doc, int posInDocument, const QTextFormat & format )
    15. {
    16. const QFontMetrics metrics( doc->defaultFont() );
    17. const QSize size = metrics.size( Qt::TextSingleLine, characterToRender );
    18. return size;
    19. }
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. enum{
    2. MyFormat = QTextFormat::UserFormat + 1
    3. };
    To copy to clipboard, switch view to plain text mode 


    Register a handler for this format for document:

    Qt Code:
    1. QObject * handler = new MyTextObject();
    2. 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 ):
    Qt Code:
    1. format.setObjectType( MyFormat );
    2. QTextCursor cursor = this->textCursor();
    3. cursor.insertText( QString(QChar::ObjectReplacementCharacter ), format );
    4. 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.

  5. #5
    Join Date
    Jul 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Contoured text in editable QGraphicsTextItem

    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.

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 10:49
  2. Interactive text formatting in QGraphicsTextItem
    By olelukoie in forum Qt Programming
    Replies: 18
    Last Post: 29th October 2009, 10:49
  3. QGraphicsTextItem is not editable
    By jano_alex_es in forum Newbie
    Replies: 2
    Last Post: 29th September 2009, 16:51
  4. Rich text in QGraphicsTextItem
    By maverick_pol in forum Qt Programming
    Replies: 3
    Last Post: 29th September 2008, 21:08
  5. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 16:30

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.