码迷,mamicode.com
首页 > 其他好文 > 详细

QT之鼠标事件

时间:2020-07-09 01:18:01      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:mamicode   鼠标   mic   mouse   sep   release   void   user   src   

Widget.h:

#ifndef WIDGET_H
#define WIDGET_H

#include<QWidget>
#include<QMouseEvent>
class Widget : public QWidget
{
    Q_OBJECT
public:
    Widget(QWidget *parent = 0);
    ~Widget();
    void mousePressEvent(QMouseEvent* e);
    void mouseReleaseEvent(QMouseEvent* e);
    void mouseMoveEvent(QMouseEvent* e);
};

#endif // WIDGET_H

Widget.cpp:

#include "widget.h"
#include<QDebug>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    this->setMouseTracking(true);//设置鼠标移动事件不需要按下也能促发
}

void Widget::mousePressEvent(QMouseEvent* e)
{
    switch(e->button())
    {
    case Qt::LeftButton:
        qDebug()<<"leftbutton is pressed"<<endl;
        break;
     case   Qt::RightButton:
         qDebug()<<"rightbutton  is pressed"<<endl;
        break;
    case Qt::MidButton:
         qDebug()<<"midbutton  is pressed"<<endl;
        break;
     default:
        break;
    }
}
void Widget::mouseReleaseEvent(QMouseEvent* e)
{
    switch(e->button())
    {
    case Qt::LeftButton:
        qDebug()<<"leftbutton is released"<<endl;
        break;
     case   Qt::RightButton:
         qDebug()<<"rightbutton  is released"<<endl;
        break;
    case Qt::MidButton:
         qDebug()<<"midbutton  is released"<<endl;
        break;
     default:
        break;
    }
}
void Widget::mouseMoveEvent(QMouseEvent* e)
{
    qDebug()<<e->pos()<<endl;
}
Widget::~Widget()
{

}

main.cpp:

#include "widget.h"
#include <QApplication>

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

    return a.exec();
}

效果:

技术图片

 

QT之鼠标事件

标签:mamicode   鼠标   mic   mouse   sep   release   void   user   src   

原文地址:https://www.cnblogs.com/SunShine-gzw/p/13270171.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!