遵循从此处链接的代码:Trap click event on dock icon using Qt on Mac ,我尝试直接在 QApplication 的构造函数中调用以下方法来接收停靠图标单击事件的通知:
[[NSAppleEventManager sharedAppleEventManager]
setEventHandler: m_dockIconClickEventHandler
andSelector: @selector(handleDockClickEvent:withReplyEvent:)
forEventClass: kCoreEventClass
andEventID: kAEReopenApplication];
如果我直接调用它,我不会收到此事件的通知。但是,如果我使用 QTimer::singleShot
调用它,并延迟 5000 毫秒,我就可以很好地收到通知。
此外,根据 Qt 文档,“一旦处理完窗口系统事件队列中的所有事件,超时间隔为 0 的 QTimer 将超时。”所以我尝试了0毫秒,但没有成功。 1 或以上似乎。
为什么我需要等待,有什么比延迟 n 毫秒更好的方法来处理这种情况?
最佳答案
当您的应用程序 exec() 被调用时,Qt 设置自己的事件处理程序,因此您的处理程序将被覆盖。 您可以在构造函数中使用
connect(this, SIGNAL(setupDockEventMonitor()), SLOT(onSetupDockEventMonitor()), Qt::QueuedConnection);
emit setupDockEventMonitor();
并在 onSetupDockEventMonitor() 中安装此事件处理程序。
关于cocoa - 在 Qt 中订阅 Apple 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8018921/