Results 1 to 4 of 4

Thread: QGraphicsTextItem center based coordinates

  1. #1
    Join Date
    Aug 2012
    Posts
    13
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default QGraphicsTextItem center based coordinates

    I'm trying to center a QGraphicsTextItem subclass inside of a parent rectangle however setPos() on this uses the top left corner for its origin. Is there some way to make a QGraphicsTextItem work on a center based coordinate system? I have searched google for hours and can't find any solutions. Any ideas would be appreciated.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QGraphicsTextItem center based coordinates

    It's just geometry.

    Qt Code:
    1. // AlignedTextItem.h
    2. #include <QGraphicsSimpleTextItem>
    3.  
    4. class AlignedTextItem
    5. {
    6. public:
    7. AlignedTextItem( const QString & text, Qt::Alignment flags, QGraphicsItem * pParent = 0 );
    8. AlignedTextItem( const QString & text, QGraphicsItem * pParent = 0 );
    9. AlignedTextItem( QGraphicsItem * pParent = 0 );
    10.  
    11. ~AlignedTextItem();
    12.  
    13. QRectF boundingRect() const;
    14. void paint( QPainter * pPainter, const QStyleOptionGraphicsItem * pOption, QWidget * pWidget = 0 );
    15.  
    16. Qt::Alignment alignment() const;
    17. void setAlignment( Qt::Alignment flags );
    18.  
    19. private:
    20. Qt::Alignment mFlags;
    21. mutable QRectF mBounds;
    22. };
    23.  
    24.  
    25. // AlignedTextItem.cpp
    26. #include "AlignedTextItem.h"
    27. #include <QPainter>
    28.  
    29. AlignedTextItem::AlignedTextItem( const QString & text /*= QString()*/, Qt::Alignment flags /*= Qt::AlignTop | Qt::AlignLeft*/, QGraphicsItem * pParent /*= 0 */ )
    30. : QGraphicsSimpleTextItem( text, pParent )
    31. , mFlags( flags )
    32. {
    33. setFlag( ItemSendsGeometryChanges );
    34. mBounds = QGraphicsSimpleTextItem::boundingRect();
    35. }
    36.  
    37. AlignedTextItem::AlignedTextItem( const QString & text /*= QString()*/, QGraphicsItem * pParent /*= 0 */ )
    38. : QGraphicsSimpleTextItem( text, pParent )
    39. , mFlags( Qt::AlignTop | Qt::AlignLeft )
    40. {
    41. setFlag( ItemSendsGeometryChanges );
    42. mBounds = QGraphicsSimpleTextItem::boundingRect();
    43. }
    44.  
    45. AlignedTextItem::AlignedTextItem( QGraphicsItem * pParent /*= 0 */ )
    46. , mFlags( Qt::AlignTop | Qt::AlignLeft )
    47. {
    48. setFlag( ItemSendsGeometryChanges );
    49. mBounds = QGraphicsSimpleTextItem::boundingRect();
    50. }
    51.  
    52. AlignedTextItem::~AlignedTextItem()
    53. {
    54. }
    55.  
    56. Qt::Alignment AlignedTextItem::alignment() const
    57. {
    58. return mFlags;
    59. }
    60.  
    61. void AlignedTextItem::setAlignment( Qt::Alignment flags )
    62. {
    63. mFlags = flags;
    64. }
    65.  
    66. QRectF AlignedTextItem::boundingRect() const
    67. {
    68. mBounds = QGraphicsSimpleTextItem::boundingRect();
    69. QPointF offset( 0.0, 0.0 );
    70. if ( mFlags.testFlag( Qt::AlignRight ) )
    71. offset.setX( -mBounds.width() );
    72. else if ( mFlags.testFlag( Qt::AlignHCenter ) )
    73. offset.setX( -mBounds.width() / 2.0 );
    74.  
    75. if ( mFlags.testFlag( Qt::AlignBottom ) )
    76. offset.setY( mBounds.height() );
    77. else if ( mFlags.testFlag( Qt::AlignVCenter ) )
    78. offset.setY( mBounds.height() / 2.0 );
    79.  
    80. mBounds.translate( offset );
    81. return mBounds;
    82. }
    83.  
    84. void AlignedTextItem::paint( QPainter * pPainter, const QStyleOptionGraphicsItem * pOption, QWidget * pWidget /*= 0 */ )
    85. {
    86. pPainter->translate( mBounds.left(), -mBounds.top() );
    87. QGraphicsSimpleTextItem::paint( pPainter, pOption, pWidget );
    88. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by d_stranz; 18th September 2012 at 02:42.

  3. #3
    Join Date
    Aug 2012
    Posts
    13
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QGraphicsTextItem center based coordinates

    Thanks for the reply, I like the idea of being able to change the origin point for the coordinates however it doesn't seem to work. I am using a QGraphicsTextItem which is editable and not a QGraphicsSimpleTextItem which you can only call setText() on. When I run the code with only change being to make it subclass from QGraphicsTextItem I get something looking like this:

    Screenshot from 2012-09-18 13:15:58.png

    It seems as though whatever the base class call to paint() and boundingRect() is doing doesn't work with this?

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QGraphicsTextItem center based coordinates

    Did you replace QGraphicsSimpleTextItem with QGraphicsTextItem in this code to create that screenshot, or is this what your own code is doing? Are you sure you have the signatures for all of the overridden methods correct (boundingRect() in particular)?

    You might have to fire up the debugger and set a breakpoint on the paint() method to see exactly what happens in the base class.

    Anyway, it looks to me as though the text in your screenshot has the correct horizontal centering, it is the vertical centering that is off. It is possible that the boundRect used by QGraphicsTextItem is computed differently from QGraphicsSimpleTextItem, and you may need to make some adjustments. QGraphicsTextItem uses a QTextDocument internally, and this document may have page formatting (like margins or indenting, for example) that are adding extra space that needs to be taken into account.

Similar Threads

  1. Replies: 51
    Last Post: 26th November 2010, 13:24
  2. Replies: 0
    Last Post: 31st July 2010, 17:27
  3. Center based image zooming
    By plagoon in forum Qt Programming
    Replies: 2
    Last Post: 25th April 2010, 16:09
  4. Replies: 4
    Last Post: 18th March 2010, 10:11
  5. Hello Qt Center
    By Achraf-BH in forum General Discussion
    Replies: 0
    Last Post: 24th November 2009, 09:28

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.