根据书中的内容,简单的实现鼠标相关的内容

源代码如下

.h
#ifndef MOUSEEVENT_H
#define MOUSEEVENT_H #include <QMainWindow>
#include <QLabel>
#include <QStatusBar>
#include <QMouseEvent>
class MouseEvent : public QMainWindow
{
Q_OBJECT public:
MouseEvent(QWidget *parent = 0);
~MouseEvent();
protected:
void mousePressEvent(QMouseEvent *e);
void mouseMoveEvent(QMouseEvent *e);
void mouseReleaseEvent(QMouseEvent *e);
void mouseDoubleClickEvent(QMouseEvent *e);
private:
QLabel *statusLabel;
QLabel *MousePosLabel;
}; #endif // MOUSEEVENT_H
.cpp
#include "mouseevent.h" MouseEvent::MouseEvent(QWidget *parent)
: QMainWindow(parent)
{
setWindowTitle(tr("鼠标事件"));
statusLabel = new QLabel;
statusLabel->setText(tr("当前位置"));
statusLabel->setFixedWidth(100);
MousePosLabel = new QLabel;
MousePosLabel->setText(tr(""));
MousePosLabel->setFixedWidth(100);
statusBar()->addPermanentWidget(statusLabel);
statusBar()->addPermanentWidget(MousePosLabel);
this->setMouseTracking(true);
resize(400,200);
} void MouseEvent::mousePressEvent(QMouseEvent *e)
{
QString str="("+QString::number(e->x())+","+QString::number(e->y()) +")";
if(e->button()==Qt::LeftButton)
{
statusBar()->showMessage(tr("左键")+str);
}
else if(e->button()==Qt::RightButton)
{
statusBar()->showMessage(tr("右键")+str);
}
else if(e->button()==Qt::MidButton)
{
statusBar()->showMessage(tr("中键")+str);
}
} void MouseEvent::mouseMoveEvent(QMouseEvent *e)
{
MousePosLabel->setText("("+QString::number(e->x())+","+QString::number(e->y())+")");
} void MouseEvent::mouseReleaseEvent(QMouseEvent *e)
{
QString str="("+QString::number(e->x())+","+QString::number(e->y()) +")";
statusBar()->showMessage(tr("释放在:")+str,3000);
} void MouseEvent::mouseDoubleClickEvent(QMouseEvent *e){} MouseEvent::~MouseEvent()
{ }

工程代码:https://gitee.com/DreamLife-Technology_DreamLife/MouseEvent

Qt-事件处理-鼠标事件的更多相关文章

  1. QT之鼠标事件

    Widget.h: #ifndef WIDGET_H #define WIDGET_H #include<QWidget> #include<QMouseEvent> clas ...

  2. Qt Quick鼠标事件处理、键盘、计时器

    在<Qt Quick 事件处理之信号与槽>中介绍了 QML 中怎样使用内建类型的信号以及怎样自己定义信号,这次我们来看看怎样处理鼠标.键盘.定时器等事件.这些时间在处理时,一般是通过信号来 ...

  3. QT学习笔记5:QMouseEvent鼠标事件简介

    一.QMouseEvent的详细描述 首先请注意,Qt中的QMouseEvent一般只涉及鼠标左键或右键的单击.释放等操作,而对鼠标滚轮的响应则通过QWheeEvent来处理. QMouseEvent ...

  4. Qt事件系统之二:鼠标事件和滚轮事件

    在Qt中,事件作为一个对象,继承自 QEvent 类,常见的有键盘事件 QKeyEvent.鼠标事件 QMouseEvent 和定时器事件 QTimerEvent 等,与 QEvent 类的继承关系图 ...

  5. qt实现类似QQ伸缩窗口--鼠标事件应用

    原创文章,引用请保证原文完整性,尊重作者劳动,原文地址http://blog.csdn.net/hiwubihe/article/details/38678305,qq:1269122125. 上一章 ...

  6. 浅谈Qt事件的路由机制:鼠标事件

    请注意,本文是探讨文章而不是教程,是根据实验和分析得出的结果,可能是错的,因此欢迎别人来探讨和纠正. 这几天对于Qt的事件较为好奇,平时并不怎么常用,一般都是用信号,对于事件的处理,一般都是需要响应键 ...

  7. 2、Qt Project之鼠标事件监控

    鼠标事件监控: 对于鼠标时间监控的相关操作,我们在简历工程的时候需要选择的是QWidget基类,不选择QMainWindow基类,如下所示: Base class:QWidget Step1:我们首先 ...

  8. QT 11 鼠标键盘事件添加

    鼠标事件 void mousePressEvent(QMouseEvent *event); //单击 void mouseReleaseEvent(QMouseEvent *event); //释放 ...

  9. Qt 隐藏标题栏 窗口移动 鼠标事件

    摘要 隐藏标题栏 头文件声明鼠标移动虚函数 .cpp文件实现功能 1 setWindowFlags(Qt::FramelessWindowHint | windowFlags()); 无标题栏移动窗体 ...

  10. Qt事件处理机制

    研一的时候开始使用Qt,感觉用Qt开发图形界面比MFC的一套框架来方便的多.后来由于项目的需要,也没有再接触Qt了.现在要重新拾起来,于是要从基础学起. Now,开始学习Qt事件处理机制. 先给出原文 ...

随机推荐

  1. 51Nod - 1205 (流水先调度)超级经典的贪心 模板题

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1205 N个作业{1,2,…,n}要在由2台机器M1和M2组成 ...

  2. ConcurrentHashMap 中putIfAbsent 和put的区别

    putIfAbsent 源代码 public V putIfAbsent(K key, V value) { Segment<K,V> s; if (value == null) thro ...

  3. SimpleProfile_GetParameter && SimpleProfile_SetParameter

    /********************************************************************* * @fn SimpleProfile_GetParame ...

  4. idea操作 clone项目、 import项目所有注解全部报错

    操作:从现有的git上边clone项目,前提是开发工具,开发环境都一样错误类型:所有的注解全部报错 原因: 是选择了Create from existing source 一路Next下去,Maven ...

  5. 系统优化怎么做-Linux系统配置优化

    大家好,这里是「聊聊系统优化 」,并在下列地址同步更新 博客园:http://www.cnblogs.com/changsong/ 知乎专栏:https://zhuanlan.zhihu.com/yo ...

  6. MYSQL 8.0.11 安装过程及 Navicat 链接时遇到的问题

    参考博客:https://blog.csdn.net/WinstonLau/article/details/78666423 我的系统和软件版本是这样的: 系统环境:win7.64位 MySQL版本: ...

  7. Linux 只显示目录或者文件方法

    ls 参数 -a 表示显示所有文件,包含隐藏文件-d 表示显示目录自身的属性,而不是目录中的内容-F 选项会在显示目录条目时,在目录后加一个/ 只显示目录 方法一: find . -type d -m ...

  8. jQuery基本toggle() toggleClass() 使用

    今天来学习一下jQuery的基本函数的使用,很简单. 首先写一个button做控制按钮,然后写一个div用按钮控制idv做动画,从而测试JQuery的动画函数 <head> <met ...

  9. 浅析BFC布局的概念以及作用

    BFC的概念以及作用 BFC的定义: (Block formatting context)直译为"块级格式化上下文".它是一个独立的渲染区域,只有Block-level box参与 ...

  10. python3 练习题100例 (一)

    断断续续的学了很久的python,有很多又忘记了.从今天开始用实例再进行一次学习,并记录.本人小白一个,请大家多多指教. #!/usr/bin/env python3 # -*- coding: ut ...