PDA

View Full Version : QGraphicTextItem and outline



eric_vi
17th October 2010, 02:49
Is it possible to have an outline text on a QGraphicTextItem? I cannot find anyway to achieve that!

thank you for your help

tbscope
17th October 2010, 05:48
You need to set a pen and a brush.

If you add a text document, you can create your own layout using QTextCharFormat etc...

Edit: it is not nice to ask the same question on multiple forums. It shows a lack of respect and trust.

Lykurg
17th October 2010, 08:15
See QPainterPath::addText().

eric_vi
17th October 2010, 14:49
Hello, thank you for your hints but i do not see how the painterpath can let me have rich text as it works within QGraphicsTextItem... where do i collect it...can you explain me more

Lykurg
17th October 2010, 14:55
Is it possible to have an outline text on a QGraphicTextItem?


Hello, thank you for your hints but i do not see how the painterpath can let me have rich text as it works within QGraphicsTextItem...Where did you mentioned rich text in your question?

eric_vi
17th October 2010, 16:50
well qgraphictextitem is as opposed to qgraphicsimpletextitem and let me reedit the text, is it not?

tbscope
17th October 2010, 17:15
As I already said:
Use a QTextDocument. Iterate over the textblocks, for each cursor position, get the text format, set an outline pen in that format.

It's already there for you to use: http://doc.qt.nokia.com/4.7/qtextcharformat.html#setTextOutline

eric_vi
17th October 2010, 18:17
hi, thank you very much, it is clearer now... i do apologize i don't have your expertise.

Should i put this method reimplementing the paint of my QGraphicsTextItem

tbscope
17th October 2010, 18:35
You do not need to reimplement anything.

Instead of using a plain or rich text in the graphics text item, use a text document. Then go down a level to access the text blocks and set the character format for those blocks.

http://doc.qt.nokia.com/4.7/qgraphicstextitem.html#setDocument
http://doc.qt.nokia.com/4.7/qtextdocument.html
http://doc.qt.nokia.com/4.7/qtextblock.html

Edit:
If you use a text cursor, you can directly insert text with a certain format.
http://doc.qt.nokia.com/4.7/qtextcursor.html#insertText

this example uses text objects, but it can be usefull for your situation too:
http://doc.qt.nokia.com/4.7/richtext-textobject.html

eric_vi
17th October 2010, 19:45
okie

the user is the one that does edit the text of this qgraphicstextitem... i am not inserting any programmatically like in the richtext object example


if i try to insert the following as i am initializing my own implementation of qgraphicstextitem




QPen outlinePen = QPen (QColor(255, 0, 0), 1, Qt::SolidLine);

QTextCharFormat format;
format.setTextOutline ( outlinePen );

QTextCursor cursor;
cursor.setCharFormat ( format );
setTextCursor(cursor);



i got a cursor but i cannot edit (enter any text anymore) that is where i am stuck. If i remove those lines i can edit normally (but without outlined text!)

tbscope
17th October 2010, 20:11
i got a cursor but i cannot edit (enter any text anymore) that is where i am stuck. If i remove those lines i can edit normally (but without outlined text!)

Well, I made myself a test program and it works perfectly here, with the outline or any styling I want. Including the editing.

You might want to provide a little toolbar with editing options too if you want.

Edit: here's part of the code for inspiration:


ui->setupUi(this);

scene = new QGraphicsScene(ui->graphicsView);
ui->graphicsView->setScene(scene);

document = new QTextDocument;

QTextCharFormat charFormat;
charFormat.setFont(QFont("times", 24));

QPen outlinePen = QPen (QColor(255, 0, 0), 1, Qt::SolidLine);
charFormat.setTextOutline(outlinePen);

QTextCursor cursor = QTextCursor(document);
cursor.insertText("Test", charFormat);

textItem = new QGraphicsTextItem();
textItem->setDocument(document);
textItem->setTextInteractionFlags(Qt::TextEditable);
scene->addItem(textItem);

eric_vi
17th October 2010, 20:20
well having a toolbar is the :) plan but i have to pass the first hurdle.. so you mean just by placing



QPen outlinePen = QPen (QColor(255, 0, 0), 1, Qt::SolidLine);

QTextCharFormat format;
format.setTextOutline ( outlinePen );

QTextCursor cursor;
cursor.setCharFormat ( format );
setTextCursor(cursor);


on a qgraphicstextitem without anything extra this edits fine? do you do any setup to the QTextDocument attached to the qgraphicstextitem ?

eric_vi
17th October 2010, 20:47
was missing document() :mad:

----> QTextCursor cursor = QTextCursor(document());

it works well :D.... thank you very much for your excellent help

by the way ... it is not that i don't trust a forum if i post a question on different ones... but sometime you don't get answer on some (and i can understand that too). It is just that some issues are sometime blocking the progression of your work and you want to figure a solution as fast as possible.


Thanks again