read the battery status on an HP notebook under WIN11.
-
I would like to read the battery status on an HP notebook under WIN11.
Use Qt_5_15_0_MinGW_64_bit// Display BAT SOC // --------------------------- // https://learn.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-system_power_status _SYSTEM_POWER_STATUS *power = new _SYSTEM_POWER_STATUS; qDebug() << "BAT SOC = " << power->BatteryLifePercent; qDebug() << "ACLineStatus = " << power->ACLineStatus;
unfortunately no success with this approach: values are always 0
OUTPUT:
BAT SOC = 0 ACLineStatus = 0
could anyone help....thank you very much
-
@kendo
This has nothing to do with Qt, it's just a Windows SDK call which you should investigate.Since all you do is create the
struct _SYSTEM_POWER_STATUS
its values are meaningless. Don't you think you should call GetSystemPowerStatus function on it? -
Try this:
#include <QCoreApplication> #include <QDebug> #include <Windows.h> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); SYSTEM_POWER_STATUS status; if(!GetSystemPowerStatus(&status)) { qDebug() << "Failed to get system power status"; } else { switch(status.ACLineStatus) { case 0: qDebug() << "AC Line Status: Offline."; break; case 1: qDebug() << "AC Line Status: Online."; break; case 255: qDebug() << "AC Line Status: Unknown."; break; default: qDebug() << "AC Line Status: An error occured."; } switch(status.BatteryFlag) { case 1: qDebug() << "Battery Flag: High—the battery capacity is at more than 66 percent."; break; case 2: qDebug() << "Battery Flag:Low—the battery capacity is at less than 33 percen."; break; case 4: qDebug() << "Battery Flag: Critical—the battery capacity is at less than five percent."; break; case 8: qDebug() << "Battery Flag: Charging."; break; case 128: qDebug() << "Battery Flag: No system battery."; break; case 255: qDebug() << "Battery Flag: Unknown status—unable to read the battery flag information."; break; default: qDebug() << "Battery Flag: An error occured."; } qDebug() << "Battery Life Percent: " << status.BatteryLifePercent << "%."; switch(status.SystemStatusFlag) { case 0: qDebug() << "System Status Flag: Battery saver is off."; break; case 1: qDebug() << "System Status Flag: Battery saver on. Save energy where possible."; break; default: qDebug() << "System Status Flag: An error occured."; } } return a.exec(); }
Reference:
https://learn.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-system_power_statusMinGW GCC Qt5.15.2 "SystemStatusFlag" Does not work
MSVC Qt5.15.2 Works
MinGW GCC Qt5.15.8 "SystemStatusFlag" Does not work
MSVC Qt5.15.8 Works
MinGW GCC Qt6.4.3"SystemStatusFlag" Does not work
MSVC Qt6.4.3 Works -
@kendo said in read the battery status on an HP notebook under WIN11.:
_SYSTEM_POWER_STATUS *power = new _SYSTEM_POWER_STATUS; qDebug() << "BAT SOC = " << power->BatteryLifePercent; qDebug() << "ACLineStatus = " << power->ACLineStatus;
This should work, good luck!
SYSTEM_POWER_STATUS power; //if(!GetSystemPowerStatus(&status)) { if(GetSystemPowerStatus(&status)) { qDebug() << "BAT SOC = " << power.BatteryLifePercent; qDebug() << "ACLineStatus = " << power.ACLineStatus; }
-
@posktomten I think you meant
if(GetSystemPowerStatus(&power)) {
-
@JonB said in read the battery status on an HP notebook under WIN11.:
I think you meant if(GetSystemPowerStatus(&power)) {
Yeah haha! I had an example where "An error occurred" was printed if it didn't work. And forgot to change.
Does it work? -
@posktomten said in read the battery status on an HP notebook under WIN11.:
Does it work?
I wouldn't know :)