Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
I want to check if battery is charging or discharging in Windows
Leon last edited by
So 1 way to do it in Windows is:
bool nativeEvent(const QByteArray& eventType, void message, long* result);@
@bool MainWindow::nativeEvent(const QByteArray& eventType, void* message, long* result)
MSG* msg = (MSG*)(message);
if(msg->message == WM_POWERBROADCAST )
qDebug() << pwr.BatteryFlag << endl;
2nd way to have a timer to run every 2 seconds and simply check the current power status
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?
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.