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

QMouseEvent on button click

Scheduled Pinned Locked Moved Solved General and Desktop
qmouseevent
15 Posts 2 Posters 8.6k Views 2 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.
  • mrjjM 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 Offline
    U Offline
    uruloke
    wrote on last edited by uruloke
    #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.

    mrjjM 1 Reply Last reply
    0
    • U 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.

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #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
      0
      • mrjjM 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 Offline
        U Offline
        uruloke
        wrote on 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?

        mrjjM 1 Reply Last reply
        0
        • U uruloke

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

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on 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
          0
          • mrjjM mrjj

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

            mrjjM 1 Reply Last reply
            0
            • U uruloke

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

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #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
              1
              • mrjjM 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 Offline
                U Offline
                uruloke
                wrote on 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()));

                mrjjM 1 Reply Last reply
                0
                • U uruloke

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

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #12

                  @uruloke
                  ok.super.

                  Can I ask what you are programming ?

                  U 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @uruloke
                    ok.super.

                    Can I ask what you are programming ?

                    U Offline
                    U Offline
                    uruloke
                    wrote on 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.

                    mrjjM 1 Reply Last reply
                    0
                    • U uruloke

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

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 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
                      0
                      • mrjjM mrjj

                        @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 last edited by
                        #15

                        @mrjj
                        I will, thanks for everything :)

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