Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Centering an Item in a QGraphicsView

    Mobile and Embedded
    1
    2
    2085
    Loading More Posts
    • 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
      lambda last edited by

      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 Reply Quote 0
      • L
        lambda last edited by

        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 Reply Quote 0
        • First post
          Last post