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. Get mouse coordinates relative to QGraphicsScene
Forum Updated to NodeBB v4.3 + New Features

Get mouse coordinates relative to QGraphicsScene

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 9.0k 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.
  • A Offline
    A Offline
    alanbortu
    wrote on last edited by
    #1

    I am attempting to detect if the user clicks on an object that is within the QGraphicsScene by using the following code:
    @
    QPointF origin = ui->graphicsView->mapFromGlobal(QCursor::pos());
    qDebug() << scene->itemAt(origin, QTransform());@

    However, the problem is, itemAt() uses coordinates that are relative to the scene which considers (0,0) to be in the middle while mapFromGlobal() considers (0,0) to be at the top left of the scene. I attempted to use an int that was the width of the graphicsView/2 - the width of the object. I did the same for the height and this works until the user moves the object, at which point the trick no longer works. So I need some advice on how to translate the mouse coordinates to either be relative to Global or the QGraphicsScene.

    1 Reply Last reply
    1
    • A Offline
      A Offline
      alanbortu
      wrote on last edited by
      #2

      Alright I think I have fixed this, with some help on IRC and documentation.

      @
      QPoint origin = ui->graphicsView->mapFromGlobal(QCursor::pos());
      QPointF relativeOrigin = ui->graphicsView->mapToScene(origin);
      scene->removeItem(scene->itemAt(relativeOrigin, QTransform()));
      @

      1 Reply Last reply
      3

      • Login

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