QQuickItem can't catch MouseButtonRelease in C++



  • Hi,
    i did a few program to show you the program. I try to catch PressEvent and ReleaseEvent but it doesn't work with simple Click. It only work on DoubleClick but all buttons except the Left One (sic)

    Here is the QML test

    import QtQuick 2.5
    import QtQuick.Window 2.0
    import QtQuick.Controls 1.4
    import GestureArea 1.0
    
    
    ApplicationWindow {
        visible: true
        
        GestureArea {
            anchors.fill: parent
            z: 1000
            opacity: 0.2
            focus: true
            onClicked: console.log('Click');
            onReleased: console.log('Released')
      /*      
            MouseArea {
                anchors.fill: parent
                propagateComposedEvents: true
                onPressed: console.log('GestureArea Click');
                onReleased:console.log('gestureArea Released');
            }
       */     
            
        }
    }
    

    Here is the GestureArea.h file

    #ifndef GESTUREAREA_H
    #define GESTUREAREA_H
    
    #include <QtQuick>
    
    class GestureArea : public QQuickItem
    {
        Q_OBJECT
    public:
        GestureArea();
        void PressEvent(QMouseEvent *event);
        void ReleaseEvent(QMouseEvent *event);
        bool event(QEvent *e);
    signals:
        void clicked(QMouseEvent *event);
        void released(QMouseEvent *event);
    };
    
    #endif // GESTUREAREA_H
    
    

    And the cpp file GestureArea.cpp

    #include "gesturearea.h"
    #include <QtQuick>
    #include <QDebug>
    
    GestureArea::GestureArea()
    {
        qDebug()<<"GestureArea::GestureArea()";
        setAcceptHoverEvents(true);
        setAcceptedMouseButtons(Qt::AllButtons);
        setFlag(ItemAcceptsInputMethod, true);
        setFlag(QQuickItem::ItemHasContents);
    }
    
    bool GestureArea::event(QEvent *e)
    {
        qDebug()<<(int)e->type();
        if (e->type()==QEvent::MouseButtonRelease) ReleaseEvent((QMouseEvent*)e);
        if (e->type()==QEvent::MouseButtonPress) PressEvent((QMouseEvent*)e);
        QQuickItem::event(e);
        return true;
    }
    
    void GestureArea::PressEvent(QMouseEvent *event)
    {
        qDebug()<<"void GestureArea::PressEvent(QMouseEvent *event)";
        emit clicked(event);
    }
    
    void GestureArea::ReleaseEvent(QMouseEvent *event)
    {
        qDebug()<<"void GestureArea::ReleaseEvent(QMouseEvent *event)";
        emit released(event);
    }
    

    I decided to overload event function to see the type of event i could catch.
    On the QML file when i put a MouseArea it can catch Press/Relesease

    But my goal is to catch these events on the C++ code.

    Sincerely



  • In order to receive a mouse release event, an item must accept the preceding press event.



  • @jpnurmi said:

    In order to receive a mouse release event, an item must accept the preceding press event.

    Ok thanks, but in this case there is a problem because what i want to do with this code is to be able to catch all events below this Item.
    Why do i want this ? to make a Gesture Capture because i don't think this exist on Qt. I want to have the "force" of the movement....But it doesn't exist on QtQuick.
    What i want is a Component in C++ that is above all others and can catch all events. A bit like a filterEvent but without knowing what is above my GestureArea

    Sincerely


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.