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. I want to check if battery is charging or discharging in Windows
Qt 6.11 is out! See what's new in the release blog

I want to check if battery is charging or discharging in Windows

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.6k 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.
  • L Offline
    L Offline
    Leon
    wrote on last edited by
    #1

    So 1 way to do it in Windows is:

    .h
    @private:
    Ui::MainWindow ui;
    bool nativeEvent(const QByteArray& eventType, void
    message, long* result);@

    .cpp

    @bool MainWindow::nativeEvent(const QByteArray& eventType, void* message, long* result)
    {
    MSG* msg = (MSG*)(message);
    if(msg->message == WM_POWERBROADCAST )
    {
    SYSTEM_POWER_STATUS pwr;
    GetSystemPowerStatus(&pwr);
    qDebug() << pwr.BatteryFlag << endl;
    }
    }@

    2nd way to have a timer to run every 2 seconds and simply check the current power status
    @SYSTEM_POWER_STATUS pwr;
    GetSystemPowerStatus(&pwr);
    qDebug() << pwr.BatteryFlag << endl;@

    Obviously 1st solution is the fastest, but isn't it bad to check so many events just to take only the WM_POWERBROADCAST ones?
    What you say is the best solution if i am not in that much need of speed to check if battery is charging or discharging. Are nativeEvents "heavy" for an application?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Native events are, well, native events. They are delivered whether you handle them or not. If not then Qt either handles them on its own or passes to the default Windows message handler but they arrive none the less.

      In your case the cost of handling a single message is (more or less) a (virtual) function call and an if statement. Some inlining might occur at one point or another and CPU branch prediction will almost certainly kick in here. Is it much? It's your call. You can use a profiler to check but I'd say 99% of the time worrying about this kind of stuff is premature optimization and a waste of time.

      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