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 - is it press or release or drag?
Forum Updated to NodeBB v4.3 + New Features

QMouseEvent - is it press or release or drag?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 843 Views 1 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.
  • Tom assoT Offline
    Tom assoT Offline
    Tom asso
    wrote on last edited by Tom asso
    #1

    Is it possible to tell if a QMouseEvent itself was generated by press, release, or drag? QMouseEvent constructors include a QEvent::Type argument, but how can I retrieve that type info after the QMouseEvent is constructed? Thanks!

    Pl45m4P jsulmJ 2 Replies Last reply
    0
    • Tom assoT Tom asso

      @Pl45m4 - I'm creating several apps that display a QPixmap within a QLabel; users interact with that QPixmap with the mouse. I've designed ClickableLabel which can be used by these apps; this widget emits a signal with the QMouseEvent as payload, then various apps can processes the event in a the app-specific manner (this 'business logic' isn't necessarily contained in a Qt widget). Does this seem reasonable? Thanks!

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

      @Tom-asso said in QMouseEvent - is it press or release or drag?:

      I've designed ClickableLabel which can be used by these apps; this widget emits a signal with the QMouseEvent as payload, then various apps can processes the event in a the app-specific manner (this 'business logic' isn't necessarily contained in a Qt widget).

      The ClickableLabel has the event handler mousePressEvent. So why do you want to transfer the QMouseEvent within the signal?
      You can/should get the information you need (like mouse position) in the handler function and then emit a signal like void imageClicked(QPoint pos), where you send the event->pos() (or event->position() if Qt6+) from the handler to the receiving widget.

      As I've mentioned before, it's unusal to send a QEvent via signal-slots.
      (If I got it right what you are doing there)

      Edit:
      The example is already prepared for this:

      void ClickableLabel::mousePressEvent(QMouseEvent* event) {
          // add this
         QPointF pos = event->position();
          // some further processing if needed
          // ...
      
      
          emit clicked(pos); // change signal to "void clicked(QPointF)"
      }
      

      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
      2
      • Tom assoT Tom asso

        Is it possible to tell if a QMouseEvent itself was generated by press, release, or drag? QMouseEvent constructors include a QEvent::Type argument, but how can I retrieve that type info after the QMouseEvent is constructed? Thanks!

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

        @Tom-asso

        If you receive the event, you can filter/check it.
        For example with an eventFilter, which you install on some widget.
        QWidget has separate event handlers like QWidget::mousePressEvent(QMouseEvent*ev) (same for release and move) or you can compare the transported QEvent ev anytime to

        • QEvent::MouseButtonDblClick Mouse press again (QMouseEvent).
        • QEvent::MouseButtonPress Mouse press (QMouseEvent).
        • QEvent::MouseButtonRelease Mouse release (QMouseEvent).
        • QEvent::MouseMove Mouse move (QMouseEvent).

        (https://doc.qt.io/qt-6/qevent.html#Type-enum)

        Whether you use an eventFilter or better re-implement the event handlers depends on your use case.


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

        ~E. W. Dijkstra

        Tom assoT 1 Reply Last reply
        1
        • Pl45m4P Pl45m4

          @Tom-asso

          If you receive the event, you can filter/check it.
          For example with an eventFilter, which you install on some widget.
          QWidget has separate event handlers like QWidget::mousePressEvent(QMouseEvent*ev) (same for release and move) or you can compare the transported QEvent ev anytime to

          • QEvent::MouseButtonDblClick Mouse press again (QMouseEvent).
          • QEvent::MouseButtonPress Mouse press (QMouseEvent).
          • QEvent::MouseButtonRelease Mouse release (QMouseEvent).
          • QEvent::MouseMove Mouse move (QMouseEvent).

          (https://doc.qt.io/qt-6/qevent.html#Type-enum)

          Whether you use an eventFilter or better re-implement the event handlers depends on your use case.

          Tom assoT Offline
          Tom assoT Offline
          Tom asso
          wrote on last edited by Tom asso
          #3

          @Pl45m4 - Thanks!
          I've designed a signal that emits just the QMouseEvent. EDIT: So the slot can just call event->type() function and filter on that.

          Pl45m4P 1 Reply Last reply
          0
          • Tom assoT Tom asso

            @Pl45m4 - Thanks!
            I've designed a signal that emits just the QMouseEvent. EDIT: So the slot can just call event->type() function and filter on that.

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

            @Tom-asso said in QMouseEvent - is it press or release or drag?:

            I've designed a signal that emits just the QMouseEvent.

            Then design again please :)
            No, really. The Qt Event system and the Signal/Slot mechanism are two different things and I can't imagine any case where you need to send an event by emitting a signal. Makes no sense.

            (This would also make the event kinda useless, since it's sent to the receiving widget directly and is not processed by Qt's event handlers and passed up/down the object tree).

            If you have a custom event type (rarely an input event like QMouseEvent), you can create an event yourself and post/send it with

            QApplication::sendEvent() or QApplication::postEvent()

            • https://doc.qt.io/qt-6/qcoreapplication.html#sendEvent
            • https://doc.qt.io/qt-6/qcoreapplication.html#postEvent

            The sendEvent documentation even includes an example where a mouse click is simulated by constructing a mouse press event and sent to an certain widget.
            But usually you don't need to do that
            (maybe in test cases, but the QTest framework has its own functions to simulate mouse clicks or button presses)

            QMouseEvent event(QEvent::MouseButtonPress, pos, 0, 0, 0);
            QApplication::sendEvent(mainWindow, &event);
            

            Edit:

            EDIT: So the slot can just call event->type() function and filter on that.

            If this is your use case, you can simply re-implement the handlers where your slot is. Then you don't even need to filter, since there is:

            QWidget::mouseReleaseEvent(QMouseEvent *event)

            • https://doc.qt.io/qt-6/qwidget.html#mouseReleaseEvent

            QWidget::mousePressEvent(QMouseEvent *event)

            • https://doc.qt.io/qt-6/qwidget.html#mousePressEvent

            and
            QWidget::mouseMoveEvent(QMouseEvent *event)

            • https://doc.qt.io/qt-6/qwidget.html#mouseMoveEvent

            while you still have access to the event inside the handler function to get the position where the event occured, for example.


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

            ~E. W. Dijkstra

            Tom assoT 1 Reply Last reply
            2
            • Tom assoT Tom asso

              Is it possible to tell if a QMouseEvent itself was generated by press, release, or drag? QMouseEvent constructors include a QEvent::Type argument, but how can I retrieve that type info after the QMouseEvent is constructed? Thanks!

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @Tom-asso said in QMouseEvent - is it press or release or drag?:

              but how can I retrieve that type info after the QMouseEvent is constructed?

              By calling https://doc.qt.io/qt-6/qevent.html#type

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              Tom assoT 1 Reply Last reply
              0
              • Pl45m4P Pl45m4

                @Tom-asso said in QMouseEvent - is it press or release or drag?:

                I've designed a signal that emits just the QMouseEvent.

                Then design again please :)
                No, really. The Qt Event system and the Signal/Slot mechanism are two different things and I can't imagine any case where you need to send an event by emitting a signal. Makes no sense.

                (This would also make the event kinda useless, since it's sent to the receiving widget directly and is not processed by Qt's event handlers and passed up/down the object tree).

                If you have a custom event type (rarely an input event like QMouseEvent), you can create an event yourself and post/send it with

                QApplication::sendEvent() or QApplication::postEvent()

                • https://doc.qt.io/qt-6/qcoreapplication.html#sendEvent
                • https://doc.qt.io/qt-6/qcoreapplication.html#postEvent

                The sendEvent documentation even includes an example where a mouse click is simulated by constructing a mouse press event and sent to an certain widget.
                But usually you don't need to do that
                (maybe in test cases, but the QTest framework has its own functions to simulate mouse clicks or button presses)

                QMouseEvent event(QEvent::MouseButtonPress, pos, 0, 0, 0);
                QApplication::sendEvent(mainWindow, &event);
                

                Edit:

                EDIT: So the slot can just call event->type() function and filter on that.

                If this is your use case, you can simply re-implement the handlers where your slot is. Then you don't even need to filter, since there is:

                QWidget::mouseReleaseEvent(QMouseEvent *event)

                • https://doc.qt.io/qt-6/qwidget.html#mouseReleaseEvent

                QWidget::mousePressEvent(QMouseEvent *event)

                • https://doc.qt.io/qt-6/qwidget.html#mousePressEvent

                and
                QWidget::mouseMoveEvent(QMouseEvent *event)

                • https://doc.qt.io/qt-6/qwidget.html#mouseMoveEvent

                while you still have access to the event inside the handler function to get the position where the event occured, for example.

                Tom assoT Offline
                Tom assoT Offline
                Tom asso
                wrote on last edited by Tom asso
                #6

                @Pl45m4 - I'm creating several apps that display a QPixmap within a QLabel; users interact with that QPixmap with the mouse. I've designed ClickableLabel which can be used by these apps; this widget emits a signal with the QMouseEvent as payload, then various apps can processes the event in a the app-specific manner (this 'business logic' isn't necessarily contained in a Qt widget). Does this seem reasonable? Thanks!

                Pl45m4P 1 Reply Last reply
                0
                • jsulmJ jsulm

                  @Tom-asso said in QMouseEvent - is it press or release or drag?:

                  but how can I retrieve that type info after the QMouseEvent is constructed?

                  By calling https://doc.qt.io/qt-6/qevent.html#type

                  Tom assoT Offline
                  Tom assoT Offline
                  Tom asso
                  wrote on last edited by
                  #7

                  @jsulm - Oy, I misread the QMouseEvent document, missed the fact that it includes type() member! Thanks!

                  1 Reply Last reply
                  0
                  • Tom assoT Tom asso

                    @Pl45m4 - I'm creating several apps that display a QPixmap within a QLabel; users interact with that QPixmap with the mouse. I've designed ClickableLabel which can be used by these apps; this widget emits a signal with the QMouseEvent as payload, then various apps can processes the event in a the app-specific manner (this 'business logic' isn't necessarily contained in a Qt widget). Does this seem reasonable? Thanks!

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

                    @Tom-asso said in QMouseEvent - is it press or release or drag?:

                    I've designed ClickableLabel which can be used by these apps; this widget emits a signal with the QMouseEvent as payload, then various apps can processes the event in a the app-specific manner (this 'business logic' isn't necessarily contained in a Qt widget).

                    The ClickableLabel has the event handler mousePressEvent. So why do you want to transfer the QMouseEvent within the signal?
                    You can/should get the information you need (like mouse position) in the handler function and then emit a signal like void imageClicked(QPoint pos), where you send the event->pos() (or event->position() if Qt6+) from the handler to the receiving widget.

                    As I've mentioned before, it's unusal to send a QEvent via signal-slots.
                    (If I got it right what you are doing there)

                    Edit:
                    The example is already prepared for this:

                    void ClickableLabel::mousePressEvent(QMouseEvent* event) {
                        // add this
                       QPointF pos = event->position();
                        // some further processing if needed
                        // ...
                    
                    
                        emit clicked(pos); // change signal to "void clicked(QPointF)"
                    }
                    

                    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
                    2
                    • Tom assoT Tom asso has marked this topic as solved on

                    • Login

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