使用 QT 在 Linux/Ubuntu 上读取电池状态 [英] Reading Battery Status on Linux/Ubuntu using QT

查看:25
本文介绍了使用 QT 在 Linux/Ubuntu 上读取电池状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 qt 开发针对运行 ubuntu 14.04 的平板电脑的应用程序

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

由于设备上只有一个电量不足的指示器并且应用程序将长时间全屏运行,我想在应用程序中显示一个电池指示器.搜索主要发现旧结果或对 windows、android 或 ios apis 的调用.

Since there is only a poor battery indicator on the device and the application will run fullscreen for prolonged time, I want to show an battery indicator inside the application. A search found mainly old results or calls to windows, android or ios apis.

有没有办法只使用 Qt api 或其他舒适的方式来获取有关电池状态的信息?

Is there any way using just the Qt apis or an other comfortable way to get information about the battery state?

推荐答案

尽管用户 vahanchos 的回答对我有帮助,并且可能是其他人的方法,但我最终得到了不同的解决方案.

Even though user vahanchos answer was helpful for me, and probably is the way to go for others, I ended up with a different solution.

就我而言,我只为一种特殊设备类型和一组已知的开发机器编码.因此我可以读取 sys/class/power_supply/ 中的相关文件.我不能保证其他设备会将它们的文件命名为完全相同.但这可能值得一试.

In my case I code for only one special device type and a known set of development machines. therefore I could just read the relevant files in sys/class/power_supply/. I can not guarantee that other devices will name their files there exactly the same. But it might be worth the 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);
}

这篇关于使用 QT 在 Linux/Ubuntu 上读取电池状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆