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. [Solved]X-y coordinates of a button press
QtWS25 Last Chance

[Solved]X-y coordinates of a button press

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 6.2k 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.
  • S Offline
    S Offline
    sharkattack28
    wrote on 21 Nov 2013, 19:45 last edited by
    #1

    Is there anyway that I can tell where the position of the mouse is on a button when it is clicked?
    I would like to read the x y position of the mouse the moment the button is clicked.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 21 Nov 2013, 19:57 last edited by
      #2

      Hi

      You can subclass the button base class, overload mousePressEvent and get the position from the give QMouseEvent. Or install an event filter on the button and check for a QEvent::MouseButtonPress on it.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sharkattack28
        wrote on 21 Nov 2013, 20:02 last edited by
        #3

        How would I implement QMouseEvent::x(); with a button click?

        1 Reply Last reply
        0
        • P Offline
          P Offline
          poorBob
          wrote on 21 Nov 2013, 20:21 last edited by
          #4

          Maybe this might be usefull: "const QPointF & QMouseEvent::localPos() const":http://qt-project.org/doc/qt-5.0/qtgui/qmouseevent.html#localPos
          ?

          with best regards,
          Robert

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sharkattack28
            wrote on 21 Nov 2013, 22:14 last edited by
            #5

            this is what I have.

            void MainWindow::on_pushButton_clicked()
            {
            QMouseEvent *mouseEvent;
            qDebug() << QString::number(mouseEvent->pos().x());
            qDebug() << QString::number(mouseEvent->pos().y());
            }

            and all I get is "0" on the output

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 21 Nov 2013, 22:53 last edited by
              #6

              That won't work, you're not inside an event handler or a more specialized mouse event handler.

              Technically this code will also crash.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • P Offline
                P Offline
                poorBob
                wrote on 22 Nov 2013, 11:09 last edited by
                #7

                As SGaist wrote "here":http://qt-project.org/forums/viewthread/35153/#151578 You have 2 options:

                1. Derive Your button from QPushButton class and reimplement there mousePressEvent(QMouseEvent * e) "doc":http://qt-project.org/doc/qt-5.0/qtwidgets/qabstractbutton.html#mousePressEvent
                2. Implement and set event filter on Your button "Event filter":http://qt-project.org/doc/qt-4.8/eventsandfilters.html#event-filters

                When You receive interesting event (here: QMouseEvent) You will be able to get exact position of mouse press.

                If You still need some help us know :)

                with best regards,
                Robert

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  sharkattack28
                  wrote on 25 Nov 2013, 12:59 last edited by
                  #8

                  Thanks poorBob,
                  Im not going to lie, I am learning as I go and I don't even know where to begin here. I read up on the links, and I am still a little lost.

                  is this the right function call?
                  void QAbstractButton::mousePressEvent(QMouseEvent *e){}:
                  and then what do I do with "e"? what syntax gets me the pos?

                  Sorry but Im new to all this GUI Stuff lol!

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    NicuPopescu
                    wrote on 25 Nov 2013, 13:55 last edited by
                    #9

                    @void MainWindow::on_pushButton_clicked()
                    {
                    qDebug()<<ui->pushButton->cursor().pos();//screen coordinates
                    qDebug()<<ui->pushButton->mapFromGlobal(ui->pushButton->cursor().pos());//local coordinates

                    }@

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      sharkattack28
                      wrote on 25 Nov 2013, 14:01 last edited by
                      #10

                      Thank you Nicu!! Works perfectly!!

                      1 Reply Last reply
                      0

                      7/10

                      22 Nov 2013, 11:09

                      • Login

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