Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Centering an Item in a QGraphicsView
Forum Updated to NodeBB v4.3 + New Features

Centering an Item in a QGraphicsView

Scheduled Pinned Locked Moved Mobile and Embedded
2 Posts 1 Posters 2.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lambda
    wrote on last edited by
    #1

    The following code doesn't center the item, selected from the QGraphicsScene items list, in the view as expected:

    @
    class GraphicsMapView : public QGraphicsView
    {
    Q_OBJECT

    public:
    GraphicsMapView(QGraphicsView *parent = 0);
    ~GraphicsMapView();
    void updateView(QPointF& pos);

    private:
    QGraphicsScene scene;

    };

    void GraphicsMapView::updateView(QPointF& pos)
    {
    QGraphicsEllipseItem* item1 = dynamic_cast<QGraphicsEllipseItem*>(scene.items()[1]);
    item1->setPos(pos);

    QRectF sceneRect(pos.x(),
                     pos.y(),
                     width(),
                     height());
    
    setSceneRect(sceneRect);
    

    }@

    The item is visible in the view but not centered and I can't achieve to center it in the view.

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lambda
      wrote on last edited by
      #2

      I've finally found a solution which seems to work fine, if this can help somebody:

      @
      item1->setPos(pos);
      setSceneRect(item1->sceneBoundingRect());
      @

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved