Nominate our 2022 Qt Champions!

How to get the coordinates of the rect item under mouse position

  • hi,i have 15 rect items in my scene how can i find the rectitem coordinates under my mouse position on the scene...some one please help me

  • You really need to start writing more detailed opening posts. You will need to appreciate that the people reading your message know nothing of your project, your goals, or what classes or techniques you try to use.

    Please make it easy to reply to your questions by spending a bit more time in formulating them. Make sure they contain all the information somebody would need to give a sensible answer.

    Now, I will assume that you are using the graphics view framework, and your "scene" is a QGraphicsScene, and your "rect item" a "QGraphicsRectItem". If that is true, you should really look at the different QGraphicsView::items() methods that are available. They will return the information you need.

  • sorry , your assumption is correct but "QGraphicsView::items" will give all rectitems .. but i want only the co ordinates of rect item under the cursor position...

  • Please read the documentation for QGraphicsView::items(). There are overloads that do not return all items, but only a subset of them. I'll leave it to you to figure out which one to use. Once you have the item(s) under the cursor, it is easy to retreive the coordinates for those items under it.

  • As Andre sayed you should read the documentation.
    You can use this one "items()":

Log in to reply