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
相关推荐
大江东去浪淘尽千古风流人物1 小时前
【cuVSLAM】GPU 加速、多相机、实时视觉/视觉惯性 SLAM设计优势
c++·人工智能·数码相机·ubuntu·计算机视觉·augmented reality
自信150413057597 小时前
重生之从0开始学习c++之模板初级
c++·学习
历程里程碑7 小时前
2. Git版本回退全攻略:轻松掌握代码时光机
大数据·c++·git·elasticsearch·搜索引擎·github·全文检索
极客智造8 小时前
深度解析 C++ 类继承与多态:面向对象编程的核心
c++
江湖人称小鱼哥9 小时前
Obsidian-Graphify-让你的笔记库自己长出知识图谱
笔记·知识图谱·obsidian·claude code·graphify·卡帕西
苦 涩10 小时前
考研408笔记之计算机网络(三)——数据链路层
笔记·计算机网络·考研408
零号全栈寒江独钓10 小时前
基于c/c++实现linux/windows跨平台获取ntp网络时间戳
linux·c语言·c++·windows
CSCN新手听安11 小时前
【linux】高级IO,以ET模式运行的epoll版本的TCP服务器实现reactor反应堆
linux·运维·服务器·c++·高级io·epoll·reactor反应堆
三品吉他手会点灯11 小时前
STM32F103 学习笔记-21-串口通信(第4节)—串口发送和接收代码讲解(中)
笔记·stm32·单片机·嵌入式硬件·学习
雾岛听蓝12 小时前
Qt操作指南:窗口组成与菜单栏
开发语言·经验分享·笔记·qt