I have created a class, GraphicPoint, that inherits both QObject and QGraphicsEllipseItem.
.h file
class GraphicPoint : public QObject, public QGraphicsEllipseItem
{
Q_OBJECT
public:
GraphicPoint(qreal x, qreal y, qreal width, qreal height, QWidget *parent = nullptr);
signals:
void clicked();
private:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
};
.cpp file
GraphicPoint::GraphicPoint(qreal x, qreal y, qreal width, qreal height, QWidget *parent) :
QObject(parent),
QGraphicsEllipseItem(x, y, width, height, nullptr)
{
}
void GraphicPoint::mousePressEvent(QGraphicsSceneMouseEvent *event){
event->accept();
emit clicked();
}
In the main window, I have created a view and a scene and added the point to the scene. Quickly clicking the button twice calls the mousePressEvent 3 times, whereas doing the same a bit slower calls it just 2 times. Here is the main window constructor
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
n = 0;
view = new QGraphicsView(this);
scene = new QGraphicsScene(this);
view->setScene(scene);
testPoint = new GraphicPoint(0, 0, 10, 10, this);
connect(testPoint, &GraphicPoint::clicked, [this](){
n++;
qDebug()<<n;
});
scene->addItem(testPoint);
setCentralWidget(view);
}
After 2 quick clicks, QDebug outputs 3 lines with the numbers 1, 2, 3.
What can be the reason of this happening? Is there some default doubleclick event that calls the additional mousePressEvent?