【Qt开发流程】之事件系统2:鼠标事件及滚轮事件

序章

以下链接是拖放事件介绍和使用示例:
【Qt开发流程】之拖放操作1:介绍链接: https://blog.csdn.net/MrHHHHHH/article/details/134626484
【Qt开发流程】之拖放操作2:使用链接: https://blog.csdn.net/MrHHHHHH/article/details/134632006
以下链接是事件系统及键盘事件描述及示例:
【Qt开发流程】之事件系统1:事件系统描述及事件发生流程链接: https://blog.csdn.net/MrHHHHHH/article/details/134722922

鼠标事件、滚轮事件

QMouseEvent类用来表示一个鼠标事件,在窗口部件中按下鼠标或者移动鼠标,释放鼠标时,都会产生鼠标事件。
利用QMouseEvent类可以获取鼠标是左键还是右键按下,及鼠标的当前位置信息等。
重定义部件的鼠标事件可以实现自定义的一些操作。
QWheelEvent类用来表示鼠标滚轮事件,主要用来获取滚轮移动的方向和距离。

示例

以下举个栗子,实现效果是:

  • 在界面上按下鼠标左键拖动窗口
  • 双击鼠标左键全屏
  • 鼠标右键使指针变成一个自定义图片
  • 使用鼠标滚轮放大缩小编辑器内容。

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
   
class MainWindow;
}

class MainWindow : public QMainWindow
{
   
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

protected:
    void mousePressEvent(QMouseEvent *event);
    void mouseDoubleClickEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void wheelEvent(QWheelEvent *event);

private:
    Ui::MainWindow *ui;

    QPoint      mOffset;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QMouseEvent>
#include <QWheelEvent>
#include <QPoint>
#include <QDebug>
#include <QCursor>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
   
    ui->setupUi(this);
    QCursor cursor;
    cursor.setShape(Qt::OpenHandCursor);
    setCursor(cursor);
}

MainWindow::~MainWindow()
{
   
    delete ui;
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
   
    if(event->button() == Qt::LeftButton)
    {
   
        QCursor cursor;
        cursor.setShape(Qt::ClosedHandCursor);
        QApplication::setOverrideCursor(cursor);
        mOffset = event->globalPos() - this->pos();
    }else if(event->button() == Qt::RightButton){
   
        QCursor cursor(QPixmap("E:/images/icon.png"));
        QApplication::setOverrideCursor(cursor);
    }
}

void MainWindow::mouseDoubleClickEvent(QMouseEvent *event)
{
   
    if(event->button() == Qt::LeftButton)
    {
   
        if(windowState() != Qt::WindowFullScreen)
        {
   
            setWindowState(Qt::WindowFullScreen);
        }else{
   
            setWindowState(Qt::WindowNoState);
        }
    }
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
   
    QApplication::restoreOverrideCursor();
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
   
    if(event->buttons() & Qt::LeftButton)
    {
   
        QPoint p;
        p = event->globalPos() - mOffset;
        move(p);
    }
}

void MainWindow::wheelEvent(QWheelEvent *event)
{
   
    if(event->delta() > 0)
    {
   
        ui->textEdit->zoomIn();
    }else {
   
        ui->textEdit->zoomOut();
    }
}

以上代码解释:

  1. 设置鼠标光标为打开手形光标。
  2. 当左键按下时,将鼠标光标设置为关闭手形光标,并获得鼠标偏移量,以移动窗口。
  3. 当右键按下时,将鼠标光标设置为自定义图标。
  4. 当双击左键时,切换窗口全屏模式。
  5. 当鼠标释放时,恢复光标到默认状态。
  6. 当鼠标移动并按下时,根据鼠标偏移量移动窗口。
  7. 当滚轮滚动时,文本编辑区域实现缩放功能。

注意:为了使编译器不产生警告信息,当没用的变量,可以用Q_UNUSED()宏。
如:

Q_UNUSED(event)

效果

整体效果如下:
视频里,不知道为啥不显示右键及释放事件。但实际是有的。代码可运行。

鼠标及滚轮事件

结论

求推荐一款400万以上的跑车,起步快,够舒适,外观好看点的,越贵越好,我想换个手机壁纸

相关推荐

  1. Qt处理鼠标滚轮事件,放大缩小地图

    2023-12-05 16:22:09       45 阅读
  2. qt hoverMoveEvent鼠标响应事件

    2023-12-05 16:22:09       39 阅读
  3. Qt 鼠标进入离开事件

    2023-12-05 16:22:09       36 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-05 16:22:09       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-05 16:22:09       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-05 16:22:09       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-05 16:22:09       18 阅读

热门阅读

  1. 服务器固件

    2023-12-05 16:22:09       33 阅读
  2. 深入浅出 Golang 中的直接依赖和间接依赖管理

    2023-12-05 16:22:09       29 阅读
  3. EasyExcel list<Map>批量导出多个sheet

    2023-12-05 16:22:09       39 阅读
  4. Hive进阶函数:inline() 和 struct() ,一列转多行

    2023-12-05 16:22:09       36 阅读
  5. Appium:iOS测试比Android测试更难?

    2023-12-05 16:22:09       43 阅读
  6. SpringMVC的基础知识

    2023-12-05 16:22:09       30 阅读
  7. 总结 SpringMVC 中的常用注解和用法

    2023-12-05 16:22:09       34 阅读
  8. Django大回顾-4 自定义过滤器和标签、模型层

    2023-12-05 16:22:09       35 阅读
  9. Backend - Django JsonResponse & HttpResponse

    2023-12-05 16:22:09       20 阅读
  10. springmvc 重定向调节数据方法

    2023-12-05 16:22:09       38 阅读