Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Get the OS platform name in C++
Forum Updated to NodeBB v4.3 + New Features

Get the OS platform name in C++

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 4.0k Views 1 Watching
  • 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.
  • X Offline
    X Offline
    Xander84
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on last edited by
      #2

      Take a look on "QGuiApplication":http://qt-project.org/doc/qt-5/qguiapplication.html#platformName-prop

      1 Reply Last reply
      0
      • X Offline
        X Offline
        Xander84
        wrote on last edited by
        #3

        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..

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andreyc
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved