Congratulations to our 2022 Qt Champions!

How to merge cocoa into Qt?

  • Hi

    I am working on Qt4.7 on mac os 10.5.8, i want to develop an application in which i have to merge Cocoa code , i have heard that it is possible to merge cocoa with Qt. Can you provide me an example that how can i integrate cocoa classes(.m,.h and .xib) with Qt. How can i make the object of (.xib) file and call it through my Qt application.


  • There are OBJECTIVE_HEADERS and OBJECTIVE_SOURCES variables to qmake (though undocumente), you can use those in order to include your Objective-C sources into your Qt project.

    To put the .xib file into the right place in the application bundle, have a look at the wiki article "How to add resource files to an OS X application bundle":, just adapt the variable names etc.

    To call Cocoa stuff in your regular C++ code: Just go ahead, you can happily mix them. See for an example or have a look at the Qt sources, the Trolls are using Cocoa themselves of course :)

  • Thanks for the reply, i have some doubt, i had a cocoa application in which (MainMenu.xib) file is launched on application load, and in cocoa application it is called in its bq. main.m class like this:

    @#import <Cocoa/Cocoa.h>

    int main(int argc, char *argv[])
    return NSApplicationMain(argc, (const char **) argv);

    How can i call this (MainMenu.xib) file using Qt in my main.cpp at application launch.


    bq. To put the .xib file into the right place in the application bundle, have a look at the wiki article

    but in this there is an example to add qml file:

    you can let qmake do all the copy work for you. Just add a snippet like this to your .pro file:

    bq. APP_QML_FILES.files = path/to/file1.qml path/to/file2.qml
    APP_QML_FILES.path = Contents/Resources

    is this the same way to add .xib file of cocoa.

  • For the application:
    I'm not sure you can have a NSApplicationMain and a QApplication simultaneously, I'm pretty sure you cannot (as NSApplicationMain seem to block until the application ends, which is the same as QApplication's exec).

    You can use alien (native) widgets together with Qt, though. Have a look at the "Alien widgets on Mac": blog entry on Qt Labs.

    For the other way round (using Qt widgets in native Mac), have a look at "QMacNativeWidget":

    It's unclear what you're trying to do so far.

    For copying the .xib:
    The snippet is ok, just replace the paths with yours and replace QML with XIB, if you want. It's just names, no special meanings. The only constraint is, you must be consistent and use the same variable name :)

  • Thanks for the reply, actually i am trying to show MainMenu.xib file at application launch, but i didnt get it how can i do this by using Qt QApplication.

  • I never worked with native Cocoa/XIB files, so I cannot comment on this one. I would dig deeper into the alien widgets topic, though. Maybe "QMacCocoaViewContainer": can be of help for you here. It contains an example for including a NSSearchField into a Qt widget.

Log in to reply