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. QGraphicsView, QGraphicsPath - find closest path ?

QGraphicsView, QGraphicsPath - find closest path ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qgraphicsviewqgraphicspath
8 Posts 2 Posters 745 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.
  • D Offline
    D Offline
    Dariusz
    wrote on last edited by
    #1

    Hey

    I have multiple QGraphicsPath in my view/scene. How can I detect which path is closest to my clicked point?

    Regards
    Dariusz

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      The easy solution is to use the items bounding box and
      loop over the items and check if mouse click was close to one of them.
      If the paths can be overlapped, its much more complicated.

      D 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        The easy solution is to use the items bounding box and
        loop over the items and check if mouse click was close to one of them.
        If the paths can be overlapped, its much more complicated.

        D Offline
        D Offline
        Dariusz
        wrote on last edited by
        #3

        @mrjj Problem with that is that it only cares about item pos() and not individual point on the curve... This is somewhat difficult task isnt it ?

        mrjjM D 2 Replies Last reply
        0
        • D Dariusz

          @mrjj Problem with that is that it only cares about item pos() and not individual point on the curve... This is somewhat difficult task isnt it ?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Dariusz
          Well its a more involved task as we want a better hit testing.
          However, try with
          bool QPainterPath::contains(const QPointF &point)
          and see if that takes into account any "holes".
          and/or work for your use case.

          1 Reply Last reply
          0
          • D Dariusz

            @mrjj Problem with that is that it only cares about item pos() and not individual point on the curve... This is somewhat difficult task isnt it ?

            D Offline
            D Offline
            Dariusz
            wrote on last edited by
            #5

            @Dariusz Hmm will give it a go, but that would return either 1 or 1+ paths that get hit, how would I know which path is closest to the hit ?

            mrjjM 1 Reply Last reply
            0
            • D Dariusz

              @Dariusz Hmm will give it a go, but that would return either 1 or 1+ paths that get hit, how would I know which path is closest to the hit ?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Dariusz
              you loop over the items and ask contain(clickpos)
              and if yes, thats the one?
              Or am i missing something?

              D 1 Reply Last reply
              0
              • mrjjM mrjj

                @Dariusz
                you loop over the items and ask contain(clickpos)
                and if yes, thats the one?
                Or am i missing something?

                D Offline
                D Offline
                Dariusz
                wrote on last edited by
                #7

                @mrjj said in QGraphicsView, QGraphicsPath - find closest path ?:

                @Dariusz
                you loop over the items and ask contain(clickpos)
                and if yes, thats the one?
                Or am i missing something?

                What happens if 3+ items return true. How would I know which one is closer ?

                D 1 Reply Last reply
                0
                • D Dariusz

                  @mrjj said in QGraphicsView, QGraphicsPath - find closest path ?:

                  @Dariusz
                  you loop over the items and ask contain(clickpos)
                  and if yes, thats the one?
                  Or am i missing something?

                  What happens if 3+ items return true. How would I know which one is closer ?

                  D Offline
                  D Offline
                  Dariusz
                  wrote on last edited by Dariusz
                  #8

                  Also another problem with path.contains that I find is that the contains would use this as check area >
                  https://doc.qt.io/archives/qt-4.8/images/graphicsview-pathitem.png
                  Which means that the shape can be bigger/inaccurate comparing to the selection location... I think I will need to do some math and create another method for path click intersection.

                  Ok I sorta managed to "fix" it... I ended up creating a forward and then reverse path during selection process. So that the path is a closed - precise - shape. This way I can get proper click detection, I still cant compute which path is closer, but at least selection works properly now.

                  1 Reply Last reply
                  1

                  • Login

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