Results 1 to 8 of 8

Thread: QGraphicsTextItem - setHTML()

  1. #1
    Join Date
    Oct 2006
    Location
    Austin, Texas, USA
    Posts
    18
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default QGraphicsTextItem - setHTML()

    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:

    Qt Code:
    1. QString text = "<center>Some<br/>Centered<br/>Text</center>";
    2. setHTML(text);
    To copy to clipboard, switch view to plain text mode 
    The displayed text shows up with the appropriate line breaks, but it isn't centered. Any help is very much appreciated.

    Thanks!

    - Clay

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsTextItem - setHTML()

    Quote Originally Posted by Claymore View Post
    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:

    Qt Code:
    1. QString text = "<center>Some<br/>Centered<br/>Text</center>";
    2. setHTML(text);
    To copy to clipboard, switch view to plain text mode 
    The displayed text shows up with the appropriate line breaks, but it isn't centered. Any help is very much appreciated.
    Did you try:
    Qt Code:
    1. QString text = "<div align='center'>Some<br/>centred<br/>text</div>";
    To copy to clipboard, switch view to plain text mode 

    Maybe QTextDocument supports it...

  3. #3
    Join Date
    Nov 2006
    Location
    Russian Federation, Novosibirsk
    Posts
    7
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsTextItem - setHTML()

    QGraphicsTextItem don`t centred multiline text.
    See bug N138558

  4. #4
    Join Date
    Jan 2007
    Location
    Rome
    Posts
    30
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QGraphicsTextItem - setHTML()

    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:

    Qt Code:
    1. void setTextAlignment(Qt::Alignment alignment)
    2. {
    3. QTextCursor cursor = m_gtextItem->textCursor();
    4. QTextBlockFormat bfmt = cursor.blockFormat();
    5. bfmt.setAlignment(Qt::AlignCenter);
    6. cursor.setBlockFormat(bfmt);
    7. m_gtextItem->setTextCursor(cursor);
    8. }
    To copy to clipboard, switch view to plain text mode 

    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

  5. #5

    Default Re: QGraphicsTextItem - setHTML()

    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!

  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: QGraphicsTextItem - setHTML()

    Quote Originally Posted by robertkun View Post
    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.

  7. #7

    Question Re: QGraphicsTextItem - setHTML()

    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 .

  8. #8
    Join Date
    Sep 2009
    Posts
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Wink Re: QGraphicsTextItem - setHTML()

    Quote Originally Posted by wysota View Post
    It applies to all objects using QTextDocument, probably including QGraphicsTextItem.



    Did you try:
    Qt Code:
    1. QString text = "<div align='center'>Some<br/>centred<br/>text</div>";
    To copy to clipboard, switch view to plain text mode 

    Maybe QTextDocument supports it...
    Do you mean that setHtml does not support html ?

Similar Threads

  1. QGraphicsTextItem - is it a bug there?
    By Tair in forum Qt Programming
    Replies: 5
    Last Post: 18th October 2006, 08:48

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.