C++ Qt中的MAC OS X用户通知
我在qt中使用NSUserNotification和NSUserNotificationCenter实现了UserNotificationAlert。我已经编写了在单击actionButton时执行操作的代码,但不幸的是它没有得到执行。我有以下代码 XYZ.h XYZ.mmC++ Qt中的MAC OS X用户通知,c++,objective-c,macos,qt,nsusernotification,C++,Objective C,Macos,Qt,Nsusernotification,我在qt中使用NSUserNotification和NSUserNotificationCenter实现了UserNotificationAlert。我已经编写了在单击actionButton时执行操作的代码,但不幸的是它没有得到执行。我有以下代码 XYZ.h XYZ.mm namespace ABC { void sendUserAlert(const QString &title, const QString &subTitle, const QString &ac
namespace ABC {
void sendUserAlert(const QString &title, const QString &subTitle, const QString &actionButtonTitle, const QString &otherButtonTitle)
{
@autoreleasepool {
NSUserNotification *notification = [[NSUserNotification alloc] init];
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
[notification setTitle: title.toNSString()];
if( !subTitle.isEmpty() ) {
[notification setSubtitle: subTitle.toNSString()];
}
if( !actionButtonTitle.isEmpty()) {
[notification setActionButtonTitle: actionButtonTitle.toNSString()];
}
if ( !otherButtonTitle.isEmpty()) {
[notification setOtherButtonTitle: otherButtonTitle.toNSString()];
}
[center deliverNotification:notification];
}
}
}
Delegate.mm
@interface HPObserver : NSObject<NSUserNotificationCenterDelegate>
- (id) init;
- (void) dealloc;
- (void) applicationDidFinishLaunching: (NSNotification *)aNotification;
@end
@implementation HPObserver
- (id) init
{
self = [super init];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(applicationDidFinishLaunching:)
name: NSApplicationDidFinishLaunchingNotification
object: NSApp];
return self;
}
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
- (void) applicationDidFinishLaunching:(NSNotification *)aNotification
{
@autoreleasepool {
NSUserNotification *notification = [[aNotification userInfo] objectForKey:NSApplicationLaunchUserNotificationKey];
if (notification.activationType == NSUserNotificationActivationTypeActionButtonClicked) {
NSlog(@"Hello World");
}
}
}
将显示userNotification,但当我单击“是”按钮时,没有任何操作。我认为我缺少将委托连接到主功能的桥梁,但我不确定如何进行。请帮我解决这个问题。Objective-C标记的用途是什么?@EI Tomato:我使用Objective C代码显示我的用户通知,我相信代理部分有错误,但我无法准确地找出它。.Objective-C标记的用途是什么?@EI Tomato:我使用Objective C代码显示我的用户通知UserNotification,我相信委托部分有一个错误,但我无法准确地找出它。。
@interface HPObserver : NSObject<NSUserNotificationCenterDelegate>
- (id) init;
- (void) dealloc;
- (void) applicationDidFinishLaunching: (NSNotification *)aNotification;
@end
@implementation HPObserver
- (id) init
{
self = [super init];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(applicationDidFinishLaunching:)
name: NSApplicationDidFinishLaunchingNotification
object: NSApp];
return self;
}
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
- (void) applicationDidFinishLaunching:(NSNotification *)aNotification
{
@autoreleasepool {
NSUserNotification *notification = [[aNotification userInfo] objectForKey:NSApplicationLaunchUserNotificationKey];
if (notification.activationType == NSUserNotificationActivationTypeActionButtonClicked) {
NSlog(@"Hello World");
}
}
}
using namespace ABC;
int main(int argc, char **argv)
{
QString title = QObject::tr("Do you want to close the window");
QString actionButtonTitle = QObject::tr("Yes");
QString otherButtonTitle = QObject::tr("No");
ABC::sendUserAlert(title,subTitle,actionButtonTitle,otherButtonTitle);
}