Battery Indicator Example using Qt

From Qt Wiki
Jump to navigation Jump to search

Template:ArticleMetaData

Overview[edit | edit source]

Template:Abstract

Template:Note

Header (batteryindicator.h)[edit | edit source]

  1. ifndef BATTERYINDICATOR_H
  2. define BATTERYINDICATOR_H
  1. include <QDialog>

//Include the System Info header file

  1. 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;

};

  1. endif // BATTERYINDICATOR_H

Source (batteryindicator.cpp)[edit | edit source]

  1. include "batteryindicator.h"
  2. 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)[edit | edit source]

<?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)[edit | edit source]

  1. include <QtGui/QApplication>
  2. include "batteryindicator.h"

int main(int argc, char *argv[]) {

   QApplication a(argc, argv);
   BatteryIndicator w;
   w.showFullScreen();
   return a.exec();

}


Project File (.pro File)[edit | edit source]

QT += core gui

TARGET = BatteryIndicator TEMPLATE = app


SOURCES += main.cpp\

       batteryindicator.cpp

HEADERS += batteryindicator.h

FORMS += batteryindicator.ui

  1. uses the System Info API

CONFIG += mobility MOBILITY = systeminfo

symbian {

   TARGET.UID3 = 0xe1c86fe6
   # TARGET.CAPABILITY += 
   TARGET.EPOCSTACKSIZE = 0x14000
   TARGET.EPOCHEAPSIZE = 0x020000 0x800000

}


Postcondition[edit | edit source]

Here is one screenshot of the BatteryIndicator Apps.

BatteryIndicatorImg.png

Download Source Code[edit | edit source]

You can download source code from this link File:BatteryIndicator.zip.