mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow>
#include <QLabel>
#include <QStatusBar>
#include <QMouseEvent> class MainWindow : public QMainWindow
{
Q_OBJECT public:
MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
//重定义了QWidget类的鼠标事件方法
void mousePressEvent(QMouseEvent *e);
void mouseMoveEvent(QMouseEvent *e);
void mouseReleaseEvent(QMouseEvent *e);
void mouseDoubleClickEvent(QMouseEvent *e);
private:
QLabel *statusLabel;
QLabel *MousePosLabel; }; #endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setWindowTitle(tr("鼠标事件"));
statusLabel=new QLabel;
statusLabel->setText(tr("当前位置:"));
statusLabel->setFixedWidth(100);
MousePosLabel=new QLabel;
MousePosLabel->setText(tr(""));
MousePosLabel->setFixedWidth(100); //在QMainWindow的状态栏中加入控件
statusBar()->addPermanentWidget(statusLabel);
statusBar()->addPermanentWidget(MousePosLabel);
//设置窗体追踪鼠标
this->setMouseTracking(true);
resize(400,200);
}
//mousePressEvent()函数为鼠标按下事件响应函数
void MainWindow::mousePressEvent(QMouseEvent *e)
{
if(e->button() == Qt::LeftButton)
{
statusBar()->showMessage(tr("左键"));
}
else if(e->button() == Qt::RightButton)
{
statusBar()->showMessage(tr("右键"));
}
else if(e->button() == Qt::MidButton)
{
statusBar()->showMessage(tr("中键"));
}
}
//mouseMoveEvent()函数为鼠标移动事件响应函数
void MainWindow::mouseMoveEvent(QMouseEvent *e)
{
MousePosLabel->setText("("+QString::number(e->x())+","+QString::number(e->y())+")");
}
//mouseReleaseEvent()函数为鼠标松开事件响应函数
void MainWindow::mouseReleaseEvent(QMouseEvent *e)
{ }
//mouseDoubleClickEvent()函数为鼠标双击事件响应函数
void MainWindow::mouseDoubleClickEvent(QMouseEvent *e)
{ }
MainWindow::~MainWindow()
{ }

main.cpp

#include "mainwindow.h"
#include <QApplication> int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show(); return a.exec();
}

运行效果

鼠标移动时,显示鼠标的坐标



当鼠标左键按下时,显示左键按下

参考资料

《Qt5开发及实例》

Qt5鼠标事件及实例的更多相关文章

  1. c#全局鼠标事件以及鼠标事件模拟

    最近在编写Max插件时,其主容器FlowLayoutPanel由于隐藏了滚动条,要实现按住鼠标中键上下拖动的功能,因此尝试了全局鼠标事件.以及鼠标勾子,可惜由于Max不争气?都未能实现,于是代码报废, ...

  2. JavaScript学习笔记3之 数组 & arguments(参数对象)& 数字和字符串转换 & innerText/innerHTML & 鼠标事件

    一.Array数组 1.数组初始化(Array属于对象类型) /*关于数组的初始化*/ //1.创建 Array 对象--方法1: var arr1=[]; arr1[0]='aa';//给数组元素赋 ...

  3. winform中键盘和鼠标事件的捕捉和重写(转)

    在 编写winform应用程序时,有时需要无论在哪个控件获取焦点时,对某一个键盘输入或者鼠标事件都进行同样的操作.比如编写一个处理图片的应用程序时, 希望无论当前哪个控件获得焦点,当用户按上.下.左. ...

  4. Js学习笔记一(鼠标事件.....)

    1.encodeURI与decodeURI()转化url为有效的url(能被识别) Url="http://news.baidu.com/p.php?id='测试'&姓名=hkui& ...

  5. Wpf 鼠标拖动元素实例

    1.Wpf中鼠标捕获和释放 //以矩形为例 //创建鼠标捕获 Mouse.Capture(rectOne); //释放鼠标捕获 rectOne.ReleaseMouseCapture(); 2.Wpf ...

  6. Qt 自定义事件详细实例(继承QEvent,然后QCoreApplication::postEvent()、sendEvent())

    创建用户事件 创建一个自定义类型的事件,首先需要有一个事件号,其值通常大于QEvent::User.为了传递事件信息,因此必须编写自定义的事件类,该事件类从QEvent继承. 编写用户事件:编写用户事 ...

  7. 【pyHook】 监测键盘鼠标事件等

    [pyHook] pyHook是一个用来进行键盘.鼠标等层面事件监控的库.这个库的正常工作需要pythoncom等操作系统的API的支持.首先来说说如何安装. 直接pip install pyHook ...

  8. (93)Wangdao.com_第二十六天_鼠标事件

    鼠标事件 与鼠标相关的事件,继承了 MouseEvent 接口 分类: click        按下鼠标(通常是按下主按钮)时触发.        mousedown 首先触发,mouseup 接着 ...

  9. 鼠标事件event和坐标

    鼠标事件(e=e||window.event) event.clientX.event.clientY 鼠标相对于浏览器窗口可视区域的X,Y坐标(窗口坐标),可视区域不包括工具栏和滚动条.IE事件和标 ...

随机推荐

  1. JavaScript document open() 方法:打开一个新文档

    <html> <head> <script type="text/javascript"> function createNewDoc() { ...

  2. mysql配置主从复制和常见问题

    克隆192.168.138.130(主库),修改后的ip为192.168.138.130(从库),修改131机器的/etc/udev/rules.d/70-persistent-net.rules,将 ...

  3. R语言开发环境搭建

    R语言开发环境搭建 一.环境 Win7 64bit系统 二.R软件下载 R 3.5.2 for Windows,官网:https://www.r-project.org/ RStudio 1.1.46 ...

  4. shell脚本一键安装redis集群[最终版]

    直接上shell了. #!/bin/bash #---------------------------------------------------------------------------- ...

  5. sentos7为例添加python3和python2共存

    转载:https://www.cnblogs.com/JahanGu/p/7452527.html 1.查看是否已经安装Python CentOS 7.2 默认安装了python2.7.5 因为一些命 ...

  6. 自动发现实现url+响应时间监控

    url自动发现脚本: [root@jenkins scripts]# cat  urlDiscovery.py #!/usr/bin/env python #coding:utf-8 import o ...

  7. UML建模类图【2】--☆☆

    虚线箭头指向依赖: 实线箭头指向关联: 虚线三角指向接口: 实线三角指向父类: 空心菱形能分离而独立存在,是聚合: 实心菱形精密关联不可分,是组合: 上面是UML的语法. 在画类图的时候,理清类和类之 ...

  8. NOIP 2016 蚯蚓 (luogu 2827 & uoj 264) - 鬼畜的优化

    题目描述 本题中,我们将用符号\lfloor c \rfloor⌊c⌋表示对c向下取整,例如:\lfloor 3.0 \rfloor= \lfloor 3.1 \rfloor=\lfloor 3.9 ...

  9. Bootstrap3基础 pagination 分页按钮 简单示例

      内容 参数   OS   Windows 10 x64   browser   Firefox 65.0.2   framework     Bootstrap 3.3.7   editor    ...

  10. mysql数据库连接的测试代码语句片断

    手动连接数据库 的代码片断, 用于各种数据库的测试... $conn = mysql_connect('localhost', 'root', '') or die('failed to connec ...