美文网首页Qt
Qt实现监听Mac系统唤醒通知

Qt实现监听Mac系统唤醒通知

作者: Sultan | 来源:发表于2019-08-05 11:19 被阅读0次

.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库。

相关文章

网友评论

    本文标题:Qt实现监听Mac系统唤醒通知

    本文链接:https://www.haomeiwen.com/subject/wjipdctx.html