2014-03-13 22 views
1

我有一个QApplication其中我有一个自定义QDialog。该对话框为用户提供一组选项,然后通过QProcess启动一个流程。虽然启动的程序仍在运行,但如果关闭的应用程序仍然运行。为了达到这个目的,我根据是否启动了一个进程,重新实现了closeEventQWidgetaccept() ed或ignore()在Qt中隐藏和重新启动QApplication的相同实例

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

任何人都可以帮助我如何做到这一点?

+0

这是否仅仅是为了在关闭应用程序时不终止进程?然后,你可以使用'QProcess :: startDetached'或者类似于这个答案来启动进程分离:https://stackoverflow.com/questions/33874243/qprocessstartdetached-but-hide-console-window –

回答

2

命名互斥量可以用来解决。

这个article是有帮助的。

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; 
} 
2

Pre Qt 5中有一个名为QtSingleApplication的项目,它只允许运行一个应用程序的一个实例,并在用户试图打开另一个应用程序时引发正在运行的应用程序。

如果您针对“qtsingleapplication qt5”执行Google搜索,您会发现更多关于QtSingleApplication与Qt5一起使用的修复信息。

This thread也可能有帮助。

相关问题