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

61 lines
2.7 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类的声明用于管理GUI应用程序的控制流和主要设置
#include "qevent.h" // 包含QEvent类的声明用于处理事件
// 定义一个静态成员变量self用于实现单例模式。初始化为0nullptr
AppInit *AppInit::self = 0;
// AppInit类的构造函数接收一个QObject类型的父对象指针。这里调用了QObject的构造函数。
AppInit::AppInit(QObject *parent) : QObject(parent)
{
}
// AppInit类的事件过滤器函数用于处理特定对象的事件。
bool AppInit::eventFilter(QObject *obj, QEvent *evt)
{
// 将传入的对象转换为QWidget类型以便使用QWidget的成员函数。
QWidget *w = (QWidget *)obj;
// 检查该窗口是否有名为"canMove"的属性并且该属性为true。如果不是则继续调用默认的事件处理。
if (!w->property("canMove").toBool()) {
return QObject::eventFilter(obj, evt);
}
// 定义并初始化静态变量,用于存储鼠标的位置和是否按下了鼠标按钮。
static QPoint mousePoint;
static bool mousePressed = false;
// 将传入的事件转换为QMouseEvent类型以便访问鼠标事件的特定属性。
QMouseEvent *event = static_cast<QMouseEvent *>(evt);
// 检查事件类型是否为鼠标按下事件。
if (event->type() == QEvent::MouseButtonPress) {
// 如果按下的是左键则设置mousePressed为true并计算鼠标点击位置与窗口左上角的相对位置。
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; // 表示事件已被处理
}
}
// 如果事件不是上述任何一种类型,则调用默认的事件处理。
return QObject::eventFilter(obj, evt);
}
// AppInit类的start成员函数用于启动事件过滤器。
void AppInit::start()
{
// 在QApplication对象上安装事件过滤器以便捕获和处理应用程序级别的事件。
qApp->installEventFilter(this);
}