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. amqp 抓包

    1. wireshark 2. tcpick -yR -r  file.name

  2. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 828D) - 贪心

    Arkady needs your help again! This time he decided to build his own high-speed Internet exchange poi ...

  3. CentOS6.8下安装Redis

    1.由于Redis是使用C语言开发的,安装时需要对Redis的源码进行编译,编译依赖gcc环境,如果没有gcc,需要先安装gcc: yum install gcc-c++ 2.安装完成后,进入Redi ...

  4. topcoder srm 545 div1

    problem1 link 这个可以贪心地从前向后构造.假设当前已经的字符串为$S$,对于一个字符$c$来说,设将$c$加到$S$后得到的新串为$S^{'}$.那么如果$X+Y+Z \ge minIn ...

  5. centos6.8下源码编译安装tmux

    1. 获取源码 git clone https://github.com/tmux/tmux.git ~/tmux 2. 准备工作 2.1 安装ncurses开发库 yum install ncurs ...

  6. 编译openwrt时总是报错“staging_dir/target-aarch64_generic_glibc/stam$/.tools_compile_yynyyyyynyyyyynyynnyyyynyyyyyyyyyyyyyyynyynynnyyynny' failed”

    1. 详细错误如下: tools/Makefile:146: recipe for target '/home/jello/openwrt/staging_dir/target-aarch64_gen ...

  7. Docker 编排工具Rancher 1.6.18

    使用docker获取rancher [root@localhost /]# docker pull rancher/server:stable [root@localhost /]# docker i ...

  8. FJUT 聪明的商人(树上倍增)题解

    思路:求树上两点的距离,显然是dep[u] + dep[v] - 2 * dep[lca],用树上倍增去写. 参考:树上倍增的写法和应用(详细讲解,新手秒懂) 代码: #include<set& ...

  9. c# 之系统环境安装

    在重装系统后,对一些原有软件进行了卸载,不知道是什么原因总是提示vs2015 需安装IE10,但是又碰到ie10的一些插件不适合此系统.网上介绍的vs修复没有任何作用 最后找到方法是:重装系统,然后不 ...

  10. (转)Awsome Domain-Adaptation

    Awsome Domain-Adaptation 2018-08-06 19:27:54 This blog is copied from: https://github.com/zhaoxin94/ ...