Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. [SOLVED]Using objective-c class in qt c++

[SOLVED]Using objective-c class in qt c++

Scheduled Pinned Locked Moved Mobile and Embedded
4 Posts 3 Posters 13.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hasti66
    wrote on 8 Oct 2014, 11:00 last edited by
    #1

    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.

    Hasti Ranjkesh

    1 Reply Last reply
    0
    • H Offline
      H Offline
      hasti66
      wrote on 8 Oct 2014, 12:56 last edited by
      #2

      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;
      }
      @end

      Good Luck :)

      Hasti Ranjkesh

      1 Reply Last reply
      1
      • J Offline
        J Offline
        jman
        wrote on 18 Aug 2015, 21:32 last edited by
        #3

        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

        B 1 Reply Last reply 20 Aug 2015, 05:45
        0
        • J jman
          18 Aug 2015, 21:32

          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

          B Offline
          B Offline
          benlau
          Qt Champions 2016
          wrote on 20 Aug 2015, 05:45 last edited by
          #4

          @jman Check my example project. It could be built on other platform:

          quickios/examples/quickiosexample at master · benlau/quickios

          Method:

          1. Place those .mm files in "ios {}" block in your .pro file

          quickios/quickiosexample.pro at master · benlau/quickios

          1. 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

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved