DoubleClick Events in Qtreeview Points

I am working on a QTreeView to examine a hard disk partition. In my Qtreeview on a Double-click event, on its elements of the one-day event, a treeview is also generated.

connect(ui->treeview,SIGNAL(doubleclicked(QModelIndex)),this,SLOT(Ondoubleclicktree(QModelIndex))); connect(ui->treeview,SIGNAL(clicked(QModelIndex)),this,SLOT(Onclickedtree(QModelIndex))); 

I want only a double click event. Please help me how to stop it to enter the event slot with one click. Thanks.

+6
source share
1 answer

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.

+1
source

Source: https://habr.com/ru/post/957971/


All Articles