如何在 QT 应用程序中处理来自 NSNotificationCenter 的通知?

Posted

技术标签:

【中文标题】如何在 QT 应用程序中处理来自 NSNotificationCenter 的通知?【英文标题】:How to process notifications from NSNotificationCenter in QT application? 【发布时间】:2013-07-02 12:20:33 【问题描述】:

有一个简单的 QT 控制台应用程序,带有 QCoreApplication。 是否可以在另一个线程中处理来自通知中心的通知?例如创建一个线程并连接该线程的循环来处理通知?或扩展 QT 应用程序以向我发送此通知?

【问题讨论】:

【参考方案1】:

Qt 本身的源代码中有很多例子。(我在这里做了“git grep NSNotificationCenter”。)

至少您可以检查如下解决方案: https://github.com/qtproject/qtbase/blob/stable/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm

L105-L109:

[[NSNotificationCenter defaultCenter]
            addObserver:self
               selector:@selector(updateScreens:)
                   name:NSApplicationDidChangeScreenParametersNotification
                 object:NSApp];

L130:

[[NSNotificationCenter defaultCenter] removeObserver:self];

【讨论】:

以上是关于如何在 QT 应用程序中处理来自 NSNotificationCenter 的通知?的主要内容,如果未能解决你的问题,请参考以下文章