Trying to put things that have already been mentioned together: if you are not worried about a slight lag in the reaction of one click, you only need to configure QTimer in your class, which you start with one click and stop if you get a second click within a certain time window. Then you simply connect the timer timeout to the slot, which does what you want to do in one click.
One way to customize this (of course, is not the only way and probably not the most elegant), which you see below:
mytreeview.h
#ifndef MYTREEVIEW_H #define MYTREEVIEW_H #include <QTreeView> #include <QTimer> class MyTreeView: public QTreeView { Q_OBJECT public: MyTreeView(QWidget *parent = 0); protected: virtual void mouseDoubleClickEvent(QMouseEvent * event); virtual void mousePressEvent(QMouseEvent * event); private: QTimer timer; private slots: void onSingleClick(); };
mytreeview.cpp
#include "mytreeview.h" #include <QtCore> MyTreeView::MyTreeView(QWidget *parent) : QTreeView(parent) { connect(&timer,SIGNAL(timeout()),this,SLOT(onSingleClick())); } void MyTreeView::mouseDoubleClickEvent(QMouseEvent * event) { Q_UNUSED(event); qDebug() << "This happens on double click"; timer.stop(); } void MyTreeView::mousePressEvent(QMouseEvent * event) { Q_UNUSED(event); timer.start(250); } void MyTreeView::onSingleClick() { qDebug() << "This happens on single click"; timer.stop(); }
Let me know if this helps.
source share