Qt下单进程最简单的方式
// An highlighted block
bool MainAdapt::singleExe()
{
QSharedMemory* g_pShareMem = new QSharedMemory(QString("centralMoniSystem"));
int nIndex = 2;
while (nIndex--)
{
//attach(AccessMode mode = ReadWrite) 默认为可读写
if (g_pShareMem->attach(QSharedMemory::ReadOnly))
{
g_pShareMem->detach();
}
}
if (g_pShareMem->create(1)) {
return true;
}
else
{
QTimer::singleShot(500, [=] {
QMessageBox::about(nullptr, QString::fromLocal8Bit("程序已启动"), QString::fromLocal8Bit("当前程序已启动"));
qApp->quit();
});
return false;
}
}
使用
QApplication a(argc, argv);
if(!singleExe())
return;
a.exec();
return 0;