序章
以下链接是拖放事件介绍和使用示例:
【Qt开发流程】之拖放操作1:介绍链接: https://blog.csdn.net/MrHHHHHH/article/details/134626484
【Qt开发流程】之拖放操作2:使用链接: https://blog.csdn.net/MrHHHHHH/article/details/134632006
以下链接是事件系统及键盘事件描述及示例:
【Qt开发流程】之事件系统1:事件系统描述及事件发生流程链接: https://blog.csdn.net/MrHHHHHH/article/details/134722922
鼠标事件、滚轮事件
QMouseEvent类用来表示一个鼠标事件,在窗口部件中按下鼠标或者移动鼠标,释放鼠标时,都会产生鼠标事件。
利用QMouseEvent类可以获取鼠标是左键还是右键按下,及鼠标的当前位置信息等。
重定义部件的鼠标事件可以实现自定义的一些操作。
QWheelEvent类用来表示鼠标滚轮事件,主要用来获取滚轮移动的方向和距离。
示例
以下举个栗子,实现效果是:
- 在界面上按下鼠标左键拖动窗口
 - 双击鼠标左键全屏
 - 鼠标右键使指针变成一个自定义图片
 - 使用鼠标滚轮放大缩小编辑器内容。
 
mainwindow.h
            
            
              cpp
              
              
            
          
          #ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
protected:
    void mousePressEvent(QMouseEvent *event);
    void mouseDoubleClickEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void wheelEvent(QWheelEvent *event);
private:
    Ui::MainWindow *ui;
    QPoint      mOffset;
};
#endif // MAINWINDOW_H
        mainwindow.cpp
            
            
              cpp
              
              
            
          
          #include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMouseEvent>
#include <QWheelEvent>
#include <QPoint>
#include <QDebug>
#include <QCursor>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QCursor cursor;
    cursor.setShape(Qt::OpenHandCursor);
    setCursor(cursor);
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        QCursor cursor;
        cursor.setShape(Qt::ClosedHandCursor);
        QApplication::setOverrideCursor(cursor);
        mOffset = event->globalPos() - this->pos();
    }else if(event->button() == Qt::RightButton){
        QCursor cursor(QPixmap("E:/images/icon.png"));
        QApplication::setOverrideCursor(cursor);
    }
}
void MainWindow::mouseDoubleClickEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        if(windowState() != Qt::WindowFullScreen)
        {
            setWindowState(Qt::WindowFullScreen);
        }else{
            setWindowState(Qt::WindowNoState);
        }
    }
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    QApplication::restoreOverrideCursor();
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton)
    {
        QPoint p;
        p = event->globalPos() - mOffset;
        move(p);
    }
}
void MainWindow::wheelEvent(QWheelEvent *event)
{
    if(event->delta() > 0)
    {
        ui->textEdit->zoomIn();
    }else {
        ui->textEdit->zoomOut();
    }
}
        以上代码解释:
- 设置鼠标光标为打开手形光标。
 - 当左键按下时,将鼠标光标设置为关闭手形光标,并获得鼠标偏移量,以移动窗口。
 - 当右键按下时,将鼠标光标设置为自定义图标。
 - 当双击左键时,切换窗口全屏模式。
 - 当鼠标释放时,恢复光标到默认状态。
 - 当鼠标移动并按下时,根据鼠标偏移量移动窗口。
 - 当滚轮滚动时,文本编辑区域实现缩放功能。
 
注意:为了使编译器不产生警告信息,当没用的变量,可以用Q_UNUSED()宏。
如:
            
            
              cpp
              
              
            
          
          Q_UNUSED(event)
        效果
整体效果如下:
视频里,不知道为啥不显示右键及释放事件。但实际是有的。代码可运行。
鼠标及滚轮事件
结论
求推荐一款400万以上的跑车,起步快,够舒适,外观好看点的,越贵越好,我想换个手机壁纸。