How to compare two QGraphicsItem objects?
-
Hi,
Currently I'm facing this challenge:
From two different sources (data structures) I'm getting two QGrahpicsItem objects and I need to find out whether they are copies of the same shape in my canvas or not. In other words, are these two variables pointing to the same object? How to know it?
Thanks for any hint! :)
-
couldn't you just compare the memory address of the object the variables are pointing to? If it's the same object, they should be identical.
If however you have different instances of the object but want to compare whether or not they describe the same shape, you'd problably have to implement a comparison operator for your shape objects...
-
I tried this:
@
if (item1 == item2)
@And never is true, even when I already know that both variables are pointing at the same object.
Then I tried this:
@
if (item1->shape() == item2->shape())
@With no luck. I guess I will have to create my own comparison operator... oh hell! :/
Any other approach? :P
-
[quote author="xtingray" date="1337862908"]I tried this:
@
if (item1 == item2)
@And never is true, even when I already know that both variables are pointing at the same object.
[/quote]Have you tried inspecting the variable values?
Add a breakpoint to the line and look at their content. If they don't contain the same addresses, then they don't point to the same object. Before starting to implement a complicated operator, I'd investigate why the pointer comparison fails. Maybe there's something else wrong with the code and you're actually creating two different objects that just look exactly alike. -