qt-C++笔记之捕获鼠标滚轮事件并输出滚轮角度增量

qt-C++笔记之捕获鼠标滚轮事件并输出滚轮角度增量

code review!

1.运行

在这里插入图片描述

2.main.cpp

#include <QApplication>
#include <QWidget>
#include <QWheelEvent>
#include <QDebug>

class WheelWidget : public QWidget {
   
protected:
    void wheelEvent(QWheelEvent *event) override {
   
        // 输出滚轮事件的相关信息
        qDebug() << "Wheel event delta:" << event->angleDelta();
        if (event->orientation() == Qt::Vertical) {
   
            qDebug() << "Vertical wheel";
        } else {
   
            qDebug() << "Horizontal wheel";
        }

        // Standard event processing
        event->accept();
    }
};

int main(int argc, char *argv[]) {
   
    QApplication a(argc, argv);

    WheelWidget widget;
    widget.show();

    return QApplication::exec();
}

在WheelWidget类中,wheelEvent函数被重写。这个函数是QWidget的一个虚函数,用于处理鼠标滚轮事件。

3.main.pro

#-------------------------------------------------
#
# Project created by QtCreator
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = test
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do this, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        main.cpp

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

相关推荐

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

    2024-02-17 11:28:01       70 阅读
  2. Qt】实现 Ctrl + 鼠标滚轮 缩放文本功能

    2024-02-17 11:28:01       54 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-17 11:28:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-17 11:28:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-02-17 11:28:01       82 阅读
  4. Python语言-面向对象

    2024-02-17 11:28:01       91 阅读

热门阅读

  1. vue.js前端框架应用案例

    2024-02-17 11:28:01       45 阅读
  2. 职场通用技能 之 ---开口夸赞,闭口批评

    2024-02-17 11:28:01       46 阅读
  3. 中医师承关键步骤

    2024-02-17 11:28:01       50 阅读
  4. Python在生物信息学中的应用:列表推导式

    2024-02-17 11:28:01       47 阅读
  5. mysql sql符号的作用

    2024-02-17 11:28:01       48 阅读
  6. Docker安装和使用Redis

    2024-02-17 11:28:01       43 阅读
  7. 数据库系统概论整理与总结

    2024-02-17 11:28:01       46 阅读
  8. 基于Web技术的家居室内温湿度监测系统

    2024-02-17 11:28:01       53 阅读
  9. 打印最小公倍数

    2024-02-17 11:28:01       53 阅读
  10. 2/13 homework

    2024-02-17 11:28:01       43 阅读
  11. P1914 小书童——凯撒密码

    2024-02-17 11:28:01       47 阅读
  12. rtt设备io框架面向对象学习-uart设备

    2024-02-17 11:28:01       48 阅读