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. QWidget::mouseMoveEvent() with and without button pressed
QtWS25 Last Chance

QWidget::mouseMoveEvent() with and without button pressed

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 7.8k 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.
  • I Offline
    I Offline
    isaacEnrique
    wrote on last edited by
    #1

    Greetings.

    I'm reimplementing the QWidget::mouseMoveEvent() function for a custom widget. I need that the function can distinguish when the mouse move event is performed without any button pressed and when the mouse movement is performed keeping the left mouse button held down.

    The problem arises because the function QMouseEvent::button() returns Qt::Nobutton for mouse motion events. What can I do in this case?.

    Thanks in advance for any help and/or suggestions.

    Isaac Pérez
    Programming is understanding.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      enable setMouseTracking on widget. You will receive move events without without any buttons pressed i.e QMouseEvent::button() returns nobutton.

      If the button is pressed and moved, QMouseEvent::button() will give you appropriate buttons inside moveevent.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • I Offline
        I Offline
        isaacEnrique
        wrote on last edited by
        #3

        Greetings.

        Probe what you said, but did not work.

        I have within my function MyWidget::mouseMoveEvent() something like as follows:

        @

        if( event->button() == Qt::LeftButton )
        {
        /* Shares corresponding to: mouse has moved and the left button pressed /
        }
        else
        {
        /
        otherwise */
        }

        @
        Am I doing something wrong?.

        According to what I read in the documentation of Qt, the function QMouseEvent::button() always returns Qt::NoButton for mouse motion events (I understand then, which returns Qt::NoButton regardless of whether or not a button down ... or am I wrong?).

        Before starting this post, I had solved the problem by using a logical variable (member of my class), which was initiated in false, is set to true when entering mousePressEvent() and returned to false mouseReleaseEvent(). This worked well, but I guess it must be a way (the right way) more elegant using Qt classes (QMouseEvent here and maybe some other classes)... What is that other way?

        Isaac Pérez
        Programming is understanding.

        T 1 Reply Last reply
        0
        • I isaacEnrique

          Greetings.

          Probe what you said, but did not work.

          I have within my function MyWidget::mouseMoveEvent() something like as follows:

          @

          if( event->button() == Qt::LeftButton )
          {
          /* Shares corresponding to: mouse has moved and the left button pressed /
          }
          else
          {
          /
          otherwise */
          }

          @
          Am I doing something wrong?.

          According to what I read in the documentation of Qt, the function QMouseEvent::button() always returns Qt::NoButton for mouse motion events (I understand then, which returns Qt::NoButton regardless of whether or not a button down ... or am I wrong?).

          Before starting this post, I had solved the problem by using a logical variable (member of my class), which was initiated in false, is set to true when entering mousePressEvent() and returned to false mouseReleaseEvent(). This worked well, but I guess it must be a way (the right way) more elegant using Qt classes (QMouseEvent here and maybe some other classes)... What is that other way?

          T Offline
          T Offline
          texas
          wrote on last edited by
          #4

          @isaacEnrique Use event->buttons() instead of event->button().

          Pl45m4P 1 Reply Last reply
          0
          • T texas

            @isaacEnrique Use event->buttons() instead of event->button().

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by
            #5

            @texas

            Really? :)
            After 7 years?


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            T 1 Reply Last reply
            1
            • Pl45m4P Pl45m4

              @texas

              Really? :)
              After 7 years?

              T Offline
              T Offline
              texas
              wrote on last edited by
              #6

              @Pl45m4 Better late than never? lol

              Pl45m4P 1 Reply Last reply
              0
              • T texas

                @Pl45m4 Better late than never? lol

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by Pl45m4
                #7

                @texas

                Yeah, I believe the OP is still sitting in front of his computer looking for a solution :)


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                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