PDA

View Full Version : QGraphicsTextItem - setHTML()



Claymore
14th November 2006, 21:54
Does anyone know what HTML tags are properly supported with the setHTML() function provided by the QGraphicsTextItem class? I've read up on Qt 4.2.0's support of HTML tags, but it only seems to apply to objects that use Rich Text.

My problem is that I would like to render some text in a scene, but have it be centered instead of left-justified (seems to be the default). Here's an example of the text I'd like to display:


QString text = "<center>Some<br/>Centered<br/>Text</center>";
setHTML(text);
The displayed text shows up with the appropriate line breaks, but it isn't centered. Any help is very much appreciated. :)

Thanks!

- Clay

wysota
14th November 2006, 23:50
Does anyone know what HTML tags are properly supported with the setHTML() function provided by the QGraphicsTextItem class? I've read up on Qt 4.2.0's support of HTML tags, but it only seems to apply to objects that use Rich Text.
It applies to all objects using QTextDocument, probably including QGraphicsTextItem.


My problem is that I would like to render some text in a scene, but have it be centered instead of left-justified (seems to be the default). Here's an example of the text I'd like to display:


QString text = "<center>Some<br/>Centered<br/>Text</center>";
setHTML(text);
The displayed text shows up with the appropriate line breaks, but it isn't centered. Any help is very much appreciated. :)

Did you try:

QString text = "<div align='center'>Some<br/>centred<br/>text</div>";

Maybe QTextDocument supports it...

Tonal
15th November 2006, 05:23
QGraphicsTextItem don`t centred multiline text.
See bug N138558

Angelo Moriconi
29th January 2007, 15:47
If you want to have a centered string inside a QGraphicsTextItem you can use this simple workaround ;) :

Suppose that m_gtextItem is your QGraphicsTextItem.
To change the alignment and obtaining an AlignHCenter one you should write
a method like this:




void setTextAlignment(Qt::Alignment alignment)
{
QTextCursor cursor = m_gtextItem->textCursor();
QTextBlockFormat bfmt = cursor.blockFormat();
bfmt.setAlignment(Qt::AlignCenter);
cursor.setBlockFormat(bfmt);
m_gtextItem->setTextCursor(cursor);
}



This method change the alignment of the QTextBlockFormat of the current cursor position (that would be a selection, or, if you want the entire document).

Bye bye,

Angelo

robertkun
21st July 2009, 02:07
Hello.I have a question, When I using the QGraphcisTextItem , I will set the document 's width to Align the text?

Do you any method to change the alignment ,but no need to using setTextWidth()?

:) Thank you very much!

Lykurg
21st July 2009, 07:09
Do you any method to change the alignment ,but no need to using setTextWidth()?

QGraphicsTextItem::document() -> QTextDocument::setDefaultTextOption() and then see the various options for QTextOption.

robertkun
6th August 2009, 06:36
Thank you , LyKurg
Here is my program:

void cgStaticTextElement::slot_onSetTextAlign( FParam* param )
{
int alignFlag = param->getValue<int>();
QTextOption option = document()->defaultTextOption ();
Qt::AlignmentFlag flags((Qt::AlignmentFlag)(alignFlag));
option.setAlignment ( flags );
document()->setDefaultTextOption ( option );
notifyReCache(this);
}

the option in this,have a right value ,like Qt::AlignRight , but the text in the QGraphcisTextItem 's Alignment not change.
If I have modif my program like this :

void cgStaticTextElement::slot_onSetTextAlign( FParam* param )
{
int alignFlag = param->getValue<int>();
QTextOption option = document()->defaultTextOption ();
document()->setTextWidth(200); //At here give a fixed value
Qt::AlignmentFlag flags((Qt::AlignmentFlag)(alignFlag));
option.setAlignment ( flags );
document()->setDefaultTextOption ( option );
notifyReCache(this);
}

the Alignment of the QGraphcisTextItem have changed...

I don't know why ? Would you give me a reason? Thank you .

dogu.tumerdem
17th September 2009, 09:34
It applies to all objects using QTextDocument, probably including QGraphicsTextItem.



Did you try:

QString text = "<div align='center'>Some<br/>centred<br/>text</div>";

Maybe QTextDocument supports it...

Do you mean that setHtml does not support html ? :p