Windows 和 QWidget::activateWindow() 中的活动窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3316350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Active windows in Windows and QWidget::activateWindow()
提问by Jake Petroules
The Qt documentation for QWidget::activateWindow()
states:
状态的 Qt 文档QWidget::activateWindow()
:
On Windows, if you are calling this when the application is not currently the active one then it will not make it the active window. It will change the color of the taskbar entry to indicate that the window has changed in some way. This is because Microsoft does not allow an application to interrupt what the user is currently doing in another application.
在 Windows 上,如果您在应用程序当前不是活动窗口时调用它,那么它不会使其成为活动窗口。它将更改任务栏条目的颜色以指示窗口已以某种方式更改。这是因为 Microsoft 不允许应用程序中断用户当前在另一个应用程序中执行的操作。
However, Skype appears to defy this rule. If Skype is running but is not the active application, I can launch it from the start menu and it brings the existing instance to the foreground, activates it and grabs input focus.
然而,Skype 似乎无视这一规则。如果 Skype 正在运行但不是活动应用程序,我可以从开始菜单启动它,它将现有实例带到前台,激活它并获取输入焦点。
And how can Ido this?
我该怎么做?
回答by Jake Petroules
(NOTE: This is specific to how QtSingleApplication works)
(注意:这特定于 QtSingleApplication 的工作方式)
The solution is stupidly simple for my issue. Simply call AllowSetForegroundWindow(ASF_ANY);
at the beginning of the application, and the original process will thus be allowed to bring itself to the foreground by use of SetForegroundWindow()
. No strange hacks, just one line of code to add and no need to modify QtSingleApplication either.
对于我的问题,解决方案非常简单。只需AllowSetForegroundWindow(ASF_ANY);
在应用程序开始时调用,原始进程就可以通过使用 将自己带到前台SetForegroundWindow()
。没有奇怪的技巧,只需添加一行代码,也无需修改 QtSingleApplication。
回答by sje397
I don't think you can do it reliably with the Qt API alone.
我认为单独使用 Qt API 无法可靠地做到这一点。
There are multiple solutions for windows. E.g. here, and here, and here.
The method I've used before is to declare a shared memory section, and write the application's window handle there. Later, when a second instance of your program is started, you can find the window handle of the first and activate it.
我之前使用的方法是声明一个共享内存部分,并在那里写入应用程序的窗口句柄。稍后,当您的程序的第二个实例启动时,您可以找到第一个的窗口句柄并激活它。
I don't think you have the issue of windows preventing you from doing this in this case, because your second instance is the active application, so it is allowed to 'pass focus' to other windows.
在这种情况下,我认为您不存在阻止您执行此操作的窗口问题,因为您的第二个实例是活动应用程序,因此可以将焦点“传递给其他窗口”。
回答by Mason Zhang
Use Single Applicationin Qt Solutions
在 Qt 解决方案中使用 单个应用程序
For some applications it is useful or even critical that they are started only once by any user. Future attempts to start the application should activate any already running instance, and possibly perform requested actions, e.g. loading a file, in that instance.
对于某些应用程序,任何用户仅启动一次它们是有用的,甚至是至关重要的。将来启动应用程序的尝试应该激活任何已经运行的实例,并可能在该实例中执行请求的操作,例如加载文件。
回答by have.a.test
you can set the setWindowOpacity from 0 to 1 .the only thing is you may open it all the time
您可以将 setWindowOpacity 从 0 设置为 1 。唯一的事情是您可以一直打开它