Battery Indicator Example using Qt
Article Metadata
Code Example
Source file: Media:BatteryIndicator.zip
Article
Created: chintandave_er
(14 Nov 2010)
Last edited: hamishwillee
(03 May 2013)
Contents |
Overview
This code example shows how to create simple basic 'Battery Indicator' application using QSystemDeviceInfo.
Note: This example uses a QWidget based UI (deprecated) and should be updated to use a QML based UI.
Header (batteryindicator.h)
#ifndef BATTERYINDICATOR_H
#define BATTERYINDICATOR_H
#include <QDialog>
//Include the System Info header file
#include <QSystemInfo>
QTM_USE_NAMESPACE
namespace Ui {
class BatteryIndicator;
}
class BatteryIndicator : public QDialog
{
Q_OBJECT
public:
explicit BatteryIndicator(QWidget *parent = 0);
~BatteryIndicator();
private:
Ui::BatteryIndicator *ui;
void setupGeneral();
QSystemDeviceInfo *deviceInfo;
};
#endif // BATTERYINDICATOR_H
Source (batteryindicator.cpp)
#include "batteryindicator.h"
#include "ui_batteryindicator.h"
//set initial values
BatteryIndicator::BatteryIndicator(QWidget *parent) :
QDialog(parent),
ui(new Ui::BatteryIndicator),
deviceInfo(NULL)
{
ui->setupUi(this);
setupGeneral();
}
BatteryIndicator::~BatteryIndicator()
{
delete ui;
}
void BatteryIndicator::setupGeneral()
{
//Create a QSystemDeviceInfo object and set its value
deviceInfo = new QSystemDeviceInfo(this);
ui->batteryLevelBar->setValue(deviceInfo->batteryLevel());
//connect the signal to the setValue slot of the progress bar
connect(deviceInfo, SIGNAL(batteryLevelChanged(int)),
ui->batteryLevelBar, SLOT(setValue(int)));
}
UI File (BatteryIndicator.ui)
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>BatteryIndicator</class>
<widget class="QDialog" name="BatteryIndicator">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>480</height>
</rect>
</property>
<property name="windowTitle">
<string>BatteryIndicator</string>
</property>
<widget class="QProgressBar" name="batteryLevelBar">
<property name="geometry">
<rect>
<x>110</x>
<y>110</y>
<width>121</width>
<height>291</height>
</rect>
</property>
<property name="autoFillBackground">
<bool>false</bool>
</property>
<property name="value">
<number>24</number>
</property>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="textDirection">
<enum>QProgressBar::TopToBottom</enum>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>120</x>
<y>10</y>
<width>161</width>
<height>31</height>
</rect>
</property>
<property name="autoFillBackground">
<bool>false</bool>
</property>
<property name="lineWidth">
<number>3</number>
</property>
<property name="midLineWidth">
<number>3</number>
</property>
<property name="text">
<string>Battery Indicator</string>
</property>
<property name="textFormat">
<enum>Qt::RichText</enum>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
<property name="textInteractionFlags">
<set>Qt::LinksAccessibleByMouse</set>
</property>
</widget>
<widget class="Line" name="line">
<property name="geometry">
<rect>
<x>0</x>
<y>40</y>
<width>661</width>
<height>16</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
Main File (main.cpp)
#include <QtGui/QApplication>
#include "batteryindicator.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
BatteryIndicator w;
w.showFullScreen();
return a.exec();
}
Project File (.pro File)
QT += core gui
TARGET = BatteryIndicator
TEMPLATE = app
SOURCES += main.cpp\
batteryindicator.cpp
HEADERS += batteryindicator.h
FORMS += batteryindicator.ui
#uses the System Info API
CONFIG += mobility
MOBILITY = systeminfo
symbian {
TARGET.UID3 = 0xe1c86fe6
# TARGET.CAPABILITY +=
TARGET.EPOCSTACKSIZE = 0x14000
TARGET.EPOCHEAPSIZE = 0x020000 0x800000
}
Postcondition
Here is one screenshot of the BatteryIndicator Apps.
Download Source Code
You can download source code from this link File:BatteryIndicator.zip.


This is a good article on using Qt Mobility function Jim Gilmour