[SOLVED]Using objective-c class in qt c++
-
wrote on 8 Oct 2014, 11:00 last edited by
Hi,
i want to implement an ios application with qt. I’ve installed qt on mac and i want to use objective-c code in qt c++. to fetch iphone address book, then i want to use it in qt c++For use objective-c class in qt c++, I’ve used the code from this link: http://stackoverflow.com/questions/1061005/calling-objective-c-method-from-c-method
//mainwindow.h public:
int MyObjectDoSomethingWith(void *myObjectInstance, void *parameter);
void someMethod(void *myObjectInstance, void *parameter);//MyObject.h
#import <Foundation/Foundation.h>@interface MyObject : NSObject
// The Objective-C member function you want to call from C++ – (int) doSomethingWith:(void *) aParameter;
@end
//mainwindow.cpp
void MainWindow::someMethod(void *objectiveCObject , void *aParameter)
{ int ret = MyObjectDoSomethingWith(objectiveCObject ,aParameter )
}//MyObject.mm
#import “MyObject.h”@implementation MyObject
// C “trampoline” function to invoke Objective-C method
int MyObjectDoSomethingWith (void *self, void *aParameter)
{ // Call the Objective-C method using Objective-C syntax return [(id) self doSomethingWith:aParameter];
}- (int) doSomethingWith:(void *) aParameter
{ // The Objective-C function you wanted to call from C++. // do work here.. return 21 ; // half of 42
}
@end
//.pro
OBJECTIVE_SOURCES += MyObject.mm
LIBS += -framework Foundation
…Afetr building, i got these errors:
symbol(s) not found for architecture x86_64
linker command failed with exit code 1 (use -v to see invocation)I use qt5.3 on mac, please help me.
- (int) doSomethingWith:(void *) aParameter
-
wrote on 8 Oct 2014, 12:56 last edited by
I found a solution.
1- In QT header file, call a cpp method like:
int MyCPPMethod(void *self, void * aParam)2- Implement the method in .mm file (objective c file)
like this:
//.mm file
@interface MyObject :NSObject
{
...
}
-(int)doSomethingWith:(void *) aParam;
@end@implementation MyObject
int MainWindow::MyCPPMethod(void *self, void * aParam)
{
self = [[MyObject alloc] init];
return [(id) self doSomethingWith:aParam];
}-(int)doSomethingWith:(void *) aParam
{
NSLog(@"Result: %d" , 21);
return 21;
}
@endGood Luck :)
-
wrote on 18 Aug 2015, 21:32 last edited by
Is there a way to block the xCode if you don't download to a Mac? It won't compile on the developer machines that are using windows.
-Jordan
-
Is there a way to block the xCode if you don't download to a Mac? It won't compile on the developer machines that are using windows.
-Jordan
@jman Check my example project. It could be built on other platform:
quickios/examples/quickiosexample at master · benlau/quickios
Method:
- Place those .mm files in "ios {}" block in your .pro file
quickios/quickiosexample.pro at master · benlau/quickios
- Use "#ifdef Q_OS_IOS..#endif" block in C++ code or use the SystemMessenger class from my project:
Example:
quickios/qisystemutils.mm at master · benlau/quickios