MousePressEvent called 3 times when double-clicking
-
I have created a class, GraphicPoint, that inherits both QObject and QGraphicsEllipseItem.
.h file
@#include <QtWidgets>
class GraphicPoint : public QObject, public QGraphicsEllipseItem
{
Q_OBJECTpublic:
GraphicPoint(qreal x, qreal y, qreal width, qreal height, QWidget *parent = nullptr);signals:
void clicked();private:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
};@.cpp file
@#include "graphicpoint.h"
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 .cpp file
@#include "mainwindow.h"
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 <<endl;
});
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?
Additional info:
The main.cpp file is as follows:
@#include "mainwindow.h"
#include <QApplication>int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();return a.exec();
}@and the mainwindow.h:
@#include <QtWidgets>
#include "graphicpoint.h"class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
MainWindow(QWidget *parent = 0);private:
QGraphicsView *view;
QGraphicsScene *scene;
GraphicPoint *testPoint;
qint32 n;
};@