Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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 :):):)


  • Lifetime Qt Champion

    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


Log in to reply