Results 1 to 1 of 1

Thread: QGraphicsItem : keep position when zoom changes

  1. #1
    Join Date
    Jul 2016
    Posts
    19
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default QGraphicsItem : keep position when zoom changes

    Hello,

    In the code below, I create a QGraphicsRectItem and I want to display its name with a QGraphicsSimpleTextItem. I want the size of the text to be unaffected by the zoom, so I use the flag QGraphicsItem::ItemIgnoresTransformations. I also want the position of the text to be centered on the QGraphicsRectItem.
    Unfortunately you can see on the capture that when I unzoom ( on the right ), the text doesn't stay inside the rectangle Capture.jpg .
    How to keep the text inside the rectangle and centered ?


    Qt Code:
    1. #include <QApplication>
    2. #include <QGraphicsScene>
    3. #include <QGraphicsView>
    4. #include <QGraphicsRectItem>
    5. #include <QPen>
    6. #include <QWheelEvent>
    7. #include <cmath>
    8. #include <QDebug>
    9.  
    10. class MainView : public QGraphicsView {
    11. public:
    12. MainView(QGraphicsScene *scene) : QGraphicsView(scene) { setBackgroundBrush(QBrush(QColor(255, 255, 255)));}
    13. protected:
    14. void wheelEvent(QWheelEvent *event) {
    15. double scaleFactor = pow(2.0, event->delta() / 240.0);
    16. scale(scaleFactor, scaleFactor);
    17. }
    18. };
    19.  
    20. int main(int argc, char *argv[])
    21. {
    22. QApplication a(argc, argv);
    23. scene.setSceneRect(0, 0, 800, 800);
    24. QGraphicsRectItem* rectItem = new QGraphicsRectItem(QRectF(0, 0, 400, 200));
    25. rectItem->setPos(200, 200);
    26. rectItem->setBrush(QColor(255, 0, 0));
    27. scene.addItem(rectItem);
    28.  
    29. QGraphicsSimpleTextItem *nameItem = new QGraphicsSimpleTextItem("name", rectItem);
    30. QFont f = nameItem->font();
    31. f.setPointSize(12);
    32. nameItem->setFont(f);
    33. nameItem->setFlag(QGraphicsItem::ItemIgnoresTransformations);
    34. nameItem->setPos(rectItem->rect().center());
    35.  
    36. MainView view(&scene);
    37. view.show();
    38.  
    39. return a.exec();
    40. }
    To copy to clipboard, switch view to plain text mode 

    Thank you
    Last edited by nilot; 9th February 2018 at 12:46.

Similar Threads

  1. Replies: 0
    Last Post: 5th August 2014, 10:26
  2. QGraphicsItem maintain constant size on zoom of parent
    By Casper14 in forum Qt Programming
    Replies: 0
    Last Post: 11th June 2014, 16:05
  3. Zoom on QGraphicsItem question (smooth not pixalted)
    By wally in forum Qt Programming
    Replies: 1
    Last Post: 3rd November 2011, 18:50
  4. QGraphicsItem disappear when zoom is too large.
    By Zikoel in forum Qt Programming
    Replies: 6
    Last Post: 30th July 2011, 12:14
  5. QGraphicsItem position
    By zgulser in forum Qt Programming
    Replies: 6
    Last Post: 5th February 2009, 09:33

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.