Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QQuickPaintedItem::mouseReleaseEvent() only called after double click

QQuickPaintedItem::mouseReleaseEvent() only called after double click

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qquickpaintedit
10 Posts 6 Posters 5.7k 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.
  • S Offline
    S Offline
    swegmann
    wrote on 10 Feb 2015, 09:38 last edited by A Former User
    #1

    In a class I derived from QQuickPaintedItem I try to capture a mouse release event. As found in many places I first make sure I get the event at all:

    @
    // MyItem.h
    class MyItem : public QQuickPaintedItem
    {
    public:
    MyItem(QQuickItem *parent = 0);

    protected:
    virtual void mouseReleaseEvent(QMouseEvent *event) override;
    };

    // MyItem.cpp
    MyItem::MyItem(QQuickItem *parent = 0) :
    QQuickPaintedItem(parent)
    {
    // this call is crucial to even get any clicks at all
    setAcceptedMouseButton(Qt::AllButtons);
    }
    @

    Now I would expect that I can simply implement the mouseReleaseEvent() method and get called every time I release the mouse button.

    @
    void MyItem::mouseReleaseEvent(QMouseEvent *event)
    {
    qDebug() << "Mouse x: " << event->x();
    // I even tried with and without either of the following two lines without
    // change in the result
    //QQuickPaintedItem::mouseReleaseEvent(event);
    //event->accept();
    }
    @

    However it turns out that I only get called after a double click but never after a single click.

    When I rename mouseReleaseEvent() to mousePressEvent() things work as expected. But I would prefer to handle the release case to be more consistent with the other Qt and QML components.

    Do you have any idea where this goes wrong?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Andi73
      wrote on 22 Apr 2015, 06:22 last edited by
      #2

      Hi, I have the same problem. Were you able to solve it?

      P 1 Reply Last reply 22 Apr 2015, 07:47
      0
      • A Andi73
        22 Apr 2015, 06:22

        Hi, I have the same problem. Were you able to solve it?

        P Offline
        P Offline
        p3c0
        Moderators
        wrote on 22 Apr 2015, 07:47 last edited by
        #3

        @Andi73 Try re-implementing mousePressEvent too.

        157

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Andi73
          wrote on 22 Apr 2015, 08:03 last edited by
          #4

          I implemented also mousePressEvent.
          mousePressEvent gets called on every single mouse down but mouseReleaseEvent only after a double click. Furthermore mouseMoveEvent also gets only called after a double click and hold.

          P 1 Reply Last reply 22 Apr 2015, 08:40
          0
          • A Andi73
            22 Apr 2015, 08:03

            I implemented also mousePressEvent.
            mousePressEvent gets called on every single mouse down but mouseReleaseEvent only after a double click. Furthermore mouseMoveEvent also gets only called after a double click and hold.

            P Offline
            P Offline
            p3c0
            Moderators
            wrote on 22 Apr 2015, 08:40 last edited by
            #5

            @Andi73 Works if you don't call base class mousePressEvent in mousePressEvent

            void MyItem::mousePressEvent(QMouseEvent *e) {
                qDebug() << "Pressed";
            }
            
            void MyItem::mouseReleaseEvent(QMouseEvent *e) {
                qDebug() << "Released";
                QQuickPaintedItem::mouseReleaseEvent(e);
            }
            

            157

            A Q 2 Replies Last reply 22 Apr 2015, 09:05
            1
            • P p3c0
              22 Apr 2015, 08:40

              @Andi73 Works if you don't call base class mousePressEvent in mousePressEvent

              void MyItem::mousePressEvent(QMouseEvent *e) {
                  qDebug() << "Pressed";
              }
              
              void MyItem::mouseReleaseEvent(QMouseEvent *e) {
                  qDebug() << "Released";
                  QQuickPaintedItem::mouseReleaseEvent(e);
              }
              
              A Offline
              A Offline
              Andi73
              wrote on 22 Apr 2015, 09:05 last edited by
              #6

              @p3c0 Thanks a lot! That works. I've looked up the code in the qt sources (QWindow.cpp) and found the following:

              void QWindow::mousePressEvent(QMouseEvent *ev)
              {
                  ev->ignore();
              }
              
              void QWindow::mouseReleaseEvent(QMouseEvent *ev)
              {
                  ev->ignore();
              }
              

              That explains everything... So I guess it's a bad idea calling the base class here.
              Thanks for your help!

              1 Reply Last reply
              0
              • S Offline
                S Offline
                swegmann
                wrote on 24 Apr 2015, 09:09 last edited by
                #7

                Indeed @p3c0's solution did the trick. Implement mousePressEvent() empty and don't call the base class. Thanks a lot!

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  Devinder
                  wrote on 2 Nov 2018, 16:40 last edited by
                  #8

                  @swegmann said in QQuickPaintedItem::mouseReleaseEvent() only called after double click:

                  Indeed @p3c0's solution did the trick. Implement mousePressEvent() empty and don't call the base class.

                  Why does this work? @p3c0 . I m not sure I understand the relevance of calling the base class's mouse event. Why do it at all? Why does removing it solve the problem? I m trying to better understand how mouse events work. Would LOVE an explanation.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 2 Nov 2018, 17:01 last edited by
                    #9

                    Hi,

                    Usually you call the base class implementation of a method to continue the normal flow of execution but do something specific to your class in addition. You don't call the base class implementation when you want to handle things completely in the subclass. This isn't specific to the mouse events.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • P p3c0
                      22 Apr 2015, 08:40

                      @Andi73 Works if you don't call base class mousePressEvent in mousePressEvent

                      void MyItem::mousePressEvent(QMouseEvent *e) {
                          qDebug() << "Pressed";
                      }
                      
                      void MyItem::mouseReleaseEvent(QMouseEvent *e) {
                          qDebug() << "Released";
                          QQuickPaintedItem::mouseReleaseEvent(e);
                      }
                      
                      Q Offline
                      Q Offline
                      QtTester
                      wrote on 13 Nov 2024, 03:25 last edited by
                      #10

                      @p3c0 nice job!

                      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