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

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

code review!

文章目录

1.运行

2.main.cpp

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
相关推荐
江屿风20 分钟前
AI Agent架构全解析:从规划到记忆的智能跃迁
人工智能·笔记·架构·agent·c/s·mcp工作流
小灰灰搞电子3 小时前
C++ initializer_list 详解
c++·list
吃我一窝jio4 小时前
Java Bug复盘与性能优化实战
java·开发语言·笔记
Rabitebla5 小时前
【Linux】文件篇(六):Shell 实战——从系统调用到命令行重定向
linux·服务器·c++·笔记·学习
Star(up up up)6 小时前
MySQL 事务隔离级别是如何实现的?从 MVCC 到锁机制原理详解
笔记·vscode·sql·学习·程序人生·mysql·学习方法
琳少_19 小时前
java程序开发笔记
java·开发语言·数据库·笔记
无忧.芙桃20 小时前
C++语言原理与实践(二十三):哈希表的实现
c++·链表·哈希算法·散列表·哈希表
AvocadoZeroOneOI1 天前
B4500 [GESP202603 三级] 凯撒密码 详细解题报告
c++·算法
Z21-X1 天前
云原生运维学习笔记:Kubernetes Volume 与 Secret 实验
运维·笔记·学习·云原生·kubernetes
一只大鸽子1 天前
游戏开发笔记7-Godot RPG游戏基础-场景切换、全局脚本
笔记·游戏·godot