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. Mapping mouse coordinates relative to a QGraphicsItem
QtWS25 Last Chance

Mapping mouse coordinates relative to a QGraphicsItem

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 414 Views
  • 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.
  • F Offline
    F Offline
    Forfunckle
    wrote on last edited by
    #1

    On mouse press, my custom QGraphicsView calls the mouseInside(mousePosition) function of my QGraphicsItem. The mouseInside function looks like this:

    bool MyItem::mouseInside(QPoint mousePosition)
    {
        return this->rect().contains(mousePosition);
    }
    

    It is supposed to answer the question "Is the mouse inside the item?" except it doesn't work. The position of the item is (0, 0) in scene coordinates but the mousePosition QPoint is relative to the scene's top left corner. This means that if the item is in the middle of my scene and I click in the top left corner of the scene (where the item is NOT) it will say that the item was clicked. How can I map the item's position relative to the top left corner of the scene so that the function works?

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Forfunckle
      wrote on last edited by
      #2

      I found the problem. I should have used QGraphicsScene::mousePressedEvent instead of QGraphicsView::mousePressedEvent.

      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