Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to compare two QGraphicsItem objects?
Forum Updated to NodeBB v4.3 + New Features

How to compare two QGraphicsItem objects?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 3.2k Views 1 Watching
  • 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.
  • X Offline
    X Offline
    xtingray
    wrote on last edited by
    #1

    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! :)


    Qt Developer

    1 Reply Last reply
    0
    • R Offline
      R Offline
      rschaub
      wrote on last edited by
      #2

      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...

      1 Reply Last reply
      0
      • X Offline
        X Offline
        xtingray
        wrote on last edited by
        #3

        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


        Qt Developer

        1 Reply Last reply
        0
        • R Offline
          R Offline
          rschaub
          wrote on last edited by
          #4

          [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.

          1 Reply Last reply
          0
          • X Offline
            X Offline
            xtingray
            wrote on last edited by
            #5

            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! :)


            Qt Developer

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved