Handling mouse events on QQuickItem

I created a basic QML application that uses QQuickView to create a view and has custom QQuickItems. I want to handle mouse events on one of these QQuickItem, overriding the mousepressevent (QEvent *) method. However, when I launch the application and click on QQuickItem, the call to the mousepressevent (QEvent *) method fails .

The QQuickItem header file looks like this:

#include <QQuickItem> #include <QSGGeometry> #include <QSGFlatColorMaterial> class TriangularGeometry: public QQuickItem { Q_OBJECT public: TriangularGeometry(QQuickItem* parent = 0); QSGNode* updatePaintNode(QSGNode*, UpdatePaintNodeData*); void mousePressEvent(QMouseEvent *event); private: QSGGeometry m_geometry; QSGFlatColorMaterial m_material; QColor m_color; }; 

Note. I am using a script to render QuickItem.

This is a fragment of a cpp file:

 void TriangularGeometry::mousePressEvent(QMouseEvent *event) { m_color = Qt::black; update(); //changing an attribute of the qquickitem and updating the scenegraph } 

I can handle mouse events from the application, but according to my requirements, I need to handle it by overriding the mousePressEvent method (QMouseEvent * event).

+4
source share
1 answer

Make sure you call this method before handling the events (constructor is a good place):

setAcceptedMouseButtons(Qt::AllButtons);

The list of buttons, of course, can be anything you want.

+9
source

Source: https://habr.com/ru/post/1502722/


All Articles