【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万以上的跑车,起步快,够舒适,外观好看点的,越贵越好,我想换个手机壁纸

相关推荐
用户805533698035 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner5 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz10 天前
QML Hello World 入门示例
qt
xcyxiner13 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner14 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner14 天前
DicomViewer (添加模型类)3
qt
xcyxiner15 天前
DicomViewer (目录调整) 2
qt
xcyxiner15 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
LDR00616 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术16 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript