案例
1、键盘按下w,s,a,d键分别为标签向上,下,左,右移动
鼠标按下获取本地坐标,全局坐标
鼠标双击获取本地坐标,全局坐标
鼠标移动获取本地坐标,全局坐标
让鼠标跟踪坐标,需要手动开启,标签会随着鼠标移动
widget.h代码
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();protected:// 确保使用 override 关键字void keyPressEvent(QKeyEvent *event) override; //声明键盘按下事件处理函数void keyReleaseEvent(QKeyEvent *event) override; //声明键盘抬起事件处理函数void mousePressEvent(QMouseEvent *event) override; //鼠标按下事件处理函数的声明void mouseReleaseEvent(QMouseEvent *event) override; //鼠标抬起事件处理函数void mouseDoubleClickEvent(QMouseEvent *event) override; //鼠标双击事件处理函数void mouseMoveEvent(QMouseEvent *event) override; //鼠标移动事件的声明private:Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp代码
#include "widget.h"
#include "ui_widget.h"
#include <QtDebug>
#include <QKeyEvent> //键盘事件Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);//如果需要让鼠标跟踪坐标,需要手动开启this->setMouseTracking(true);}
Widget::~Widget()
{delete ui;
}//重写键盘按下事件处理函数
void Widget::keyPressEvent(QKeyEvent *event)
{
// static int num = 1;
// qDebug() << "num = " << num++;//qDebug() << event->text() << "被按下,对应的键值为: " << event->key();//对用户按下的健进行判断switch(event->key()){//向上移动case 'W':case 'w':{//判断是否已经到界面外部了if(ui->label->y() <= -ui->label->height()){//将组件移动到界面的最下面ui->label->move(ui->label->x(),this->height());}//让组件向上移动10格ui->label->move(ui->label->x(),ui->label->y()-10);}break;//向下移动case 'S':case 's':{if(ui->label->y() >= this->height()){ui->label->move(ui->label->x(),-ui->label->height());}ui->label->move(ui->label->x(),ui->label->y()+10);}break;//向左移动case 'A':case 'a':{if(ui->label->x() <= -ui->label->width()){ui->label->move(this->width(),ui->label->y());}ui->label->move(ui->label->x()-10,ui->label->y());}break;//向右移动case 'D':case 'd':{if(ui->label->x() >= this->width()){ui->label->move(-ui->label->width(),ui->label->y());}ui->label->move(ui->label->x()+10,ui->label->y());}break;}
}//键盘抬起事件处理函数的定义
void Widget::keyReleaseEvent(QKeyEvent *event)
{qDebug() << event->text() <<"被按下,对应的键值为:"<<event->key();
}//鼠标按下事件处理函数
void Widget::mousePressEvent(QMouseEvent *event)
{//判断是哪个鼠标键按下if(event->button() == Qt::LeftButton){qDebug() << "鼠标左键被按下了";}else if(event->button() == Qt::RightButton){qDebug() << "鼠标右键被按下了";}else if(event->button() == Qt::MidButton){qDebug() << "鼠标中键被按下了";}
}//键盘抬起事件处理函数的定义
void Widget::mouseReleaseEvent(QMouseEvent *event)
{//判断是哪个鼠标键按下if(event->button() == Qt::LeftButton){qDebug() << "鼠标左键被抬起,本地坐标:" << event->pos() << "鼠标左键被抬起,全局坐标:" << event->globalPos();}else if(event->button() == Qt::RightButton){qDebug() << "鼠标右键被抬起,本地坐标:" << event->pos() << "鼠标左键被抬起,全局坐标:" << event->globalPos();}else if(event->button() == Qt::MiddleButton){qDebug() << "鼠标中键被抬起,本地坐标:" << event->pos() << "鼠标左键被抬起,全局坐标:" << event->globalPos();}
}//鼠标双击事件处理函数的定义
void Widget::mouseDoubleClickEvent(QMouseEvent *event)
{//判断是哪个鼠标键按下if(event->button() == Qt::LeftButton){qDebug() << "鼠标左键被双击,本地坐标:" << event->pos() << "鼠标左键被抬起,全局坐标:" << event->globalPos();}else if(event->button() == Qt::RightButton){qDebug() << "鼠标右键被双击,本地坐标:" << event->pos() << "鼠标左键被抬起,全局坐标:" << event->globalPos();}else if(event->button() == Qt::MiddleButton){qDebug() << "鼠标中键被双击,本地坐标:" << event->pos() << "鼠标左键被抬起,全局坐标:" << event->globalPos();}
}//鼠标移动事件的定义
void Widget::mouseMoveEvent(QMouseEvent *event)
{//判断是哪个鼠标键按下if(event->button() == Qt::LeftButton){qDebug() << "鼠标左键被双击,本地坐标:" << event->pos() << "鼠标左键被抬起,全局坐标:" << event->globalPos();}else if(event->button() == Qt::RightButton){qDebug() << "鼠标右键被双击,本地坐标:" << event->pos() << "鼠标左键被抬起,全局坐标:" << event->globalPos();}else if(event->button() == Qt::MiddleButton){qDebug() << "鼠标中键被双击,本地坐标:" << event->pos() << "鼠标左键被抬起,全局坐标:" << event->globalPos();}qDebug() << "本地坐标:" << event->pos() << "鼠标左键被抬起,全局坐标:" << event->globalPos();ui->label->move(event->x()-ui->label->width()/2,event->y()-ui->label->height()/2);
}
运行效果
2、使用鼠标移动事件完成界面的移动
widget.h代码
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QPoint> //点类
#include <QMouseEvent> //鼠标事件QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();void mousePressEvent(QMouseEvent *event) override; //鼠标点击事件void mouseMoveEvent(QMouseEvent *event) override; //鼠标移动事件private:Ui::Widget *ui;QPoint temp; //辅助向量
};
#endif // WIDGET_H
widget.cpp代码
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);//去除头部this->setWindowFlag(Qt::FramelessWindowHint);
}Widget::~Widget()
{delete ui;
}void Widget::mousePressEvent(QMouseEvent *event) //鼠标点击事件
{temp = event->globalPos() - this->pos(); //得到中间向量//鼠标的当前位置 组件左上角的位置}void Widget::mouseMoveEvent(QMouseEvent *event) //鼠标移动事件
{if(event->button() == Qt::LeftButton){this->move(event->globalPos()- temp); //组件移动位置}else if(event->button() == Qt::RightButton){this->close();}
}
运行效果