Reading Battery Status on Linux / Ubuntu Using QT

I am currently developing an application using qt tablet targeting with ubuntu 14.04

Since the device has only a bad battery indicator, and the application will work in full screen mode for a long time, I want to show the battery indicator inside the application. The search found mostly old results or calls in windows, android or ios apis.

Is there a way to use only Qt apis or another convenient way to get battery status information?

+5
source share
2 answers

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); } 
0
source

Even if Qt doesn’t have such an API, you can find a command line utility (like upower ) that returns battery status data and executes it from your Qt application using QProcess . After the utility completes, you can read its standard output and analyze it to find all the necessary information.

For battery-related command line tools in Ubuntu, you can link, for example, to this page .

+2
source