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

这里写图片描述

  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt 场景、视图、项应用实例,Qt整套源码,喜欢请下载: Link: #include <QGraphicsLineItem> class Node; class Link : public QGraphicsLineItem { public: Link(Node *formN, Node *toN); ~Link(); inline Node *formNode()const { return myFormNode; } inline Node *toNode() const { return myToNode; } void setColor(const QColor &c); QColor color()const; void trackNodes(); private: Node *myFormNode; Node *myToNode; }; Node: #ifndef NODE_H #define NODE_H #include <QGraphicsItem> #include <QSet> class Link; class Node : public QGraphicsItem { public: Node(); ~Node(); inline void setText(const QString &text;); inline QString text()const {return myText;} inline void setTextColor(const QColor &c) { myTextColor = c; update();} inline QColor textColor()const{return myTextColor;} inline void setBackgroundColor(const QColor &c) { myBackgroundColor = c; update();} inline QColor backgroundColor()const{return myBackgroundColor;} inline void setOutLineColor(const QColor &c) { myOutLineColor = c; update();} inline QColor outLineColor()const{return myOutLineColor;} void addLink(Link *l); void removeLink(Link *l); QRectF boundingRect()const Q_DECL_OVERRIDE; QPainterPath shape() const Q_DECL_OVERRIDE; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = Q_NULLPTR) Q_DECL_OVERRIDE; protected: void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE; QVariant itemChange(GraphicsItemChange change, const QVariant &value;) Q_DECL_OVERRIDE; private: QRectF outlineRect()const; int roundness(double size)const;//计算圆角 QSet<Link *> myLinks; QString myText; QColor myTextColor; QColor myBackgroundColor; QColor myOutLineColor; }; #endif // NODE_H View: #ifndef GRAPHICSVIEW_H #define GRAPHICSVIEW_H #include <QWidget> #include <QGraphicsView> #include <QGraphicsScene> class GraphicsView : public QGraphicsView { Q_OBJECT public: explicit GraphicsView(QGraphicsScene *scene, QWidget *parent = 0); protected: void wheelEvent(QWheelEvent *e); }; #endif // GRAPHICSVIEW_H

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值