Qt鼠标双击事件mouseDoubleClickEvent(QMouseEvent* event)

重写mouseDoubleClickEvent(QMouseEvent event)*

1、实例:双击窗口时输出“sss”


/*widget.h*/
#ifndef WIDGET_H
#define WIDGET_H

#include
#include

class Widget : public QWidget
{
    Q_OBJECT
public:
    Widget(QWidget* parent=0);
    ~Widget(){}
protected:
    //声明
    void mouseDoubleClickEvent(QMouseEvent*);
};


#endif
/*widget.cpp*/
#include"widget.h"
#include<QMouseEvent>

Widget::Widget(QWidget* parent) : QWidget(parent)
{
}

//重写
void Widget::mouseDoubleClickEvent(QMouseEvent* event)
{
    qDebug("sss");
}

/*main.cpp*/

#include"widget.h"
#include

int main(int argc,char* argv[])
{
    QApplication app(argc,argv);
    Widget    w;
    w.show();

    return app.exec();
}

Qt鼠标双击事件mouseDoubleClickEvent(QMouseEvent* event)_第1张图片

我在Widget里放置了ListWidget后,在Widget类中重写了mouseDoubleClickEvent,但是不知道为何触发不了。之后改在ListWidget类中重写mouseDoubleClickEvent,这样是可以触发鼠标双击事件的。

2、左键双击,右键双击

   上面默认是不分鼠标左右键的,也可以判断是左键双击还是右键双击触发:


void Widget::mouseDoubleClickEvent(QMouseEvent* event)
{
    if(event->button()==Qt::LeftButton) 
        qDebug("Left");
    if(event->button()==Qt::RightButton)
        qDebug("Right");
}

Qt鼠标双击事件mouseDoubleClickEvent(QMouseEvent* event)_第2张图片

你可能感兴趣的:(Qt)