#include "mainwindow.h" #include #include #include #include #include #include #include int main(int argc, char *argv[]) { QApplication a(argc, argv); // 设置应用程序图标 a.setWindowIcon(QIcon(":/common/resource/logo.png")); // 使用应用程序名称作为唯一标识符 const QString appKey = "LapWeldApp_SingleInstance_Key"; // 创建系统信号量,用于同步访问共享内存 QSystemSemaphore semaphore(appKey + "_semaphore", 1); semaphore.acquire(); // 获取信号量 // 创建共享内存对象 QSharedMemory sharedMemory(appKey + "_memory"); bool isRunning = false; // 尝试附加到现有的共享内存 if (sharedMemory.attach()) { // 如果能够附加,说明已有实例在运行 isRunning = true; } else { // 尝试创建新的共享内存 if (!sharedMemory.create(1)) { // 创建失败,可能是因为已经存在 qDebug() << "Unable to create shared memory segment:" << sharedMemory.errorString(); isRunning = true; } } semaphore.release(); // 释放信号量 if (isRunning) { // 已有实例在运行,显示提示信息并退出 QMessageBox::information(nullptr, QObject::tr("应用程序已运行"), QObject::tr("编织袋拆垛应用程序已经在运行中,请勿重复启动!"), QMessageBox::Ok); return 0; } // 没有其他实例运行,正常启动应用程序 MainWindow w; w.show(); int result = a.exec(); // 应用程序退出时,清理共享内存 if (sharedMemory.isAttached()) { sharedMemory.detach(); } return result; }