Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get the mouse click coordinate in a scene?
Qt 6.12 LTS is out! See what's new in the release blog

How to get the mouse click coordinate in a scene?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 3.5k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hi..
    I make some circle in one scene using QGraphicsEllipseItem. I want to grab a real coordinate (not the center of the circle) of this item when someone left-clicked every circle. Using the first code it can only detect the coordinate when I clicked outside of the circle but when I clicked inside of every circle, the coordinate not shown.

    @
    void DialogApplication::mousePressEvent(QMouseEvent *event)
    {
    if (event->button()==Qt::LeftButton){
    mouse_koordinat=event->pos();
    qDebug() << "coordinate in my item: " << mouse_koordinat.x();
    }
    }@

    Using this second code there is an error message: "overloaded member function not found in 'DialogApplication' "

    @void DialogApplication::mousePressEvent(QGraphicsSceneMouseEvent *event) //QMouseEvent *event) //
    {
    if (event->button()==Qt::LeftButton){

        mouse_koordinat=event->buttonDownScenePos(Qt::LeftButton);
    
        qDebug() << "coordinate in my item: " << mouse_koordinat.x();
    }
    

    }
    @

    I appreciate if there is a suggestion and help. Thank you...

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi,

      is DialogApplication inherited from QGraphicsView?

      The second error means you MUST define this method in class Declaration

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • podsvirovP Offline
        podsvirovP Offline
        podsvirov
        wrote on last edited by
        #3

        Try this:
        @
        #include <QGraphicsEllipseItem>
        #include <QDebug>
        class Ellipse: public QGraphicsEllipseItem
        {
        public:
        Ellipse(qreal x, qreal y, qreal width, qreal height, QGraphicsItem *parent = 0) :
        QGraphicsEllipseItem(x, y, width, height, parent) {}
        protected:
        void mousePressEvent(QGraphicsSceneMouseEvent *event)
        {
        if (event->button() & Qt::LeftButton)
        {
        QPointF center = rect().center();
        qDebug() << "Center:" << center;
        }
        }
        };
        @

        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
        • Unsolved