Results 1 to 3 of 3

Thread: QGraphicsTextItem Vertical text alignment

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

    Question QGraphicsTextItem Vertical text alignment

    My label creator application requires to align text(QGraphicsTextItem) horizontaly and vertically within a given boundingRect.
    So far I'm NOT able to come up with a way to make vertical alignment work for Qt::AlignTop, Qt::AlignBottom, and Qt::AlignVCenter.
    Horizontal alignment works ok.

    Any suggestion for making vertical alignment work in a QGraphicsTextItem?


    QRectF BaseTextItem::boundingRect() const
    {
    return QRectF(0,0,310,100);

    }
    void BaseTextItem::alignVerticalTest()
    {
    QTextDocument *text_document = this->document();
    QTextOption alignment_option(Qt::AlignBottom);
    text_document->setDefaultTextOption(alignment_option);
    this->setDocument(text_document);
    }

  2. #2
    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 Vertical text alignment

    Wouldn't it be easier if you use a normal item and do the painting yourself? then you could use QPainter::drawText().
    Qt Code:
    1. QPainter painter(this);
    2. painter.drawText(boundingRect(), Qt::AlignCenter, tr("Qt by\nNokia"));
    To copy to clipboard, switch view to plain text mode 

    If you have to use a text item I think (it's long time ago) you have to set the page size of the document to the bounding rect: QTextDocument::setPageSize().

  3. #3
    Join Date
    Oct 2010
    Posts
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsTextItem Vertical text alignment

    Sample easy and good work alignment in QGraphicsTextItem

    Qt Code:
    1. class DiagramTextItem : public QGraphicsTextItem
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. enum { Type = UserType + 3 };
    7. DiagramTextItem(QGraphicsItem *parent = 0, QGraphicsScene *scene = 0);
    8. void setBoundingRect( qreal x, qreal y, qreal w, qreal h);
    9. void setText( const QString &inText );
    10.  
    11. signals:
    12.  
    13. protected:
    14. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
    15. QRectF boundingRect() const;
    16.  
    17. private:
    18. QRectF myBoundRect;
    19. QTextOption textOp;
    20. QString text;
    21. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. DiagramTextItem::DiagramTextItem(QGraphicsItem *parent, QGraphicsScene *scene)
    2. : QGraphicsTextItem(parent, scene)
    3. {
    4. myBoundRect.setRect( 0, 0, 0, 0 );
    5.  
    6. textOp.setAlignment( Qt::AlignCenter );
    7. textOp.setWrapMode( QTextOption::WrapAtWordBoundaryOrAnywhere );
    8. }
    9.  
    10. void DiagramTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem
    11. *option, QWidget *widget)
    12. {
    13. //painter->drawRect( boundingRect() );
    14.  
    15. painter->drawText( boundingRect(),
    16. text,
    17. textOp);
    18.  
    19. QGraphicsTextItem::paint(painter, option, widget);
    20. }
    21.  
    22. QRectF DiagramTextItem::boundingRect() const
    23. {
    24. return myBoundRect;
    25. }
    26.  
    27. void DiagramTextItem::setBoundingRect( qreal x, qreal y, qreal w, qreal h)
    28. {
    29. myBoundRect.setRect( x, y, w, h );
    30. }
    31.  
    32. void DiagramTextItem::setText( const QString &inText )
    33. {
    34. text = inText;
    35. update();
    36. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Replies: 1
    Last Post: 21st November 2009, 20:38
  3. Replies: 2
    Last Post: 30th December 2008, 21:35
  4. Rich text in QGraphicsTextItem
    By maverick_pol in forum Qt Programming
    Replies: 3
    Last Post: 29th September 2008, 20:08
  5. Vertical text in a QTextDocument
    By Angelo Moriconi in forum Qt Programming
    Replies: 6
    Last Post: 7th February 2008, 05:30

Tags for this Thread

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.