Despite the fact that vahanchos's answer was useful to me, and probably this is the way for others, I got a different solution.
In my case, I encode only one type of special device and a well-known set of development machines. so I could just read the relevant files in sys/class/power_supply/ . I cannot guarantee that other devices will call their files exactly the same. But maybe worth a try.
#include <QFile> void refreshValues(){ QFile acLine("/sys/class/power_supply/AC/online"); QFile acAdp("/sys/class/power_supply/ADP0/online"); QFile bCap("/sys/class/power_supply/BAT0/capacity"); bool ac = false; int level = 0; if(acLine.exists()){ acLine.open(QIODevice::ReadOnly | QIODevice::Text); if(QString(acLine.readAll()).toInt()){ ac = true; } acLine.close(); }else if(acAdp.exists()){ acAdp.open(QIODevice::ReadOnly | QIODevice::Text); if(QString(acAdp.readAll()).toInt()){ ac = true; } acAdp.close(); } if(bCap.exists()){ bCap.open(QIODevice::ReadOnly | QIODevice::Text); level = QString(bCap.readAll()).toInt(); bCap.close(); } setAcPowerActive(ac); setBatteryLevel(level); }
source share