I am working on QTreeView to explore the hard drive partition.In my Qtreeview, on Double click event on its items of treeview single click event also generates.
connect(ui->treeview,SIGNAL(doubleclicked(QModelIndex)),this,SLOT(Ondoubleclicktree(QModelIndex)));
connect(ui->treeview,SIGNAL(clicked(QModelIndex)),this,SLOT(Onclickedtree(QModelIndex)));
I want only double click event. Please help me how to stop it for entering in single click event slot. Thanks.
Trying to put things that were already mentioned together: if you are not worried about a slight lag in reaction to a single click, you only need to set up a QTimer
in your class which you start on single click and stop if you receive a second click within a certain time window.
You then just connect the timeout of the timer to the slot that does what you want to do on a single click.
One way of setting this up (certainly not the only way and probably not the most elegant) 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With