What could be reasons for these leftovers on the scene?
-
Hello, I am working on an algorithm which moves rectangles between boxes. Therefore I am just using QGraphicsRectItems and whenever there is a rectangle moved somewhere else, I call setRect("moved Rectangle") for the specific QGraphicsRectItem so that this rectangle appears at another place. Sometimes there are moved more than one rectangle simultaneously and it seems that sometimes there are some leftovers of the rectangles before they have been moved. I am sorry that I can't post my code here but I am pretty sure that it's doing what it should do. The only thing are these leftovers. I posted two pictures of how that looks. I hope you can give me at least a hint what I could try.
-
@Sano said in Why could be reasons for these leftovers on the scene?:
whenever there is a rectangle moved somewhere else, I call setRect("moved Rectangle") for the specific QGraphicsRectItem so that this rectangle appears at another place
I think of
setRect()
as an initial call, to establish the rectangle's size. Try setPos() instead for moving anyQGraphicsItem
, does that help?Otherwise are you using the in-built
QGraphicsRectItem
or did you derive from that yourself? -
Using setPos() breaks my code(the rectangles are all over the place). It seems that setPos() isn't working as I assumed.
I have two central parts in my algorithm(the central part of the algorithm just works on QRect objects) where QRect is modified. It is always modified in the following ways:
Let "a" be a QRect.
a.moveTopLeft("some QPoint") or a.moveBottomRight("some QPoint")
Of course when the algorithm changes the QRect object than you also have to change the QGraphicsRectItem object. Until now I have done this with setRect() by just passing the changed QRect object. I am not sure how I can achieve the same with setPos()
If I change QRect "a" to QRect "b" by applying one of the two upper functions, how do I set the position for the QGRaphicsRectItem of "a" to get the "same" change for the scene object?
And I am using the in-built QGraphicsRectItem.
-
@Sano said in What could be reasons for these leftovers on the scene?:
I am not sure how I can achieve the same with setPos()
That only moves a rectangle. I didn't know you were changing sizes.
I just meant to try it to see if still get artefacts, I don't know whether you can test to see if that is (somehow) the issue.
-
Hi,
Aren't you moving the QGraphicsRectItem objects ?
-
I am moving both. The algorithm just moves the QRect objects.
I made a class called "Rectobject" which has a QRect member and an according QGraphicsRectItem member. The algorithm just processes the QRect object inside of this class and moves the QRect by using moveTopLeft() or moveBottomRight(). Whenever a "Rectobject" is processed a copy is sent to a queue which is processed by the scene in certain time intervals.
In each of these time steps the sceneObject gets a "Rectobject" and with that we have the "updated" QRect "a" and also the QGraphicsRectItem "c" so that we can just call c->setRect(a) and so we move the according "Scene-rect". I don't know if I should have used setPos() but it worked absolutely fine with setRect() besides these leftovers.
-
@Sano said in What could be reasons for these leftovers on the scene?:
All my QRects are of the form QRect(0,0,x,y).
If that is the case and you alter
x,y
(and not the0,0
), you are not moving your rectangles, you are resizing them..... Yourx,y
are actuallywidth,height
.Something is over-complex here.
setPos()
just sets the top-leftx,y
coordinates. There should no problem converting betweensetPos()
&setRect()
, providing you are changing x,y and not width,height.Whether any of this affects your "trails" I do not know. Usually that happens by drawing outside the
boundingRect()
, but you don't seem to be doing that. -
@JonB Sorry, I expressed the wrong thing. My QRect in the absolute beginning, when creating them, are all of the form QRect(0,0,x,y) and the only transformations done from there are moveTopLeft() and moveBottomRight().
Just to be sure: When lets say I have a QRect "a" and the according QGraphicsRectItem "b". Now I change "a" by doing
a.moveTopLeft(QPoint(50,50))
When I want to change the Sceneobject than it would suffice to write:
b->setPos(a.topLeft())
Am I right or is there something I don't understand.
-
Can you explain the data duplication ?
I fail to see why you have a structure with a QRect and a QGraphicsRectItem that you need to keep in sync at some while. QGraphicsRectItem already provides all the information you need. -
@SGaist The data duplication could absolutely be unnecessary. I just thought somehow that I need both because when I change the QGraphicsRectItem inside the algorithm there would be no visualization because the algorithm isn't attached to some kind of timer. The visualization happens when the algorithm is over. It could absolutely be that the structure of this program is bad because there are so many things I don't know about Qt. First and foremost I need to somehow resolve the problem with these leftovers.