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
相关推荐
帅云毅11 分钟前
Web3.0的认知补充(去中心化)
笔记·学习·web3·去中心化·区块链
豆豆11 分钟前
day32 学习笔记
图像处理·笔记·opencv·学习·计算机视觉
꧁坚持很酷꧂22 分钟前
Linux Ubuntu18.04下安装Qt Craeator 5.12.9(图文详解)
linux·运维·qt
nenchoumi311933 分钟前
VLA 论文精读(十六)FP3: A 3D Foundation Policy for Robotic Manipulation
论文阅读·人工智能·笔记·学习·vln
ChoSeitaku36 分钟前
17.QT-Qt窗口-工具栏|状态栏|浮动窗口|设置停靠位置|设置浮动属性|设置移动属性|拉伸系数|添加控件(C++)
c++·qt·命令模式
凉、介39 分钟前
PCI 总线学习笔记(五)
android·linux·笔记·学习·pcie·pci
软行2 小时前
LeetCode 每日一题 2845. 统计趣味子数组的数目
数据结构·c++·算法·leetcode
鬼面瓷2 小时前
CAPL编程_03
前端·数据库·笔记
小贾要学习2 小时前
【C++】继承----下篇
android·java·c++
帅云毅2 小时前
Web漏洞--XSS之订单系统和Shell箱子
前端·笔记·web安全·php·xss