Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Simple QQuickItem mouseMoveEvent not working



  • I'm working with QQuickItem and trying to re-implement mouse events. What I want to do is get events when mouse click and move which is mouseMoveEvent exactly do.
    I override mousePressEvent and mouseMoveEvent but only working is mouse pressed.

    Below is the minimal code.

    moveitem.h

    #ifndef MOVEITEM_H
    #define MOVEITEM_H
    
    #include <QQuickItem>
    
    class MoveItem : public QQuickItem
    {
        Q_OBJECT
    public:
        MoveItem(QQuickItem *paremt = nullptr);
    
    protected:
        virtual void mouseMoveEvent(QMouseEvent *event);
        virtual void mousePressEvent(QMouseEvent *event);
    
    signals:
    
    };
    
    #endif // MOVEITEM_H
    

    moveitem.cpp

    #include "moveitem.h"
    
    MoveItem::MoveItem(QQuickItem *parent)
        : QQuickItem(parent)
    {
        setFlag(QQuickItem::ItemHasContents);
        setAcceptedMouseButtons(Qt::LeftButton);
    }
    
    void MoveItem::mouseMoveEvent(QMouseEvent *event)
    {
        qDebug() << event;
        return QQuickItem::mouseMoveEvent(event);
    }
    
    void MoveItem::mousePressEvent(QMouseEvent *event)
    {
        qDebug() << event;
        return QQuickItem::mousePressEvent(event);
    }
    

    main.qml

    import QtQuick 2.12
    import QtQuick.Window 2.12
    
    import Hello 1.0
    
    Window {
      width: 640
      height: 480
      visible: true
      title: qsTr("Hello World")
    
      MoveItem {
        width: 100
        height: 100
      }
    }
    
    

    When build this and run, mouse press in the MoveItem prints mouse event. But press and move mouse, prints nothing.
    What is wrong with my codes?


Log in to reply