#include <QMouseEvent>
#include <QDebug>
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
QCursor cursor;//创建光标对象
cursor.setShape(Qt::OpenHandCursor);
setCursor(cursor);
setMouseTracking(true);//设置鼠标跟踪
}
Widget::~Widget()
{
delete ui;
}
void Widget::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
QCursor cursor;
cursor.setShape(Qt::ClosedHandCursor);
QApplication::setOverrideCursor(cursor);//使鼠标指针暂时改变形状
//这里使用了globalPos()函数来获取鼠标指针的位置,这个位置是指针在桌面上的位置
//因为窗口的位置就是指它在桌面上的位置
offset = event->globalPos()-pos();//获取指针位置和窗口位置的差值
qDebug()<<offset;
}else if (event->button() == Qt::RightButton)
{
QCursor cursor(QPixmap("open.jpg"));
QApplication::setOverrideCursor(cursor);
}
}
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
QApplication::restoreOverrideCursor();//恢复鼠标的指针形状
}
void Widget::mouseDoubleClickEvent(QMouseEvent *event)
{
if (event->buttons() == Qt::LeftButton)
{
if (windowState() != Qt::WindowFullScreen)
{
setWindowState(Qt::WindowFullScreen);
}else setWindowState(Qt::WindowNoState);//否则恢复以前的大小
}
}
//这里表是的是滚轮事件
//这里使用QWheelEvent类的delta()函数获取了滚轮移动的距离,每当滚轮旋转一下,默认是15度,这时delta()函数就会返回15*8即整数120
//当滚轮向远离使用者的方向旋转时,返回正值,反之返回负值,根据返回值来判断滚轮的移动方向,从而进行编辑器中内容的放大或者是缩小
void Widget::wheelEvent(QWheelEvent *event)
{
if (event->delta() > 0 &&!Qt::LeftButton)//表示的是当滚轮远离使用者时
{
ui->textEdit->zoomOut();//进行放大
}else
{
ui->textEdit->zoomIn();
}
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons()&Qt::LeftButton)
{
QPoint temp;
temp = event->globalPos() - offset;
move(temp);//使用鼠标指针当前的位置减去差值,就得到了窗口应该移动的位置
}
}
评论0