1、鼠标事件
实际返回的是X Y坐标,上图左侧全局x,y相对于桌面坐标;右侧相对于窗体坐标。
鼠标按下事件如下:
2、widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
Init();
}
Widget::~Widget()
{
delete ui;
}
void Widget::Init(){
}
void Widget::mouseMoveEvent(QMouseEvent *event){
qDebug()<<"Move Event"<<endl;
qDebug()<<event->globalPos()<<endl;
qDebug()<<event->windowPos()<<endl;
}
void Widget::mousePressEvent(QMouseEvent *event){
qDebug()<<"Press Event"<<endl;
event->button();
if(event->button() == Qt::LeftButton)
{
qDebug()<<"左键按下 val= "<<event->button()<<endl;
}
else if (event->button() == Qt::RightButton) {
qDebug()<<"右键按下val= "<<event->button()<<endl;
}
else if (event->button() == Qt::MiddleButton) {
qDebug()<<"中键按下val= "<<event->button()<<endl;
}
qDebug()<<event->globalPos()<<endl;
qDebug()<<event->windowPos()<<endl;
}
void Widget::mouseReleaseEvent(QMouseEvent *event){
qDebug()<<"Realse Event"<<endl;
qDebug()<<event->globalPos()<<endl;
qDebug()<<event->windowPos()<<endl;
}
void Widget::mouseDoubleClickEvent(QMouseEvent *event)
{
qDebug()<<"DoubleClick"<<endl;
qDebug()<<event->globalPos()<<endl;
qDebug()<<event->windowPos()<<endl;
}
3、widget.h
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
Init();
}
Widget::~Widget()
{
delete ui;
}
void Widget::Init(){
}
void Widget::mouseMoveEvent(QMouseEvent *event){
qDebug()<<"Move Event"<<endl;
qDebug()<<event->globalPos()<<endl;
qDebug()<<event->windowPos()<<endl;
}
void Widget::mousePressEvent(QMouseEvent *event){
qDebug()<<"Press Event"<<endl;
event->button();
if(event->button() == Qt::LeftButton)
{
qDebug()<<"左键按下 val= "<<event->button()<<endl;
}
else if (event->button() == Qt::RightButton) {
qDebug()<<"右键按下val= "<<event->button()<<endl;
}
else if (event->button() == Qt::MiddleButton) {
qDebug()<<"中键按下val= "<<event->button()<<endl;
}
qDebug()<<event->globalPos()<<endl;
qDebug()<<event->windowPos()<<endl;
}
void Widget::mouseReleaseEvent(QMouseEvent *event){
qDebug()<<"Realse Event"<<endl;
qDebug()<<event->globalPos()<<endl;
qDebug()<<event->windowPos()<<endl;
}
void Widget::mouseDoubleClickEvent(QMouseEvent *event)
{
qDebug()<<"DoubleClick"<<endl;
qDebug()<<event->globalPos()<<endl;
qDebug()<<event->windowPos()<<endl;
}
|