Reading Detailed System Information with QT C++

Posted on 2020-05-16 by Burak Hamdi TUFAN
General Programming
Reading Detailed System Information with QT C++
Hello everyone, in this article we are going to read system software and hardware information in Qt C++. We will use QSysInfo, QStorageInfo, QNetworkInterface, QSettings, QProcess classes in our application.

Let's get started.

In this article we are going to see a couple differant ways to read system hardware and software informations. We will use the QSysInfo class, then we will read the system Registry for windows, and last we will use the command promt to get these values.

In this application I will create a console application with Qt C++.

Make sure you included network and core headers in your .pro file at your project.

Below code block you will see it:


QT += core
QT += network
Do not forget Clean and Rebuild the project. If you do not do it headers may not be included in your project.

Now I will use the cout command to print the values on the console so I need to include below libraries.


#include 
using namespace std;

And We have to include below libraries in project file.


#include 
#include 
#include 
#include 
#include 
  • QSysInfo We will get some system informations from QSysInfo class.
  • QStorageInfo We will read the mounted hard disk drives informations with QStorageInfo Class.
  • QNetworkInterface We will acess the active network status with QNetworkInterface class.
  • QSettings We will access the registry and fetch some informations which on system registry on WINDOWS with QSettings class.
  • QProcess We will run commands via QProcess to read some informations of the system.

Here first we are going to use QSysInfo Class.

Additional information: you can change the console application title with system command with title parameter.

QSysInfo class has static methods to get system values. QSysInfo will not provide all informations about the device. Some of the informations whic QSysInfo provides is in below code block.


    system("title SystemInfo_Example - Thecodeprogram");
    cout << "QT C++ System Info" << endl;

    cout << "QSysInfo Class" << endl;
    cout << "CPU Architecture : " << QSysInfo::currentCpuArchitecture().toLocal8Bit().constData() << endl;
    cout << "Product Type : " << QSysInfo::prettyProductName().toLocal8Bit().constData() << endl;
    cout << "Kernel Type : " << QSysInfo::kernelType().toLocal8Bit().constData() << endl;
    cout << "Kernel Version : " << QSysInfo::kernelVersion().toLocal8Bit().constData() << endl;
    cout << "Machine ID : " << QSysInfo::machineHostName().toLocal8Bit().constData() << endl;
    cout << "-----------------------------------------------------------------" << endl;

Below image you can see the output.

QSysInfo Example Output

Now We use QStorageInfo class to get mounted hard drive informations. We are going to read the name, rootpath, filesystem, and disk spaces of the mounted hard drives.

Below code block you will see it.


    cout << "QStorageInfo Class" << endl;
    cout << "Mounted Volumes" << endl;
    foreach (const QStorageInfo &storage, QStorageInfo::mountedVolumes()) {
        if (storage.isValid() && storage.isReady()) {
            if (!storage.isReadOnly()) {
                cout << "Name:" << storage.name().toLocal8Bit().constData() <<  " ";
                cout << "Root:" << storage.rootPath().toLocal8Bit().constData() <<  " ";
                cout << "FileSys:" << storage.fileSystemType().constData() <<  " ";
                cout << "Disk Spaces:";
                cout << storage.bytesAvailable()/1000000000 <<  "GB";
                cout << "/";
                cout << storage.bytesTotal()/1000000000 <<  "GB";
                cout <<  endl;
            }
        }
    }

Below image you can see the output of the program.

QStorageInfo Class Example Output

After that we are going to read the Network status of the device. To get the network status of the device we are going to use QNetworkInterface class.

With QNetworkInterface class we are going to get Which interfaces are active and device IP number on these active interfaces. Also we are going to get device MAC adresses on these interfaces.

Below code block you will see how we did it:


    cout << "QNetworkInterface Class" << endl;
    cout << "Connected Network Informations" << endl;

    foreach(QNetworkInterface networkInterface, QNetworkInterface::allInterfaces())
    {
        if (networkInterface.flags().testFlag(QNetworkInterface::IsUp) && !networkInterface.flags().testFlag(QNetworkInterface::IsLoopBack))
        {
            foreach (QNetworkAddressEntry entry, networkInterface.addressEntries())
            {
                if ( entry.ip().toString().contains(".")){
                    cout << "Interface:"<< networkInterface.name().toLocal8Bit().constData() << " ";
                    cout << "IP:"<< entry.ip().toString().toLocal8Bit().constData() << " ";
                    cout << "MAC:"  << networkInterface.hardwareAddress().toLocal8Bit().constData() << endl;
                }
            }
        }
    }

    cout << "-----------------------------------------------------------------" << endl;

And the output of the example like will be below.

QNetworkInterface Class Example Output

Now QSettings class' turn.

Attention this specification is going to work on windows and regedit. Linux has no regedit. So it will not work on Linux.

We are going to access the system registry with the QSettings class and fetch some informations from there. System registry keeps the some of system informations like bios informations and also CPU informations. Here I will get the some of bios information of the device.

Below code block you will see it.


    cout << "QSettings Class" << endl;
    cout << "System BIOS informations. ONLY FOR WINDOWS." << endl;

    QSettings settings("HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\BIOS", QSettings::NativeFormat);
    cout << "Base Manufacturer : " << settings.value("BaseBoardManufacturer", "0").toString().toLocal8Bit().constData() << endl;
    cout << "Base Product : " << settings.value("BaseBoardProduct", "0").toString().toLocal8Bit().constData() << endl;
    cout << "BIOS Vendor : " << settings.value("BIOSVendor", "0").toString().toLocal8Bit().constData() << endl;
    cout << "BIOS Release Date : " << settings.value("BIOSReleaseDate", "0").toString().toLocal8Bit().constData() << endl;
    cout << "System Manufacturer : " << settings.value("SystemManufacturer", "0").toString().toLocal8Bit().constData() << endl;
    cout << "Product Name : " << settings.value("SystemProductName", "0").toString().toLocal8Bit().constData() << endl;
    cout << "System SKU : " << settings.value("SystemSKU", "0").toString().toLocal8Bit().constData() << endl;

    cout << "-----------------------------------------------------------------" << endl;

Below image you can see the output of the example:

Reading System Registry with QSettings in Windows

And the last method to read system values is use the command line with QProcess. You will see this method when you try to search on the internet with C#. They are using the similar commands.

Here we are going to use wmic command and the related parameters according to our required information.
winnt is the kernel type of Windows Operating System.

Here I will get the CPU name, GPU name and GPU ram informations.


    QProcess process_system;
    QString system_output;

    if(QSysInfo::kernelType() == "winnt")
    {
        QString cpuname = "wmic cpu get name";
        process_system.start(cpuname);
        process_system.waitForFinished();
        system_output = process_system.readAllStandardOutput().toUpper();
        cout << "CPU : "  << system_output.toLocal8Bit().constData() << endl;

        QString gpuname = "wmic PATH Win32_videocontroller get VideoProcessor ";
        process_system.start(gpuname);
        process_system.waitForFinished();
        system_output = process_system.readAllStandardOutput();
        cout << "GPU : "  << system_output.toLocal8Bit().constData() << endl;

        QString gpuRam = "wmic PATH Win32_VideoController get AdapterRAM";
        process_system.start(gpuRam);
        process_system.waitForFinished();
        system_output = process_system.readAllStandardOutput();
        cout << "GPU RAM : "  << system_output.toLocal8Bit().constData() << endl;
    }
If you are using the Linux operating system you can youse this method like below code block:

    if(QSysInfo::kernelType() == "linux")
    {
        QString linuxcpuname = "cat /proc/cpuinfo | grep 'model name' | uniq";
        process_system.start(linuxcpuname);
        process_system.waitForFinished();
        QString linuxOutput = process_system.readAllStandardOutput();
        cout << "Linux CPU Info : "  << linuxOutput.toLocal8Bit().constData() << endl;
    }

Output of the program for my computer like below:

Usage of wmic and get system hardware information with QT C++

As you can see our example is ready. We are ready to read system informations. You can customize the above example according to your requirements.

You can reach the example application on Github via : https://github.com/thecodeprogram/SystemInfo_Example

Have a good system informations reading.

I wish you have healthy days.

Burak Hamdi TUFAN


Tags
Share this Post
Send with Whatsapp