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. read the battery status on an HP notebook under WIN11.

read the battery status on an HP notebook under WIN11.

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 1.4k 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.
  • K Offline
    K Offline
    kendo
    wrote on last edited by
    #1

    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

    JonBJ 1 Reply Last reply
    0
    • K kendo

      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

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @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?

      1 Reply Last reply
      1
      • posktomtenP Offline
        posktomtenP Offline
        posktomten
        wrote on last edited by posktomten
        #3

        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_status

        MinGW 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

        posktomten

        1 Reply Last reply
        1
        • posktomtenP Offline
          posktomtenP Offline
          posktomten
          wrote on last edited by posktomten
          #4

          @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

          JonBJ 1 Reply Last reply
          1
          • posktomtenP posktomten

            @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;
            }
            
            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #5

            @posktomten I think you meant if(GetSystemPowerStatus(&power)) {

            posktomtenP 1 Reply Last reply
            0
            • JonBJ JonB

              @posktomten I think you meant if(GetSystemPowerStatus(&power)) {

              posktomtenP Offline
              posktomtenP Offline
              posktomten
              wrote on last edited by
              #6

              @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

              JonBJ 1 Reply Last reply
              0
              • posktomtenP posktomten

                @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?

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #7

                @posktomten said in read the battery status on an HP notebook under WIN11.:

                Does it work?

                I wouldn't know :)

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  kendo
                  wrote on last edited by
                  #8

                  it works !!!

                  Now I can send a switch-off message to my USB-PD-Charger if (SOC >= 80%):
                  It's a pity that this cannot be parameterized in the WIN11 setup. (or in HP BIOS)

                  Thanks and have a nice week end!

                  JonBJ 1 Reply Last reply
                  0
                  • K kendo

                    it works !!!

                    Now I can send a switch-off message to my USB-PD-Charger if (SOC >= 80%):
                    It's a pity that this cannot be parameterized in the WIN11 setup. (or in HP BIOS)

                    Thanks and have a nice week end!

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by
                    #9

                    @kendo
                    My HP laptop has a parameter in the BIOS, which I have never changed, so that it only ever charges to 80%. Dunno what that is about.

                    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