QGraphicsItem returns the wrong position in the scene. (Qt4.7.3)

I have a QGraphicsRectItem element in a QGraphicsScene. The element is movable and has no parent . I put the position of the position of the position from the file and calling the constructor:

item = new QGraphicsRectItem (rect); 

which is working. The positions are as intended.

Then I try to save the position back to the file, getting it from the element using

 item->pos().toPoint() 

The position is wrong - not an absolute position in the scene. Position refers to the last position in which the item was created.

Is the pos () method correct for retrieving a position position within a scene?

Thanks for any tips!

PS: scenePos () returns the same values

+3
source share
3 answers

I had problems with QGraphicsRectItem. My current hypothesis is this:

The "rect" you set / get refers to the "pos" element, so you can use one or the other (or both if you want to get confused) to control the location where the geometry is actually running.

My solution was as follows:

 // Given QRectF scene_rect that represents the rectangle I want drawn... setPos( scene_rect.topLeft() ); // Set the item "position" relative to the scene scene_rect.moveTo( 0.0, 0.0 ); // ...then set the rectangle location to (0,0) setRect( scene_rect ); // ...so the rectangle is drawn at (0,0) relative to the item 
+5
source

I found your question because I searched the Internet for tips that I could use.

I took a look at the sources of Qt 4.7.4:

  • QGraphicsRectItemPrivate has a QRectF rect member QRectF rect , which can be obtained using rect () and changed using setRect ().
  • But since it is derived from QGraphicsItem , it also has QRectF pos .
  • The boundingRect() function returns a rect adjusted by the width of the pen, which means that the area to be updated is generally correct.

So, as Aldo said, you need to decide whether you use rect.topLeft or pos for positioning.
To help you and others decide, here are a few pro and opposing points:

Benefits of using rect:

  • You can determine the position and size of the QGraphicsRectItem in a single function call.
  • In the case of QGraphicsLineItem , the pos () function always points to the top corner of the bounding edge rectangle, which does not have to be one of the points on the line, therefore, leaving pos in QPointF (0,0), the QLine points always refer to the scene.

The disadvantage of using rect:

  • Since you cannot use setPos (), there is no function that emits QGraphicsItem::ItemPositionChange . For me, this moment weighs more than advantages if you do not use QGraphicsLineItem (if I need QGraphicsLineItem, I would probably extract it from it and send QGraphicsItem :: ItemPositionChange myself).

How and when to use both:

If you use both pos and rect, let pos become the center of your GraphicsItem.
Imagine a marker, cursor or similar with a certain radius in your graphic scene. If you want to use QGraphicsRectItem for this, I suggest:

 QPointF markerpos = GetMarkerPos(); double r = GetMarkerRadius(); QRectF markerrect(-r, -r, +r * 2., +r * 2.); // topleft: -radius, size: 2*radius setPos(markerpos); setRect(markerrect); 

Thus, pos always points to the center, and the line is determined only by the radius.

+3
source

Now I have found a workaround. Do not tie if this is a Qt error or my misunderstanding on the stage / view.

Now I create the elements at position 0,0 and move them to the desired position using moveBy (x, y).

 item = new QGraphicsRectItem( QRect(QPoint(x,y),QSize(w,h)) ); QPoint p = item->pos().toPoint(); //WRONG position! Relative to x,y item = new QGraphicsRectItem( QRect(QPoint(0,0),QSize(w,h)) ); item->moveBy(x,y); QPoint p = item->pos().toPoint(); //Right position! Relative to 0,0 of scene 

Strange, somehow.

+1
source

Source: https://habr.com/ru/post/1339614/


All Articles