views:

944

answers:

3

Hi all,

Is there a (Qt) way to determine the platform a Qt application is running on at runtime?

+4  A: 

There are macro that are defined on the corresponding platforms:

Q_WS_X11
Q_WS_MAC
Q_WS_QWS
Q_WS_WIN

For more informations there is the QSysInfo class.

Georg
Nope the macros are compiletime. Good call on QSysInfo though, even though it's not comprehensive.
A Qt application is going to be compiled separately for different platforms though - why is it necessary to know at runtime?
thekidder
No idea, and what's not comprehensive about QSysInfo I don't know either. QSysInfo is only usable in combination with those macros.
Georg
thekidder, now that you ask no idea ;) The OP did request it though.gs, for one thing, QSysInfo lacks the Linux version
Linux has no clearly defined versions. The only thing that it's got is the kernel version.
Georg
At the least, there's the kernel version and X server version, and then there's the LSB distro-id string (as in "lsb_release -a"). No idea if it would be actually useful, though :)
+5  A: 

You can write this function:

QString getSystem() {
    #ifdef Q_WS_X11
    return QString("Linux");
    #endif

    #ifdef Q_WS_MAC
    return QString("Mac");
    #endif

    #ifdef Q_WS_QWS
    return QString("Embedded Linux");
    #endif

    #ifdef Q_WS_WIN
    return QString("Windows");
    #endif
}

I think this is as dynamic as it gets, why would you want to have it more dynamic?

Some more informations can be found here: http://stackoverflow.com/questions/341594/how-do-i-read-system-information-in-c

Georg
+7  A: 

Note that the Q_WS_* macros are defined at compile time, but QSysInfo gives some run time details.

To extend gs's function to get the specific windows version at runtime, you can do

#ifdef Q_WS_WIN
switch(QSysInfo::windowsVersion())
{
  case QSysInfo::WV_2000: return "Windows 2000";
  case QSysInfo::WV_XP: return "Windows XP";
  case QSysInfo::WV_VISTA: return "Windows Vista";
  default: return "Windows";
}
#endif

and similar for Mac.

Reed Hedges