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 分钟前
考研408笔记之操作系统(三)——内存管理
笔记·操作系统·考研408
Rabitebla3 分钟前
【数据结构】消失的数字+ 轮转数组:踩坑详解
c语言·数据结构·c++·算法·leetcode
Queenie_Charlie17 分钟前
关于二叉树(2)
数据结构·c++·二叉树·简单树结构
故事和你9120 分钟前
洛谷-算法2-2-常见优化技巧1
开发语言·数据结构·c++·算法·动态规划·图论
SunAqua24 分钟前
《MCU与DSP芯片笔记》一、DSP芯片TI C2000系列TMS320F28035
笔记·单片机·嵌入式硬件
白夜111729 分钟前
C++(mixins 混入模式)
开发语言·c++·笔记
清风玉骨34 分钟前
C++/Qt从零开始编译使用libxlsxwriter库
开发语言·qt
苦 涩38 分钟前
考研408笔记之操作系统(二)——进程与线程
笔记·操作系统·考研408
苦 涩43 分钟前
考研408笔记之操作系统(一)——计算机系统概述
笔记·操作系统·考研408
三品吉他手会点灯1 小时前
C语言学习笔记 - 16.C编程预备计算机专业知识 - Hello World程序的运行原理
c语言·笔记·学习