Results 1 to 6 of 6

Thread: Center QGraphicsTextItem at position

  1. #1
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1

    Default Center QGraphicsTextItem at position

    Hi all,

    I would like to center a QGraphicsTextItem at a certain position in the scene. The Item is set to QGraphicsItem::ItemIgnoresTransformations and is thus not affected by zoom. If I try doing sth like "item.setPos(posx - item.boundingRect().width()/2)" it does not work, because boundingRect seems to refer to the rect at zoom 1.0 and at higher/lower zoomlevels this moves the item off center.
    Furthermore at very high zoomlevels when maybe only two pixels of the scene are visible, I would like the item to be centered at the border between those two pixels and not starting at the border and extending only into the pixel to the right.

    Is this positioning possible?

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Center QGraphicsTextItem at position

    I would like to center a QGraphicsTextItem at a certain position in the scene.
    Do you mean, that you want a text fixed at the centre of the scene or at the centre of the view (motinor screen)?
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1

    Default Re: Center QGraphicsTextItem at position

    I want the center of the text-item to be at some position in the scene.
    item.setPos(100,100) would place the text's upper left corner at position 100,100 but I want the center of the text to be at that position.
    And that is made more difficult by the zooming problems I describe above.

  4. #4
    Join Date
    Jan 2012
    Location
    Dortmund, Germany
    Posts
    159
    Thanks
    69
    Thanked 10 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: Center QGraphicsTextItem at position

    In my MainWindow I use this:

    Qt Code:
    1. QRect rect=this->widget->view->viewport()->rect();
    2. this->scene->setSceneRect(QRectF(0,0,rect.width(),rect.height()));
    To copy to clipboard, switch view to plain text mode 
    Perhaps you should also look into the ->mapTo... and ->mapFrom... methods.

    To know when the scene's size is being changed by the user I subclass the CentralWidget and plug myself into the resizeEvent, triggering a signal from there:

    Qt Code:
    1. CentralWidget::CentralWidget(QWidget *parent) :
    2. QWidget(parent)
    3. {
    4. }
    5.  
    6. void CentralWidget::resizeEvent(QResizeEvent *)
    7. {
    8. emit this->centralWidgetWasResized();
    9. }
    10. bool CentralWidget::event(QEvent * event)
    11. {
    12. if (event->type()==QEvent::Show) emit this->centralWidgetWasResized();
    13. event->ignore();
    14. return QWidget::event(event);
    15. }
    To copy to clipboard, switch view to plain text mode 

    Actually I am not quite sure if this tackles your problem in the right way, but it might give you some ideas.

  5. #5
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1

    Default Re: Center QGraphicsTextItem at position

    I'm sorry but I do not see how this would help me.

  6. #6
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1

    Default Re: Center QGraphicsTextItem at position

    I found a not-so-clean way to do it. If in a view I correct the position of the items every time the zoom/scale changes, it works like this:

    item->setPos(xpos - item->boundingRect().width()/2.0/transform().m11(), ypos);

    But this will cause problems if two views show the same item but have different values for transform().m11() (i.e. zoom/scaling).
    A better way would be to somehow have the items "position anchor" (I don't know how positioning is done internally) to be in its center as opposed to the upper left corner.

    Any further help/comments are welcome.

Similar Threads

  1. QGraphicsTextItem and text cursor position via QPoint
    By Lykurg in forum Qt Programming
    Replies: 2
    Last Post: 10th July 2017, 18:38
  2. QGraphicsTextItem center based coordinates
    By mihnen in forum Qt Programming
    Replies: 3
    Last Post: 19th September 2012, 16:48
  3. QGraphicsTextItem position
    By harmodrew in forum Qt Programming
    Replies: 1
    Last Post: 2nd September 2010, 21:03
  4. QGraphicsTextItem not remembering it's position
    By grabalon in forum Qt Programming
    Replies: 2
    Last Post: 6th July 2010, 15:55
  5. Set EllipseItem center to position
    By maverick_pol in forum Qt Programming
    Replies: 5
    Last Post: 15th May 2008, 13:18

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.