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.



  • You are right. Due I was serializing the objects in some point of the process, even when the variables contain the same information, for the program they seem to be two different objects.

    I will have to refactor my code to deal with this. Thank you! :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.