.h文件
#ifndef MYCLASS_H
#define MYCLASS_H
class MyClass
{
private:
void *workspaceWatcher;
public:
MyClass();
~MyClass();
void test();
};
#endif // MYCLASS_H
.mm文件
#include "MyClass.h"
#include <Foundation/Foundation.h>
#include <AppKit/AppKit.h>
#include <QDebug>
@interface MDWorkspaceWatcher : NSObject {
MyClass *myClass;
}
- (id)initWithMyClass:(MyClass *)aMyClass;
@end
@implementation MDWorkspaceWatcher
- (id)initWithMyClass:(MyClass *)aMyClass {
if ((self = [super init])) {
myClass = aMyClass;
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
selector:@selector(didActivateApp:)
name:NSWorkspaceDidWakeNotification
object:nil];
return self;
}
}
- (void)dealloc {
[[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
[super dealloc];
}
- (void)didActivateApp:(NSNotification *)notification {
myClass->test();
NSLog(@"唤醒");
}
@end
MyClass::MyClass()
{
this->workspaceWatcher = [[MDWorkspaceWatcher alloc] initWithMyClass:this];
}
MyClass::~MyClass() {
[(MDWorkspaceWatcher *)this->workspaceWatcher release];
}
void MyClass::test()
{
qDebug() << "123";
}
不要忘了在.pro文件中添加:
OBJECTIVE_SOURCES +=
LIBS += -framework AppKit -framework Foundation
否则在.mm文件中无法正常引用OC库。
网友评论