QT windows下只允许运行一个实例
main.cpp
int main(int argc, char *argv[]) { QApplication a(argc, argv); QSharedMemory *shareMem = new QSharedMemory(QString("SingleInstanceIdentify")); /* if the sharedmemory has not been created, it returns false, otherwise true. * But if the application exit unexpectedly, the sharedmemory will not detach. * So, we try twice. */ volatile short i = 2; while (i--) { if (shareMem->attach(QSharedMemory::ReadOnly)) /* no need to lock, bcs it's read only */ { shareMem->detach(); } } if (shareMem->create(1)) { MainWindow w; w.show(); a.exec(); if (shareMem->isAttached()) shareMem->detach(); delete shareMem; } return 0; }

浙公网安备 33010602011771号