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

重写mouseDoubleClickEvent(QMouseEvent event)*

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


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

#include<QWidget>
#include<QDebug>

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<QApplication>

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

    return app.exec();
}

技术分享

我在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");
}

技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

文章来自:http://blog.csdn.net/lyh__521/article/details/47099745
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3