Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to get coordinates of mouse click on the widget?



  • Hello,
    I have an object called "widget". It's class "cellularwidget" is a child of "QWidget". I need to get coordinates of mouse click in this widget when I click on it. How can I do that?

    Thank you!



  • You should read a couple of tutorials or something. It's too easy and direct answers would spoil your experience.



  • I read help for class "QWidget" but I could not find any function for that.



  • Here is an "example":http://doc.qt.nokia.com/4.7/widgets-scribble.html using mousepressevent.



  • Kxyu: I've also been looking at the documentation for mousePressEvent in the QWidget documentation, but it doesn't mention anything about mouse coorinates. Just saying that "it’s too easy" doesn't really help, it just declares the other person an idiot.

    Eddy: Thanks for the link, but it seems broken. Do you think you could update it or post a new one?

    Edit: Found the answer:

    @
    // Extract the coordinates from the QMouseEvent object
    int x = event->x();
    int y = event->y();
    @


Log in to reply