main.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include "softnumkeyboardwidget.h"
  2. #include <QDesktopWidget>
  3. #include <QApplication>
  4. #include <QSharedMemory>
  5. int main(int argc, char *argv[])
  6. {
  7. QApplication::addLibraryPath("./plugins");
  8. QSharedMemory *shareMem = new QSharedMemory(QString("SingleInstanceIdentifySoftNumKeyBoard"));
  9. /* if the sharedmemory has not been created, it returns false, otherwise true.
  10. * But if the application exit unexpectedly, the sharedmemory will not detach.
  11. * So, we try twice.
  12. */
  13. volatile short i = 2;
  14. while (i--)
  15. {
  16. if (shareMem->attach(QSharedMemory::ReadOnly)) /* no need to lock, bcs it's read only */
  17. {
  18. shareMem->detach();
  19. }
  20. }
  21. if (shareMem->create(1))
  22. {
  23. QApplication a(argc, argv);
  24. QFont font = a .font();
  25. font.setPointSize(14);
  26. a.setFont(font);
  27. SoftNumKeyboardWidget w;
  28. QDesktopWidget *pDesk = QApplication::desktop();
  29. w.show();
  30. w.move((pDesk->width() - w.width()) / 2, (pDesk->height() - w.height()) / 2);// ĬÈϾÓÖÐ
  31. a.exec();
  32. if (shareMem->isAttached())
  33. shareMem->detach();
  34. delete shareMem;
  35. }
  36. return 0;
  37. }