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. QGraphicsScene itemAt - allow radius around point?
Forum Updated to NodeBB v4.3 + New Features

QGraphicsScene itemAt - allow radius around point?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 3.3k 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.
  • S Offline
    S Offline
    SKolaMunn
    wrote on last edited by
    #1

    I have polygon objects and I want to detect when the mouse is close to the boundary of the polygon. Currently I use mouseMoveEvent and when the pt is on the border or within 5 pixels inside the border, I determine the border was clicked. I'd like to also say the border has been clicked if within 5 pixels outside the border (where the polygon item's mouseMoveEvent is not triggered). Is this possible? One way I can think of is overriding the mouseMoveEvent in the view or scene and checking each pt within a 5 pixel radius of the mouse position for proximity to a polygon edge, but this seems dirty and inefficient. Is there a better way to do this?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris H
      wrote on last edited by
      #2

      Can you override the shape() function of the polygon object to return a slightly larger polygon?

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        How about instead of using itemAt, you simply use the [[doc:QGraphicsScene]] items() with a QRectF argument. The QRectF can be simply constructed to be a small box around the current mouse position.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SKolaMunn
          wrote on last edited by
          #4

          Thanks for the suggestions. I tried using QGraphicsScene::items(QRectF), it seems to work but its a little "messy", the size of my QRectF is in scene units, not each individual polygons' units, although I'm not sure whether that's a bad thing or not...

          As for overriding the shape function, this seems interesting and worth trying. I'm a little concerned as to when else is this function used? Will it be "dangerous" to change its functionality? Will this affect the way the polygon is drawn? I guess I should try it and see...

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Chris H
            wrote on last edited by
            #5

            I think that shape() is used only for hit detection and determining when an item needs to be redrawn. It will make your widget appear larger than it really is when the code goes to figure out if it's just been exposed and needs to be redrawn, but that's probably not that big a deal. I don't think it gets used for anything else, but Andre would know better than me!

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SKolaMunn
              wrote on last edited by
              #6

              I just tried using the shape function. It worked exactly as desired for boundary hit detection but it seriously messed up the repainting of my other polygons in the scene during mouse events! Putting a breakpoint in the function showed that it was being called all the time and I had to remove the breakpoint. Looks like the items function is working better, I just have to decide what values I'd like to use and do similar things in my scene's hover event to change the cursor when its near the boundary.

              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