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. QMouseEvent on button click
Forum Update on Monday, May 27th 2025

QMouseEvent on button click

Scheduled Pinned Locked Moved Solved General and Desktop
qmouseevent
15 Posts 2 Posters 7.1k 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.
  • M mrjj
    8 Nov 2015, 11:46

    @uruloke
    ok
    1: place button on mainform
    2: right click button
    3: select Goto Slot
    4: Select "released" from the list
    Now you got the click handler

    void MainWindow::on_pushButton_released() {
      QPoint p = mapFromGlobal(QCursor::pos());
        ui->pushButton->setText( QString::number(p.x()) + "," + QString::number(p.x()));
    }
    
    U Offline
    U Offline
    uruloke
    wrote on 8 Nov 2015, 11:54 last edited by
    #3

    @mrjj Thank you @mrjj.
    It was this line here I had trouble with defining myself
    QPoint p = mapFromGlobal(QCursor::pos());

    M 1 Reply Last reply 8 Nov 2015, 11:57
    0
    • U uruloke
      8 Nov 2015, 11:54

      @mrjj Thank you @mrjj.
      It was this line here I had trouble with defining myself
      QPoint p = mapFromGlobal(QCursor::pos());

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 8 Nov 2015, 11:57 last edited by mrjj 11 Aug 2015, 11:58
      #4

      @uruloke
      well also I forgot to mention
      to use QMouseEvent
      you would override
      void mousePressEvent(QMouseEvent *eventPress);
      for the widget.
      like here
      http://doc.qt.io/qt-5/qtwidgets-widgets-scribble-example.html

      U 1 Reply Last reply 8 Nov 2015, 12:03
      0
      • M mrjj
        8 Nov 2015, 11:57

        @uruloke
        well also I forgot to mention
        to use QMouseEvent
        you would override
        void mousePressEvent(QMouseEvent *eventPress);
        for the widget.
        like here
        http://doc.qt.io/qt-5/qtwidgets-widgets-scribble-example.html

        U Offline
        U Offline
        uruloke
        wrote on 8 Nov 2015, 12:03 last edited by uruloke 11 Aug 2015, 12:03
        #5

        @mrjj
        Okay, so Qcursor works for me, but I cannot get QMouseEventto work correctly.

        void MainWindow::on_screen2Button_clicked(QMouseEvent *eventPress) { QPoint p = mapFromGlobal(eventPress->globalPos()); ui->pixMap->setText( QString::number(p.x()) + "," + QString::number(p.x())); }
        It doesn't give me any errors, but it doesn't print the text out either, it worked with Qcursor though

        EDIT 1:
        cool, will look into the example.

        M 1 Reply Last reply 8 Nov 2015, 12:07
        0
        • U uruloke
          8 Nov 2015, 12:03

          @mrjj
          Okay, so Qcursor works for me, but I cannot get QMouseEventto work correctly.

          void MainWindow::on_screen2Button_clicked(QMouseEvent *eventPress) { QPoint p = mapFromGlobal(eventPress->globalPos()); ui->pixMap->setText( QString::number(p.x()) + "," + QString::number(p.x())); }
          It doesn't give me any errors, but it doesn't print the text out either, it worked with Qcursor though

          EDIT 1:
          cool, will look into the example.

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 8 Nov 2015, 12:07 last edited by mrjj 11 Aug 2015, 12:08
          #6

          @uruloke
          you have to add

          protected:
          void mousePressEvent(QMouseEvent *eventPress);

          to mainwindow .H
          and
          void mousePressEvent(QMouseEvent *eventPress) {
          }
          in .cpp

          its a virtual function and must be named like this.
          It will then be called by system.
          What you seems to try is to put MouseEvent *eventPress to a button click.
          and it will be zero as it not filled out by system since a buttons click is not the same as mouse click.
          so you sort of give it a empty QMouseEvent.

          Note its protected: that is important.

          U 1 Reply Last reply 8 Nov 2015, 12:14
          0
          • M mrjj
            8 Nov 2015, 12:07

            @uruloke
            you have to add

            protected:
            void mousePressEvent(QMouseEvent *eventPress);

            to mainwindow .H
            and
            void mousePressEvent(QMouseEvent *eventPress) {
            }
            in .cpp

            its a virtual function and must be named like this.
            It will then be called by system.
            What you seems to try is to put MouseEvent *eventPress to a button click.
            and it will be zero as it not filled out by system since a buttons click is not the same as mouse click.
            so you sort of give it a empty QMouseEvent.

            Note its protected: that is important.

            U Offline
            U Offline
            uruloke
            wrote on 8 Nov 2015, 12:14 last edited by
            #7

            @mrjj
            Ah so if I want it to run when a mouse is pressed I just have to run the mousePressEvent(QMouseEvent *eventPress)inside my onButtonClicked event?

            M 1 Reply Last reply 8 Nov 2015, 12:16
            0
            • U uruloke
              8 Nov 2015, 12:14

              @mrjj
              Ah so if I want it to run when a mouse is pressed I just have to run the mousePressEvent(QMouseEvent *eventPress)inside my onButtonClicked event?

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 8 Nov 2015, 12:16 last edited by
              #8

              @uruloke
              well Im not sure I understand.
              Why dont you just run when button is clicked ?

              mousePressEvent is for click on some spot on mainwindow where no control.

              Dont call the mousePressEvent your self. system call it.

              U 1 Reply Last reply 8 Nov 2015, 12:23
              0
              • M mrjj
                8 Nov 2015, 12:16

                @uruloke
                well Im not sure I understand.
                Why dont you just run when button is clicked ?

                mousePressEvent is for click on some spot on mainwindow where no control.

                Dont call the mousePressEvent your self. system call it.

                U Offline
                U Offline
                uruloke
                wrote on 8 Nov 2015, 12:23 last edited by
                #9

                @mrjj
                Ah, yeah I forgot that it was an event :)

                void mousePressEvent(QMouseEvent *eventPress) { QPoint p = mapFromGlobal(eventPress->globalPos()); ui->pixMap->setText( QString::number(p.x()) + "," + QString::number(p.x())); }

                I get errors on mapFromGlobal now and ui, didn't get that before.

                M 1 Reply Last reply 8 Nov 2015, 12:26
                0
                • U uruloke
                  8 Nov 2015, 12:23

                  @mrjj
                  Ah, yeah I forgot that it was an event :)

                  void mousePressEvent(QMouseEvent *eventPress) { QPoint p = mapFromGlobal(eventPress->globalPos()); ui->pixMap->setText( QString::number(p.x()) + "," + QString::number(p.x())); }

                  I get errors on mapFromGlobal now and ui, didn't get that before.

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 8 Nov 2015, 12:26 last edited by mrjj 11 Aug 2015, 12:28
                  #10

                  @uruloke
                  try with

                  eventPress->pos().x();
                  eventPress->pos().y();

                  and no mapFromGlobal

                  oh and make sure its maiwindow::mousePressEvent(...) else it wont compile
                  if you are in the .cpp file.

                  U 1 Reply Last reply 8 Nov 2015, 12:31
                  1
                  • M mrjj
                    8 Nov 2015, 12:26

                    @uruloke
                    try with

                    eventPress->pos().x();
                    eventPress->pos().y();

                    and no mapFromGlobal

                    oh and make sure its maiwindow::mousePressEvent(...) else it wont compile
                    if you are in the .cpp file.

                    U Offline
                    U Offline
                    uruloke
                    wrote on 8 Nov 2015, 12:31 last edited by
                    #11

                    @mrjj
                    thanks a lot for the help.
                    It was just the mapFromGlobal that fucked it up.
                    It worked when I removed that.

                    QPoint p = eventPress->globalPos();
                    ui->pixMap->setText( QString::number(p.x()) + "," + QString::number(p.y()));

                    M 1 Reply Last reply 8 Nov 2015, 12:33
                    0
                    • U uruloke
                      8 Nov 2015, 12:31

                      @mrjj
                      thanks a lot for the help.
                      It was just the mapFromGlobal that fucked it up.
                      It worked when I removed that.

                      QPoint p = eventPress->globalPos();
                      ui->pixMap->setText( QString::number(p.x()) + "," + QString::number(p.y()));

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 8 Nov 2015, 12:33 last edited by
                      #12

                      @uruloke
                      ok.super.

                      Can I ask what you are programming ?

                      U 1 Reply Last reply 8 Nov 2015, 12:58
                      0
                      • M mrjj
                        8 Nov 2015, 12:33

                        @uruloke
                        ok.super.

                        Can I ask what you are programming ?

                        U Offline
                        U Offline
                        uruloke
                        wrote on 8 Nov 2015, 12:58 last edited by
                        #13

                        @mrjj Sure, I am making a program to take screenshots with. So I need the mouse global position to mark the area to screenshot.

                        M 1 Reply Last reply 8 Nov 2015, 13:00
                        0
                        • U uruloke
                          8 Nov 2015, 12:58

                          @mrjj Sure, I am making a program to take screenshots with. So I need the mouse global position to mark the area to screenshot.

                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 8 Nov 2015, 13:00 last edited by
                          #14

                          @uruloke
                          ahh. such beast. :)
                          make sure to borrow from
                          http://doc.qt.io/qt-5/qtwidgets-desktop-screenshot-example.html

                          U 1 Reply Last reply 8 Nov 2015, 13:11
                          0
                          • M mrjj
                            8 Nov 2015, 13:00

                            @uruloke
                            ahh. such beast. :)
                            make sure to borrow from
                            http://doc.qt.io/qt-5/qtwidgets-desktop-screenshot-example.html

                            U Offline
                            U Offline
                            uruloke
                            wrote on 8 Nov 2015, 13:11 last edited by
                            #15

                            @mrjj
                            I will, thanks for everything :)

                            1 Reply Last reply
                            0

                            12/15

                            8 Nov 2015, 12:33

                            • Login

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