Results 1 to 5 of 5

Thread: Subscript text in painter->drawTExt(..)

  1. #1
    Join Date
    May 2007
    Location
    Lublin, Poland
    Posts
    345
    Thanks
    40
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Subscript text in painter->drawTExt(..)

    Hi,

    I have used the subscript text while create a QGraphicsTextItem(using setHTML("<sub>...</sub>"), but now I need to draw the subscript text using a painter.
    Possibly I will use the painter->drawText(...), but how to construct a QString with a subscript text?

    Thanks for any ideas.

    Maverick
    Qt allows you to use everything you want
    wysota
    --------------------------------------------------------------------------------
    #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
    abort(); // trap; generates core dump
    #else
    exit(1); // goodbye cruel world
    #endif

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Subscript text in painter->drawTExt(..)

    How about using QTextLayout?
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    maverick_pol (8th January 2008)

  4. #3
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,312
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Subscript text in painter->drawTExt(..)

    Qt Code:
    1. void drawRichText(QPainter *painter, const QRect &rect, int flags, QTextDocument &text)
    2. {
    3. text.setPageSize(QSize(rect.width(), QWIDGETSIZE_MAX));
    4.  
    5. QAbstractTextDocumentLayout* layout = text.documentLayout();
    6.  
    7. const int height = qRound(layout->documentSize().height());
    8. int y = rect.y();
    9. if (flags & Qt::AlignBottom)
    10. y += (rect.height() - height);
    11. else if (flags & Qt::AlignVCenter)
    12. y += (rect.height() - height)/2;
    13.  
    14. QAbstractTextDocumentLayout::PaintContext context;
    15. context.palette.setColor(QPalette::Text, painter->pen().color());
    16.  
    17. painter->save();
    18.  
    19. painter->translate(rect.x(), y);
    20. layout->draw(painter, context);
    21.  
    22. painter->restore();
    23. }
    To copy to clipboard, switch view to plain text mode 

    If you need additional operations like heightForWidth() or textSize() you can have a look at the implementation of QwtRichTextEngine in the Qwt package.

    And yes, you can ask yourself, why you have to write this strange code for such a simple task.

    HTH,
    Uwe

  5. The following user says thank you to Uwe for this useful post:

    maverick_pol (8th January 2008)

  6. #4
    Join Date
    May 2007
    Location
    Lublin, Poland
    Posts
    345
    Thanks
    40
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Subscript text in painter->drawTExt(..)

    Hi,

    Thanks both of You! So what which solutions is better? Let's say I want to draw a text("upper <sub>lower</sub>"at QPointF(10,10). How to use QTextlayout to do this? I am not quite sure.

    Let's say that I set a var containing my text like this:
    Qt Code:
    1. text->setHtml("upper<sub>lower</sub>");
    To copy to clipboard, switch view to plain text mode 

    and now I need to draw the text in QPointF(10,10);

    Isn't there a simpler way than using your function Uwe? Just wondering.

    Thanks.

    Maverick
    Last edited by maverick_pol; 8th January 2008 at 21:27.
    Qt allows you to use everything you want
    wysota
    --------------------------------------------------------------------------------
    #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
    abort(); // trap; generates core dump
    #else
    exit(1); // goodbye cruel world
    #endif

  7. #5
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,312
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Subscript text in painter->drawTExt(..)

    Quote Originally Posted by maverick_pol View Post
    Isn't there a simpler way than using your function Uwe?
    My code always prints the labels into a rect. In your case you can skip the alignment lines and have to adopt it to paint to a position. But the basics of the code will remain as ugly as it is.

    I guess Scribe is designed for implementing something big like an office package and maybe it's ok for this - I never wrote one myself. But for the use case of drawing simple rich text labels, almost nobody ( I also had to ask the TrollTech support and it's by far not the first time I'm posting this code) will find the right code on his own. For a product, that takes care of a clean API, this is a very surprising situation.

    If you have a commercial license you could also have a look at the MathML renderer in the Qt solution package.

    Uwe

  8. The following user says thank you to Uwe for this useful post:

    maverick_pol (9th January 2008)

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 15:30

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.