You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
smms/supermarket/appinit.cpp

64 lines
2.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "appinit.h" // 包含AppInit类的声明
#include "qapplication.h" // 包含QApplication类的声明用于访问应用程序的全局状态
#include "qevent.h" // 包含QEvent类的声明用于处理事件
// AppInit类的单例实例
AppInit *AppInit::self = 0;
// AppInit类的构造函数接收一个QObject指针作为父对象
AppInit::AppInit(QObject *parent) : QObject(parent)
{
// 构造函数中没有执行任何操作只是简单地调用了QObject的构造函数
}
// AppInit类的事件过滤器函数
bool AppInit::eventFilter(QObject *obj, QEvent *evt)
{
// 将QObject转换为QWidget因为我们需要使用QWidget的属性和方法
QWidget *w = (QWidget *)obj;
// 检查窗口是否设置了"canMove"属性为true如果为false则不处理事件
if (!w->property("canMove").toBool()) {
return QObject::eventFilter(obj, evt);
}
// 定义两个静态变量来存储鼠标的位置和按下状态
static QPoint mousePoint;
static bool mousePressed = false;
// 将QEvent转换为QMouseEvent以便访问鼠标事件的特定信息
QMouseEvent *event = static_cast<QMouseEvent *>(evt);
// 检查事件类型是否为鼠标按下事件
if (event->type() == QEvent::MouseButtonPress) {
// 如果按下的是左键,则记录鼠标按下状态和鼠标的初始位置
if (event->button() == Qt::LeftButton) {
mousePressed = true;
mousePoint = event->globalPos() - w->pos();
return true; // 事件已处理
}
}
// 检查事件类型是否为鼠标释放事件
else if (event->type() == QEvent::MouseButtonRelease) {
mousePressed = false;
return true; // 事件已处理
}
// 检查事件类型是否为鼠标移动事件
else if (event->type() == QEvent::MouseMove) {
// 如果鼠标按下并且当前按下的是左键,则移动窗口到新的位置
if (mousePressed && (event->buttons() && Qt::LeftButton)) {
w->move(event->globalPos() - mousePoint);
return true; // 事件已处理
}
}
// 如果事件不是我们关心的类型则使用QObject的默认事件过滤器
return QObject::eventFilter(obj, evt);
}
// AppInit类的启动函数
void AppInit::start()
{
// 在应用程序中安装事件过滤器
qApp->installEventFilter(this);
}