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
相关推荐
承渊政道7 分钟前
【优选算法】(实战体会位运算的逻辑思维)
数据结构·c++·笔记·学习·算法·leetcode·visual studio
m0_7167652332 分钟前
C++提高编程--STL常用容器(set/multiset、map/multimap容器)详解
java·开发语言·c++·经验分享·学习·青少年编程·visual studio
承渊政道39 分钟前
【优选算法】(实战推演模拟算法的蕴含深意)
数据结构·c++·笔记·学习·算法·leetcode·排序算法
朽棘不雕1 小时前
c++中为什么new[]和delete[]要配对使用
c++
elseif1231 小时前
出题团招人
c++
左左右右左右摇晃2 小时前
系统性能指标与损耗分析
笔记
不想写代码的星星2 小时前
SFINAE 的演进:从替换失败不是错误,到 Concepts 的优雅
c++
悠哉悠哉愿意2 小时前
【单片机复习笔记】第十六届省赛复盘
笔记·单片机·嵌入式硬件
2401_878530212 小时前
自定义内存布局控制
开发语言·c++·算法
leaves falling3 小时前
C++模板初阶:让代码“复制粘贴”自动化
开发语言·c++·自动化