Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在Qt中隐藏和重新启动QApplication的同一实例_C++_Windows_Qt_Qapplication - Fatal编程技术网

C++ 在Qt中隐藏和重新启动QApplication的同一实例

C++ 在Qt中隐藏和重新启动QApplication的同一实例,c++,windows,qt,qapplication,C++,Windows,Qt,Qapplication,我有一个QApplication,其中有一个自定义QDialog。该对话框为用户提供一组选项,然后通过QProcess启动一个流程。当启动的进程仍在运行时,如果关闭,应用程序仍必须运行。为了实现这一点,我重新实现了QWidget的closeEvent和accept()ed或ignore()ed,根据是否启动了进程来设置事件 在closeEvent()函数中,我隐藏了我的QDialog。这样,对于用户来说,应用程序将关闭(但它将在任务管理器中运行)。我希望用户通过再次运行程序来重新启动应用程序。在

我有一个
QApplication
,其中有一个自定义
QDialog
。该对话框为用户提供一组选项,然后通过
QProcess
启动一个流程。当启动的进程仍在运行时,如果关闭,应用程序仍必须运行。为了实现这一点,我重新实现了
QWidget
closeEvent
accept()
ed或
ignore()
ed,根据是否启动了进程来设置事件

closeEvent()
函数中,我隐藏了我的
QDialog
。这样,对于用户来说,应用程序将关闭(但它将在任务管理器中运行)。我希望用户通过再次运行程序来重新启动应用程序。在这一点上,我需要找出另一个实例已经在运行,并且该实例应该出现在前台


有谁能帮助我实现这一点吗?

在Qt 5之前,有一个名为QtSingleApplication的项目,它只允许一个应用程序的一个实例运行,并且如果用户试图打开另一个实例,它将引发正在运行的应用程序

如果您在谷歌上搜索“qtsingleapplication qt5”,您将找到有关qtsingleapplication与qt5一起使用的修复程序的更多信息


也可能有帮助。

在Qt 5之前,有一个名为QtSingleApplication的项目,它只允许一个应用程序的一个实例运行,如果用户试图打开另一个实例,它将引发正在运行的应用程序

如果您在谷歌上搜索“qtsingleapplication qt5”,您将找到有关qtsingleapplication与qt5一起使用的修复程序的更多信息


也可能有帮助。

可以使用命名互斥来解决此问题

这是有益的

WINAPI WinMain(
  HINSTANCE, HINSTANCE, LPSTR, int)
{
  try {
    // Try to open the mutex.
    HANDLE hMutex = OpenMutex(
      MUTEX_ALL_ACCESS, 0, "MyApp1.0");

    if (!hMutex)
      // Mutex doesn’t exist. This is
      // the first instance so create
      // the mutex.
      hMutex = 
        CreateMutex(0, 0, "MyApp1.0");
    else
      // The mutex exists so this is the
      // the second instance so return.
      return 0;

    Application->Initialize();
    Application->CreateForm(
      __classid(TForm1), &Form1);
    Application->Run();

    // The app is closing so release
    // the mutex.
    ReleaseMutex(hMutex);
  }
  catch (Exception &exception) {
    Application->
      ShowException(&exception);
  }
  return 0;
}

可以使用命名互斥来解决

这是有益的

WINAPI WinMain(
  HINSTANCE, HINSTANCE, LPSTR, int)
{
  try {
    // Try to open the mutex.
    HANDLE hMutex = OpenMutex(
      MUTEX_ALL_ACCESS, 0, "MyApp1.0");

    if (!hMutex)
      // Mutex doesn’t exist. This is
      // the first instance so create
      // the mutex.
      hMutex = 
        CreateMutex(0, 0, "MyApp1.0");
    else
      // The mutex exists so this is the
      // the second instance so return.
      return 0;

    Application->Initialize();
    Application->CreateForm(
      __classid(TForm1), &Form1);
    Application->Run();

    // The app is closing so release
    // the mutex.
    ReleaseMutex(hMutex);
  }
  catch (Exception &exception) {
    Application->
      ShowException(&exception);
  }
  return 0;
}

这是否只是为了在关闭应用程序时不终止进程?然后,您可以使用
QProcess::startDetached
,或类似于下面的回答,以分离方式启动流程:这只是为了在关闭应用程序时不终止流程吗?然后,您可以使用
QProcess::startDetached
,或类似于下面的回答,启动分离的流程: