Results 1 to 7 of 7

Thread: Immediate help is required on QGraphicsTextItem

  1. #1
    Join Date
    Feb 2008
    Posts
    51
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Immediate help is required on QGraphicsTextItem

    Hi,

    I have customized QGraphicsTextItem, added into scene.

    I want to align the text in that item.

    I tried following way but unable to get results.

    Please help me.

    alignment - is Qt::Alignment

    Qt Code:
    1. QTextCursor objCursor = textCursor();
    2. objCursor.movePosition(QTextCursor::Start);
    3. objCursor.movePosition(QTextCursor::StartOfBlock);
    4. QTextBlockFormat txtBlockFormat = objCursor.blockFormat();
    5. txtBlockFormat.setAlignment(alignment);
    6.  
    7. objCursor.setBlockFormat(txtBlockFormat);
    8.  
    9. //or
    10. objCursor.mergeBlockFormat(txtBlockFormat);
    11.  
    12. setTextCursor(objCursor);
    To copy to clipboard, switch view to plain text mode 

    The above code is supposed to align first block only.

    I am expecting the results shown in attached image file.

    Is there any other way to do it?

    Thanks in advance.

    Regards,
    Sandip
    Last edited by wysota; 19th November 2008 at 09:56. Reason: missing [code] tags

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

    Default Re: Immediate help is required on QGraphicsTextItem

    Select the first block before applying the block format.

  3. #3
    Join Date
    Feb 2008
    Posts
    51
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Immediate help is required on QGraphicsTextItem

    Quote Originally Posted by wysota View Post
    Select the first block before applying the block format.
    What do you mean by "Select the first block"?

    I tried

    objCursor.select(QTextCursor::BlockUnderCursor);

    is it correct?

    thanks
    Sandip

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

    Default Re: Immediate help is required on QGraphicsTextItem

    Yes, that's what I meant. Just make sure your text really contains more than one block or else you'll achieve the same result as before.

  5. #5
    Join Date
    Feb 2008
    Posts
    51
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Immediate help is required on QGraphicsTextItem

    Hi Wysota,

    It is working after setting text width of document.

    Is there any way to tell it to grow or shrink automatically?

    If text width will be set to -1 then it grows and shrinks automatically but alignment doesn't works.

    Problem is that bounding dotted rectangle is shown according to text width but actual text may be too small. So it looks bad in my application.

    Thanks for your replies.

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

    Default Re: Immediate help is required on QGraphicsTextItem

    You have to set the width manually or come up with a heuristic to calculate it. Without the width the document doesn't know how much space it occupies so it is hard to layout it properly.

  7. #7
    Join Date
    Jun 2008
    Posts
    35
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Immediate help is required on QGraphicsTextItem

    This helped me. It was called initially:

    Qt Code:
    1. QTextDocument *text_document = text_item->document();
    2.  
    3. if (NULL != text_document)
    4. {
    5. text_document->setTextWidth(width);
    6.  
    7. QTextOption alignment_option(Qt::AlignHCenter);
    8.  
    9. text_document->setDefaultTextOption(alignment_option);
    10. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Rich text in QGraphicsTextItem
    By maverick_pol in forum Qt Programming
    Replies: 3
    Last Post: 29th September 2008, 20:08
  2. QGraphicsTextItem AlphaColor Background to CSS2
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 30th November 2007, 15:17
  3. Problem on set QGraphicsTextItem write protect.
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 22nd July 2007, 20:53
  4. QGraphicsTextItem: handling cursor position change
    By Angelo Moriconi in forum Qt Programming
    Replies: 2
    Last Post: 30th January 2007, 10:42
  5. QGraphicsTextItem size
    By Angelo Moriconi in forum Qt Programming
    Replies: 1
    Last Post: 26th January 2007, 08:34

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.