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
相关推荐
垆边人似月.37 分钟前
找出最小的 K 个数
数据结构·c++·算法
C语言小火车42 分钟前
C++ 为什么越来越依赖标准库?
java·开发语言·c++
云半S一44 分钟前
Agent 学习总结一
经验分享·笔记·学习·agent
跨知易知识产权1 小时前
美国发明专利和外观专利的保护期限分别是多久,外观专利是否需要缴纳维持费?
笔记
八月的雨季 最後的冰吻1 小时前
C++ 面试要点笔记(续)
c++·笔记·面试
青少儿编程课堂2 小时前
蓝桥杯·全国青少年信息素养大赛·电子学会C/C++考级:并查集连通块与朋友圈问题解析
c++·python·算法·bfs·信息学竞赛
wyhwust2 小时前
codeblocks程序调试
c++
我命由我123452 小时前
准入类与水平类职业资格
笔记·学习·职场和发展·产品运营·求职招聘·职场发展·产品经理
He BianGu2 小时前
【笔记】在WPF中BitmapScalingMode的作用
笔记·wpf
傲世仙尊2 小时前
运算符重载-operator函数与五个不能重载的符号
c++