Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. MousePressEvent called 3 times when double-clicking

MousePressEvent called 3 times when double-clicking

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 584 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Iridium Oxide
    wrote on last edited by
    #1

    I have created a class, GraphicPoint, that inherits both QObject and QGraphicsEllipseItem.

    .h file

    @#include <QtWidgets>

    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

    @#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_OBJECT

    public:
    MainWindow(QWidget *parent = 0);

    private:
    QGraphicsView *view;
    QGraphicsScene *scene;
    GraphicPoint *testPoint;
    qint32 n;
    };@

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved