Results 1 to 3 of 3

Thread: Text drawing with QPainter

  1. #1

    Question Text drawing with QPainter

    Hi,

    I have a specific need: I'm drawing text in some custom box items in a QGraphicsView. The text is created from many string and I want to have individual control over those strings. e.g.: Change the color of a string that composes the whole string:

    Angle: 123° - xxx <- I want to set the value in red for example

    So at first, i've created a function that glues the strings together and I'm able to manage them. But today, I've found about QPainter::drawText() and it's ability to wrap the text using a bounding rect. So all my custom code can be reproduced by a couple of lines using drawText().

    The problem I get is that drawText() does not support text formatting like HTML. So I've found out about QTextDocument::drawContents which supports HTML and pretty much all the same alignment options, but now the text is managed as a text document so I don't have any control over the vertical alignment of the text with the document's page. I want the vertical alignment to be related to the whole bounding rect instead of a single text line.

    So my question: Is there a way to draw some text at any position within a bounding rect and having Qt automatically manage the alignment and word wrap, but at the same time being able to put some text formatting?

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Text drawing with QPainter

    You can use a QTextDocument

    Or, did you know you can just add QLabels too?

  3. #3

    Default Re: Text drawing with QPainter

    Quote Originally Posted by tbscope View Post
    You can use a QTextDocument

    Or, did you know you can just add QLabels too?
    Hi tbscore, thanks for the reply.

    As I said in my first post, I'm able to do almost what I want with QTextDocument, but the problem is with the vertical align. I don't want to manually add line feeds before the text so it is displayed at the vertical center of the box for example. I would like the box to be considered a big area instead of lines.

    Is there a way to speficy the height of a QTextDocument line so the vertical alignment setting would actually work?

    I'll look into QLabels too, thanks for the tip!

Similar Threads

  1. Drawing a QPixmap transparently with QPainter
    By ComServant in forum Newbie
    Replies: 2
    Last Post: 11th October 2010, 23:10
  2. QPainter and complex drawing : advices
    By toutarrive in forum Qt Programming
    Replies: 0
    Last Post: 18th November 2009, 10:51
  3. drawing Text
    By A.H.M. Mahfuzur Rahman in forum Qt Programming
    Replies: 11
    Last Post: 15th June 2009, 09:57
  4. Drawing Rich Formatted Text with QPainter
    By millsks in forum Qt Programming
    Replies: 1
    Last Post: 5th March 2009, 19:59
  5. Replies: 4
    Last Post: 14th April 2008, 16:39

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.