qt5.7 区分鼠标单击双击事件

不解释了,渣渣写的代码很简答,大佬请指教

源文件

#include "widget.h"
#include "ui_widget.h"
int flag = 0;
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    timer= new QTimer(this);
    timer->setSingleShot(true);
    connect(timer,SIGNAL(timeout()),this,SLOT(click_choice()));
}
void Widget::click_choice()
{
    switch (flag) {
    case 1:
        click_single();
        flag =0;
        break;
    case 2:
        click_double();
        flag =0;
        break;
    default:
        break;
    }
}
 
void Widget::click_single()
{
    qDebug()<<"single click"< 
  
}
void Widget::click_double()
{
    qDebug()<<"double click"< 
  
}
 
Widget::~Widget()
{
    delete timer;//   这里不知道要delete不,如果有懂的大佬指点一下,谢谢 
    delete ui;
}
 
void Widget::on_pushButton_clicked()
{
    //qDebug()<<"this clicke"< 
  
    if(!timer->isActive())
   {
        timer->start(200);//
    }
        flag++;
    qDebug()<remainingTime()< 
  
}

 

源文件

#ifndef WIDGET_H
#define WIDGET_H
#include
#include
#include 
 
namespace Ui {
class Widget;
}
 
class Widget : public QWidget
{
    Q_OBJECT
 
public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
 
private slots:
    void on_pushButton_clicked();
    void click_single();
    void click_double();
    void click_choice();
private:
    Ui::Widget *ui;
    QTimer *timer;
};
 
#endif // WIDGET_H
 

你可能感兴趣的:(qt)