Qt World Summit: Submit your Presentation

Adding native OS Specific code to project

  • My project is a Qt application written in C++, running on Windows, OS X and Linux. There is a certain area Qt doesn't cover in its abstraction: Iterating other application windows active on the desktop, and returning their names, their 'handles' and icons.

    I have OS specific code to do just that: Pure Win32/C++ on windows Objective-C on OS X and some X11 c++ code for linux

    I'd like to build my own abstraction that would utilize those OS Specific native snippets to provide it the runtime data it needs.
    So, I'd have to have objective-C code build and run with my OS X build Win32/c++ with windows and similar with Linux

    Let's focus on Mac, just for this example. How do I combine an objective C class into my Qt C++ project? how do I call it from C++? Are there any samples where I can see how it's done?

    Ideally I would like to have the OS specific code in the project, and not build separate shared libraries to be linked in, although this is an option if nothing else can be done.

  • Do it like the trolls do:

    Have a common header file, that defines the platform independent interface, i.e. the classes and methods. Let's say it's called windowinfo.h

    Then create a windowinfo.cpp file, that contains only the implementation that is common to all platforms. Then create windowinfo_mac.cpp, windowinfo_win.cpp and windowinfo_x11.cpp. These three files contain the platform specific code. In your .pro file use scopes to add the respective files:

    HEADERS += windowinfo.h
    SOURCES += windowinfo.cpp
    unix {
    macx {
    SOURCES += windowinfo_mac.cpp
    } else {
    SOURCES += windowinfo_x11.cpp

    win32: SOURCES += windowinfo_win.cpp

    If you do not use Objective-C++ on the Mac, but pure Objective C, you might want to add a .m file too.

    QSettings sources in src/corelib/io are an example for win/mac differences. qdnd_xxx in src/gui/kernel has four variations.

  • Excellent. thanks for the tip. I'll adopt this approach.

Log in to reply