【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

cpp 复制代码
#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

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()宏。

如:

cpp 复制代码
Q_UNUSED(event)

效果

整体效果如下:

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

鼠标及滚轮事件

结论

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

相关推荐
Geek之路20 分钟前
QT-多线程、线程池的使用
qt·多线程
凯子坚持 c38 分钟前
C语言复习概要(四)
c语言·开发语言
何陈陈1 小时前
【Linux】线程池
linux·服务器·开发语言·c++
清风玉骨1 小时前
Qt-QHBoxLayout布局类控件(42)
开发语言·qt
夏旭泽1 小时前
C-include
开发语言·c++
通信仿真实验室1 小时前
MATLAB使用眼图分析QPSK通信系统接收端匹配滤波后的信号
开发语言·算法·matlab
感谢地心引力1 小时前
【Qt】Qt安装(2024-10,QT6.7.3,Windows,Qt Creator 、Visual Studio、Pycharm 示例)
c++·windows·python·qt·visual studio
通信仿真实验室1 小时前
(15)衰落信道模型作用于信号是相乘还是卷积
开发语言·人工智能·算法·matlab
远望樱花兔2 小时前
【d59】【Java】【力扣】146.LRU缓存
java·开发语言·算法
Bruce_Liuxiaowei2 小时前
Python小示例——质地不均匀的硬币概率统计
开发语言·python·概率统计