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
,或类似于下面的回答,启动分离的流程: