Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved QMouseEvent on button click

    General and Desktop
    qmouseevent
    2
    15
    5595
    Loading More Posts
    • 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.
    • U
      uruloke last edited by

      Hello,
      I am fairly new to Qt and would like to know how I can use QMouseEvent? I have tried a lot of ways but I cannot get it to work.

      What I wanna do is when I click a button I want it to setText to the mouse X and Y coordinates.

      Any help appreciated

      mrjj 1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion @uruloke last edited by

        @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 1 Reply Last reply Reply Quote 1
        • U
          uruloke @mrjj last edited by

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

          mrjj 1 Reply Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion @uruloke last edited by mrjj

            @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 Reply Quote 0
            • U
              uruloke @mrjj last edited by uruloke

              @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.

              mrjj 1 Reply Last reply Reply Quote 0
              • mrjj
                mrjj Lifetime Qt Champion @uruloke last edited by mrjj

                @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 Reply Quote 0
                • U
                  uruloke @mrjj last edited by

                  @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?

                  mrjj 1 Reply Last reply Reply Quote 0
                  • mrjj
                    mrjj Lifetime Qt Champion @uruloke last edited by

                    @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 Reply Quote 0
                    • U
                      uruloke @mrjj last edited by

                      @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.

                      mrjj 1 Reply Last reply Reply Quote 0
                      • mrjj
                        mrjj Lifetime Qt Champion @uruloke last edited by mrjj

                        @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 Reply Quote 1
                        • U
                          uruloke @mrjj last edited by

                          @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()));

                          mrjj 1 Reply Last reply Reply Quote 0
                          • mrjj
                            mrjj Lifetime Qt Champion @uruloke last edited by

                            @uruloke
                            ok.super.

                            Can I ask what you are programming ?

                            U 1 Reply Last reply Reply Quote 0
                            • U
                              uruloke @mrjj last edited by

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

                              mrjj 1 Reply Last reply Reply Quote 0
                              • mrjj
                                mrjj Lifetime Qt Champion @uruloke last edited by

                                @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 Reply Quote 0
                                • U
                                  uruloke @mrjj last edited by

                                  @mrjj
                                  I will, thanks for everything :)

                                  1 Reply Last reply Reply Quote 0
                                  • First post
                                    Last post