• Qt单一应用实例判断


            原本项目中使用QSharedMemory的方法来判断当前是否已存在运行的实例,但在MacOS上,当程序异常崩溃后,QSharedMemory没有被正常销毁,导致应用程序无法再次被打开。

            对此,Qt assistant中有相关说明:

            摘抄 qt-solutions/qtsingleapplication at master · qtproject/qt-solutions (github.com) 实现了一个single application的类,记录下来:

    main.cpp

    1. #include "mainwindow.h"
    2. #include "singleapplication.h"
    3. int main(int argc, char *argv[])
    4. {
    5. CSingleApplication a(argc, argv);
    6. if (a.isRunning()) {
    7. return 0;
    8. }
    9. MainWindow w;
    10. w.show();
    11. a.setActivationWindow(&w);
    12. return a.exec();
    13. }

    singleapplication.cpp

    1. #include "singleapplication.h"
    2. #include
    3. #include
    4. #include
    5. #include
    6. CSingleApplication::CSingleApplication(int& argc, char** argv)
    7. : QApplication(argc, argv)
    8. {
    9. m_strServerName = QFileInfo(QCoreApplication::applicationFilePath()).fileName();
    10. initLocalConn();
    11. }
    12. CSingleApplication::~CSingleApplication()
    13. {
    14. }
    15. bool CSingleApplication::isRunning()
    16. {
    17. return m_isRunning;
    18. }
    19. void CSingleApplication::setActivationWindow(QWidget* w)
    20. {
    21. m_pActivateWin = w;
    22. }
    23. void CSingleApplication::initLocalConn()
    24. {
    25. m_isRunning = false;
    26. QLocalSocket socket;
    27. socket.connectToServer(m_strServerName);
    28. if (socket.waitForConnected(200)) {
    29. m_isRunning = true;
    30. return;
    31. }
    32. createLocalServer();
    33. }
    34. void CSingleApplication::createLocalServer()
    35. {
    36. m_pLocalServer = new QLocalServer(this);
    37. connect(m_pLocalServer, &QLocalServer::newConnection, this, &CSingleApplication::onNewConnection);
    38. if (!m_pLocalServer->listen(m_strServerName)) {
    39. if (QAbstractSocket::AddressInUseError == m_pLocalServer->serverError()) {
    40. QLocalServer::removeServer(m_strServerName);
    41. m_pLocalServer->listen(m_strServerName);
    42. }
    43. }
    44. }
    45. void CSingleApplication::activateWindow()
    46. {
    47. if (nullptr != m_pActivateWin) {
    48. m_pActivateWin->show();
    49. m_pActivateWin->raise();
    50. m_pActivateWin->activateWindow();
    51. }
    52. }
    53. void CSingleApplication::onNewConnection()
    54. {
    55. if (nullptr == m_pLocalServer) {
    56. return;
    57. }
    58. QLocalSocket* socket = m_pLocalServer->nextPendingConnection();
    59. if (nullptr != socket) {
    60. socket->waitForReadyRead(300);
    61. delete socket;
    62. activateWindow();
    63. }
    64. }

    singleapplication.h

    1. #ifndef CSINGLEAPPLICATION_H
    2. #define CSINGLEAPPLICATION_H
    3. #include
    4. class QLocalServer;
    5. class CSingleApplication : public QApplication
    6. {
    7. public:
    8. CSingleApplication(int& argc, char** argv);
    9. virtual ~CSingleApplication() override;
    10. public:
    11. bool isRunning();
    12. void setActivationWindow(QWidget* w);
    13. private:
    14. bool m_isRunning = false;
    15. QWidget* m_pActivateWin = nullptr;
    16. QString m_strServerName;
    17. QLocalServer* m_pLocalServer = nullptr;
    18. void initLocalConn();
    19. void createLocalServer();
    20. void activateWindow();
    21. private slots:
    22. void onNewConnection();
    23. };
    24. #endif // CSINGLEAPPLICATION_H

  • 相关阅读:
    从Github上整理下来的《Java面试神技》
    力扣:1592. 重新排列单词间的空格
    【Html】用CSS定义咖啡 - 咖啡配料展示
    Centos7 安装 Mysql
    3 redis实现一个消息中间件
    静态HTML个人音乐网页 大学生网页设计作业 简单音乐娱乐网页制作 DW个人网站模板下载 大学生简单音乐网页作品代码
    react 项目商城中,显示或者隐藏组件(锚点)
    endnote21软件 web page引用 求解答
    Automated Feature Engineering In AutoML
    最全BAT大型互联网公司面试题整理,没有之一
  • 原文地址:https://blog.csdn.net/zhango5/article/details/133691576