QT运行Objective-C代码 [英] QT run objective-c code

查看:127
本文介绍了QT运行Objective-C代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Mac应用程序上运行本机对象c代码.

I'm trying to run native object-c code on my Mac application.

我的代码如下:

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无法理解代码结构:

But am getting errors that seems that QT does not understand the code structure:

application.cpp:错误:预期的外部声明 -(void)receiveSleepNote:(NSNotification *)note ^

application.cpp: error: expected external declaration - (void) receiveSleepNote: (NSNotification*) note ^

推荐答案

为了使用C ++编译Objective-c,您需要在.m或.mm文件中包含Objective-c代码.

In order to compile objective-c with C++, you need to have the objective-c code in a .m or .mm file.

然后,附带的标头可以包含可以从C ++调用的函数,而这些函数的主体可以包含Objective-C代码.

The accompanying header can then contain functions that can be called from C++ and the body of those functions can contain objective-c code.

例如,假设我们要调用一个函数来弹出OSX通知.从标题开始:-

So let's say, for example, we wanted to call a function to pop up an OSX notification. Start with the header: -

#ifndef __MyNotification_h_
#define __MyNotification_h_

#include <QString>

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

#endif

如您所见,这是标头中的常规函数​​,可以从C ++调用.这是实现:-

As you can see, this is a regular function in a header that can be called from C++. Here's the implementation:-

#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文件扩展名,编译器将能够正确处理此问题.

The implementation contains objective-c and due to its .mm file extension, the compiler will handle this correctly.

请注意,在问题中提供的示例中,您需要考虑代码的作用.尤其是在使用' self '时,因为我希望这需要引用一个Objective-C类,而不是C ++类.

Note that in the example you provide in the question, you need to think about what the code is doing; especially when using 'self', as I expect that would need to refer to an Objective-C class, not a C++ class.

这篇关于QT运行Objective-C代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆