Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Calling an Objective-C class Function in C++ Class or main-window .widget so that the button, i have created using qt widget will use the objective-C Class method

    Mobile and Embedded
    2
    2
    684
    Loading More Posts
    • 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.
    • J
      Jitesh last edited by

      I have one objective-C class which i have created in X-Code.
      let say we call it ObjCClass: corresponding
      ObjCClass.h:
      #import <Foundation/Foundation.h>
      @interface myClass : NSObject
      -(NSString*) doSomething;
      @end
      ObjCClass.m:

      #include "ObjCFunc.h"
      @implementation myClass
      -(NSString*) doSomething
      {
      NSLog (@"Hola Jitesh!!! you are inside Objective C Class");
      NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
      [formatter setDateFormat:@"yyyy"];
      NSDate date;
      NSString
      string = [formatter stringFromDate:date];
      return string
      }
      @end

      Now, I have one qt widget based project in which i have one button and one label. I want when i click on that button it will call ObjCClass method "doSomething" and and the result will show on label.
      so in QT Creator i have:
      button clicked method so what are the steps i need to follow to use that objective c method and include objective c class in qt project.
      Please help:-
      Thanks :):):)

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi and welcome to devnet,

        First thing, rename your mywidget.cpp to mywidget.mm so the compiler will know that you are going write Objective-C++. Next, in your pro file add OBJECTIVE_SOURCES += mywidget.mm and remove the corresponding mywidget.cpp

        Now in mywidget.mm you can include your Objective-C class and use it in your C++ code. You can also take a look at the QtMacExtras module for some inspiration.

        Hope it helps

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • First post
          Last post