Get the OS platform name in C++



  • Hey, I was wondering if it's possible to get the platform name from c++ like in QML with Qt.platform.os?

    The class QQmlPlatform seems to be private sadly, of course I could just copy the code from the method but that is not considered good practice in my opinion...
    @
    QString QQmlPlatform::os()
    {
    #if defined(Q_OS_ANDROID)
    return QLatin1String("android");
    #elif defined(Q_OS_BLACKBERRY)
    return QLatin1String("blackberry");
    #elif defined(Q_OS_IOS)
    return QLatin1String("ios");
    #elif defined(Q_OS_MAC)
    return QLatin1String("osx");
    #elif defined(Q_OS_WINCE)
    return QLatin1String("wince");
    #elif defined(Q_OS_WIN)
    return QLatin1String("windows");
    #elif defined(Q_OS_LINUX)
    return QLatin1String("linux");
    #elif defined(Q_OS_UNIX)
    return QLatin1String("unix");
    #else
    return QLatin1String("unknown");
    #endif
    }
    @

    another way would be to get the string from thr QML engine like this:
    @
    QString os = engine->evaluate("Qt.platform.os").toString();
    @

    that works and I have a pointer to the QQmlEngine anyways, but seems like a lot of hassle to run the actual C++ methode QQmlPlatform::os like this.. :D





  • hey thanks, yeah I just found that myself, is that always the same string as the QML version? because the doc says nothing about mobile platforms, also it returns "Cocoa" for Mac OS X and not "osx" like QQmlPlatform::os() does, but I might use that instead..



  • In the widgets world the platform is defined during build configuration.
    The qconfig.h file has a define QT_QPA_DEFAULT_PLATFORM_NAME that keeps the platform name.
    And this name is used to create a platform specific plugin.
    For MacOSX the plugin directory is
    Qt5.2.1/5.2.1/clang_64/plugins/platforms/

    IMHO, if you need a unified solution then it is better to copy QQmlPlatform::os() to you project.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.