Results 1 to 12 of 12

Thread: drawing Text

  1. #1
    Join Date
    Jun 2009
    Posts
    29
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default drawing Text

    How can I draw or show text from my Qt Code.
    How its size and color can be changed?

    Thanks is advance
    Mahfuz

  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: drawing Text

    Quote Originally Posted by A.H.M. Mahfuzur Rahman View Post
    How can I draw or show text from my Qt Code.
    Where do you want to show the code? Inside your Qt application? Use QPlainTextEdit with syntax highlighting. There exist a tutorial or a article in QtQuarterly.

  3. #3
    Join Date
    Jun 2009
    Posts
    6
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: drawing Text

    Quote Originally Posted by A.H.M. Mahfuzur Rahman View Post
    How can I draw or show text from my Qt Code.
    How its size and color can be changed?
    What do you mean exactly?

    You use QLabel widgets to show a piece of text in your user interface. Read the Widgets Turoarial for how to do this, then read the documentation of QLabel to see how to affect it's appearance. (For example, you can call the setFont function of the label, or use html tags in the text you pass to the label)

    If you know all of this and are instead interested in using Qt to manually paint text anywhere (on screen, to an image file, etc.), read the introductory document about Qt's Paint System and the documentation of the QPainter class, to find out about the usage of the QPainter::drawText class. The size of the text drawn with QPainter can be altered by changing the QPainter's font settings with QPainter::setFont, or by transforming the painter coordinate system (see the QPainter::scale, QPainter::setWorldTransform functions). The color can be changed by setting a QPen that specifies a color (see QPainter::setPen).
    Also look at Qt's Painting Examples, for example the Transformations Example.

    Cheers

    auryce74

  4. #4
    Join Date
    Jun 2009
    Posts
    29
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: drawing Text

    Sorry, for my vague question.

    I was exactly looking for showing or painting text on a QGraphicsScene.
    I believe auryce74 has given the answer.

    Thank u both
    Mahfuz

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drawing Text

    I would suggest you look at the Qt Demo examples first...
    There are all kinds of examples a beginner wants to write,,,not even a beginner, everyone can get some reference from the demos if they look at it carefully.

  6. #6
    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: drawing Text

    Quote Originally Posted by A.H.M. Mahfuzur Rahman View Post
    I was exactly looking for showing or painting text on a QGraphicsScene.
    Then you could simply use QGraphicsSimpleTextItem or QGraphicsTextItem, without doing the painting yourself.

  7. #7
    Join Date
    Jun 2009
    Posts
    29
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: drawing Text

    I am trying with QGraphicsTextItem and QGraphicsSimpleTextItem. I can draw text but the size of the text is so small that I have to zoom to see the text. Also, the text is looking so simple.

    How can I make it larger and cute?
    I tried setWidth but that is not handy.

    please help

  8. #8
    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: drawing Text

    Quote Originally Posted by A.H.M. Mahfuzur Rahman View Post
    How can I make it larger and cute?
    I tried setWidth but that is not handy.
    QGraphicsTextItem::setFont() and simple use HTML?

  9. #9
    Join Date
    Jun 2009
    Posts
    6
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: drawing Text

    Quote Originally Posted by A.H.M. Mahfuzur Rahman View Post
    I am trying with QGraphicsTextItem and QGraphicsSimpleTextItem. I can draw text but the size of the text is so small that I have to zoom to see the text. Also, the text is looking so simple.

    How can I make it larger and cute?
    I tried setWidth but that is not handy.
    QGraphicsSimpleTextItem is probably the easier one of the two classes for just adding a bit of nice-looking text to a QGraphicsScene. You could use it to create a pretty text item in your scene like this (if your scene is called "myGraphicsScene"):

    Qt Code:
    1.  
    2. myTextItem = myGraphicsScene->addSimpleText("Some Text\nSpanning Two Lines",
    3. QFont("Comic Sans MS", 24, QFont::Bold));
    4.  
    5. myTextItem->setPen(QPen(QColor("darkslateblue"), 1.5, Qt::SolidLine, Qt::RoundCap,
    6. Qt::RoundJoin));
    7.  
    8. QLinearGradient gradient(0, 0, 0, 100);
    9. gradient.setColorAt(0.0, "mediumslateblue");
    10. gradient.setColorAt(1.0, "cornsilk");
    11. myTextItem->setBrush(gradient);
    To copy to clipboard, switch view to plain text mode 

    You can change:
    • the font family, size and style by modifying the parameters of the QFont that's passed to the addSimpleText function. (It could also be changed on a QGraphicsSimpleTextItem that's already bee created, by calling the setFont function). See the QFont class documentation.
    • the outline of the text, by modifying the parameters of the QPen that's passed to the setPen function. See the QPen class reference.
    • the fill color / gradient / pattern of the text, by modifying the QBrush that's set with the setBrush function. (In this example, a gradient is used directly without constructing a QBrush object first). Read the QBrush class documentation to find out what else is possible.


    (Also consult the QColor class documentation, for the different possibilities of how to define a color.)

    You can then always move the item to any position in the scene using the setPos function, and change it's size using the scale function. (see the QGraphicsItem class documentation)

    [edit:]
    Oh, and take a look at the Basic Drawing and Painter Paths examples (e.g. run them from inside the qtdemo program) to get a feel for the differen pen and brush settings.
    Last edited by auryce74; 14th June 2009 at 20:52.

  10. The following user says thank you to auryce74 for this useful post:

    A.H.M. Mahfuzur Rahman (15th June 2009)

  11. #10
    Join Date
    Jun 2009
    Posts
    29
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: drawing Text

    Thanks. I have managed to make in cute and large.
    But, one problem.

    Consider the case:

    I have Board,Hole,Ball classes
    Board is parent of Hole and Ball. All of Board,Hole and Ball class is subclass of QGraphicsSvgItem.

    Zvalue 0f Board -> 0
    Hole -> 1
    Ball -> 2

    Now, I create a textItem which is child of Hole and set its zValue to 3. I saw that the Ball Item is on text Item on my executed program.

    Please clear me the ZValue concept and help so that i can place text on ball item

    I need to do it from Hole class because I want to show the text on a Hole not a ball.

    Thanks in advance
    Mahfuz

  12. #11
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drawing Text

    zValue has nothing to do with position... zValue is about depth.
    Say you have 3 items A,B and C with zValues 0, 1 and 2 respectively. Now if these three items overlap in position then the item with higher zValue will be drawn on top. Hence in above case, C will be on top of B and B on top of A.

  13. #12
    Join Date
    Jun 2009
    Posts
    6
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: drawing Text

    Quote Originally Posted by A.H.M. Mahfuzur Rahman View Post
    Now, I create a textItem which is child of Hole and set its zValue to 3. I saw that the Ball Item is on text Item on my executed program.
    If the textItem is a child of Hole, setting its zValue to 3 sets its arrangement within the hole item, in other words it merely affects whether it will be shown above or below other child items of hole (if there are any).

    Other top-level items that are on top of the hole item will consequently also be on top of all of hole's child items.

    Quote Originally Posted by A.H.M. Mahfuzur Rahman View Post
    I need to do it from Hole class because I want to show the text on a Hole not a ball.
    No, make the text a top-level item of it's own, and manually set it's position next to that of the hole, for example with something like this:

    Qt Code:
    1. QPointF holePosition = hole->scenePos();
    2. textItem->setPos(holePosition.x() + 10, holePosition.y());
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Replies: 2
    Last Post: 23rd July 2012, 08:42
  3. Drawing Text
    By QbelcorT in forum Qt Programming
    Replies: 7
    Last Post: 8th March 2009, 17:33
  4. Drawing Rich Formatted Text with QPainter
    By millsks in forum Qt Programming
    Replies: 1
    Last Post: 5th March 2009, 19:59
  5. Problems with QString
    By cyberboy in forum Qt Programming
    Replies: 2
    Last Post: 13th October 2008, 08:18

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
  •  
Qt is a trademark of The Qt Company.