gpt4 book ai didi

c++ - 在 Qt 中隐藏和重新启动同一个 QApplication 实例

转载 作者:可可西里 更新时间:2023-11-01 11:18:50 27 4
gpt4 key购买 nike

我有一个 QApplication,其中有一个自定义的 QDialog。该对话框为用户提供一组选项,然后通过 QProcess 启动一个进程。虽然启动的进程仍在运行,但应用程序如果已关闭则仍必须运行。为此,我重新实现了 QWidgetcloseEventaccept()ed 或 ignore()ed基于进程是否启动的事件。

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

谁能帮我实现这个目标?

最佳答案

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

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

关于c++ - 在 Qt 中隐藏和重新启动同一个 QApplication 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22378529/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com