This example demonstrates how to find the operating system name and version where the application is currently running. Macros are used to isolate the platform specific source code.
#include <QtCore>
#include <QtGui>
class MainWindow : public QWidget
{
public:
MainWindow();
};
We only need a very simple declaration for the MainWindow class.
MainWindow::MainWindow()
{
QString osName = "Unknown";
QString osEdition = "Unspecified";
#if defined(Q_OS_WIN)
QSysInfo::WinVersion winVersion =
QSysInfo::windowsVersion();
osName = "Windows";
osEdition = QSysInfo::windowsEdition(winVersion);
#elif defined(Q_OS_DARWIN)
QSysInfo::MacVersion macVersion =
QSysInfo::macVersion();
osName = "Mac OS X";
osEdition = QSysInfo::macEdition(macVersion);
#else
osName = "Unix";
#endif
setMinimumSize(400, 200);
QLabel *label_1 = new QLabel();
label_1->setText("Operating System: " + osName);
QLabel *label_2 = new QLabel();
label_2->setText("OS Edition: " + osEdition);
QPushButton *pb = new QPushButton();
pb->setText("Close");
QFrame *box = new QFrame();
box->setLineWidth(1);
box->setFrameStyle(QFrame::Box);
QVBoxLayout *layout1 = new QVBoxLayout(box);
layout1->setContentsMargins(20, 20, 20, 20);
layout1->addWidget(label_1);
layout1->addSpacing(20);
layout1->addWidget(label_2);
layout1->addStretch();
QHBoxLayout *layout2 = new QHBoxLayout();
layout2->addStretch();
layout2->addWidget(pb);
layout2->addStretch();
QVBoxLayout *layoutMain = new QVBoxLayout(this);
layoutMain->setContentsMargins(80, 55, 80, 15);
layoutMain->addWidget(box);
layoutMain->addSpacing(75);
layoutMain->addLayout(layout2);
connect(pb, &QPushButton::clicked,
this, &QWidget::close);
}
The macros on lines 6 and 13 are required since the code inside these blocks are platform specific. If you try to compile code which is designed specifically for Windows, this code will fail to compile on any other platform. The test for Q_OS_WIN guards the Windows only code.
To test if the code will be run on the OS X platform query the Q_OS_DARWIN macro. The code for this platform can only be compiled for an application which will be run on OS X. The third block is for Unix platforms.
The call to windowsVersion( ) found on line 8 will return the winVersion. This is an enum which corresponds to the Windows operating system where the application is running. An example value for this enum is QSysInfo::WV_WINDOWS10. For a full list of enum values refer to the QSysInfo class documentation.
On line 11 we call windowsEdition( ) and pass the winVersion. The return value for this method is the name of the operating system as a string.
The same logic applies to the code for OS X which calls macVersion( ) and macEdition( ).
The user interface source code starts on line 25 and is very similar to prior examples in this Journal. There is only one call to connect() which sets up a Signal/Slot connection to exit the application when the close button is clicked.
Main Function
Since the source code for main() has not changed there is no need to show it again. Refer to example 3 or download the full source for this example.
Running the Example
To build and run this example use the same CMake build file and commands as we showed for the first example. The only suggested modification is on line 2 of the CMakeLists.txt file, change “example_1” to “example_41”.