简体   繁体   English

Qtreeview项目上的DoubleClick活动

[英]DoubleClick events on Qtreeview items

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. 我正在使用QTreeView来探索硬盘分区。在我的Qtreeview中,双击事件也会生成其treeview单击事件。

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. 尝试将已经提到的内容放在一起:如果您不担心单次点击会有轻微的延迟,您只需在班级中设置一个QTimer ,您可以单击启动,如果您收到第二个,则停止在特定时间窗口内单击。 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 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 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. 如果这有帮助,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM