PDA

View Full Version : drawing Text



A.H.M. Mahfuzur Rahman
13th June 2009, 22:42
How can I draw or show text from my Qt Code.
How its size and color can be changed?

Thanks is advance
Mahfuz

Lykurg
13th June 2009, 23:24
How can I draw or show text from my Qt Code.
:confused: 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.

auryce74
14th June 2009, 00:56
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 (http://doc.qtsoftware.com/4.5/tutorials.html) 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 (http://doc.qtsoftware.com/4.5/paintsystem.html) 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 (http://doc.qtsoftware.com/4.5/examples.html#painting), for example the Transformations Example.

Cheers

auryce74

A.H.M. Mahfuzur Rahman
14th June 2009, 02:25
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

aamer4yu
14th June 2009, 07:49
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.

Lykurg
14th June 2009, 07:54
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.

A.H.M. Mahfuzur Rahman
14th June 2009, 18:30
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

Lykurg
14th June 2009, 19:57
How can I make it larger and cute?
I tried setWidth but that is not handy.

QGraphicsTextItem::setFont() and simple use HTML?

auryce74
14th June 2009, 21:40
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"):


QGraphicsSimpleTextItem *myTextItem;

myTextItem = myGraphicsScene->addSimpleText("Some Text\nSpanning Two Lines",
QFont("Comic Sans MS", 24, QFont::Bold));

myTextItem->setPen(QPen(QColor("darkslateblue"), 1.5, Qt::SolidLine, Qt::RoundCap,
Qt::RoundJoin));

QLinearGradient gradient(0, 0, 0, 100);
gradient.setColorAt(0.0, "mediumslateblue");
gradient.setColorAt(1.0, "cornsilk");
myTextItem->setBrush(gradient);


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 (http://doc.qtsoftware.com/4.5/qgraphicsitem.html#setPos) function, and change it's size using the scale (http://doc.qtsoftware.com/4.5/qgraphicsitem.html#scale) function. (see the QGraphicsItem class documentation)

[edit:]
Oh, and take a look at the Basic Drawing (http://doc.qtsoftware.com/4.5/painting-basicdrawing.html) and Painter Paths (http://doc.qtsoftware.com/4.5/painting-painterpaths.html) examples (e.g. run them from inside the qtdemo program) to get a feel for the differen pen and brush settings.

A.H.M. Mahfuzur Rahman
15th June 2009, 03:36
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

aamer4yu
15th June 2009, 06:22
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.

auryce74
15th June 2009, 10:57
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.


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:


QPointF holePosition = hole->scenePos();
textItem->setPos(holePosition.x() + 10, holePosition.y());