QT运行Objective-C代码

8
我是一位有用的助手,可以为你翻译文本。
我试图在我的 Mac 应用程序中运行本地 Object-C 代码。
我的代码如下:
MainWindow.h:
#ifdef Q_OS_MAC
    #include <Carbon/Carbon.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <stdio.h>

    #include <mach/mach_port.h>
    #include <mach/mach_interface.h>
    #include <mach/mach_init.h>

    #include <IOKit/pwr_mgt/IOPMLib.h>
    #include <IOKit/IOMessage.h>
#endif

MainWindow.cpp:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);


    #ifdef Q_OS_MAC
    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver: self
            selector: @selector(receiveSleepNote:)
            name: NSWorkspaceWillSleepNotification object: NULL];
    #endif

}

#ifdef Q_OS_MAC
- (void) receiveSleepNote: (NSNotification*) note
{
    NSLog(@"receiveSleepNote: %@", [note name]);
}
#endif

但是我遇到了错误,似乎QT无法理解代码结构:

应用程序.cpp:错误:预期外部声明 - (void) receiveSleepNote:(NSNotification *)note ^


混合使用Obj-C和C++有一个相当不错的参考资料:http://philjordan.eu/article/mixing-objective-c-c++-and-objective-c++。 - Kuba hasn't forgotten Monica
1个回答

11
为了使用C++编译Objective-C,您需要将Objective-C代码放在.m或.mm文件中。 相应的头文件可以包含可从C++调用的函数,这些函数的主体可以包含Objective-C代码。 因此,假设我们想调用一个函数以弹出OSX通知。首先是头文件:-
#ifndef __MyNotification_h_
#define __MyNotification_h_

#include <QString>

class MyNotification
{
public:
    static void Display(const QString& title, const QString& text);    
};    

#endif

如您所见,这是一个可以从C++中调用的标准函数头。以下是其实现:

#include "mynotification.h"
#import <Foundation/NSUserNotification.h>
#import <Foundation/NSString.h>

void MyNotification::Display(const QString& title, const QString& text)
{
    NSString*  titleStr = [[NSString alloc] initWithUTF8String:title.toUtf8().data()];
    NSString*  textStr = [[NSString alloc] initWithUTF8String:text.toUtf8().data()];

    NSUserNotification* userNotification = [[[NSUserNotification alloc] init] autorelease];
    userNotification.title = titleStr;
    userNotification.informativeText = textStr;

    [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:userNotification];
}

这个实现包含Objective-C,由于它的.mm文件扩展名,编译器会正确处理它。

请注意,在您提供问题的示例中,您需要考虑代码正在做什么,特别是在使用“self”时,因为我期望它将需要引用Objective-C类,而不是C++类。


在我的Qt应用程序中,[NSUserNotificationCenter defaultUserNotificationCenter]总是返回nil。你知道可能是什么问题吗?我正在使用OS X 10.10上的Qt 5.4.1。 - psyched
@user1113852 对于您提供的这些信息,我无法给出明确的答案。我建议您发布一个新的SO问题,以便得到更好的帮助。 - TheDarkKnight
我已经发布了这个问题的相反类型 -- 如何在Objective C Cocoa应用程序中包含和调用Qt/C++ Dylib。截至2015年12月9日,我仍然没有得到答案。 - Volomike
1
这是从C++调用Objective-C的最佳方式,SO上有一个类似的答案获得了很多赞,但过于复杂。 - Ali
当我复制完全相同的代码并编译时,会出现以下错误信息:Undefined symbols for architecture x86_64: "_OBJC_CLASS_$_NSUserNotificationCenter", "_OBJC_CLASS_$_NSUserNotification" "_OBJC_CLASS_$_NSString", referenced from:objc-class-ref in mynotification.o "_objc_msgSend", "_objc_autorelease", "_objc_alloc", referenced from: MyNotification::Display(QString const&, QString const&) in mynotification.o clang: error: linker command failed with exit code 1 (use -v to see invocation) - jl303
1
看起来你没有链接到 Foundation 框架。 - TheDarkKnight

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接