Get Network information using Qt
somnathbanik
(Talk | contribs) |
hamishwillee
(Talk | contribs) m (Text replace - "<code cpp>" to "<code cpp-qt>") |
||
| (3 intermediate revisions by one user not shown) | |||
| Line 1: | Line 1: | ||
[[Category:Qt Mobility]] | [[Category:Qt Mobility]] | ||
| − | {{ | + | {{ArticleMetaData |
|id=... | |id=... | ||
|platform=S60 3rd Edition, FP1, FP2<br>S60 5th Edition | |platform=S60 3rd Edition, FP1, FP2<br>S60 5th Edition | ||
| Line 8: | Line 8: | ||
|creationdate=March 30, 2010 | |creationdate=March 30, 2010 | ||
|keywords= QSystemNetworkInfo, QListWidget, QScrollArea | |keywords= QSystemNetworkInfo, QListWidget, QScrollArea | ||
| + | |||
| + | |sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) --> | ||
| + | |installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) --> | ||
| + | |sdk=<!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) --> | ||
| + | |devicecompatability=<!-- Compatible devices (e.g.: All* (must have GPS) ) --> | ||
| + | |signing=<!-- Empty or one of Self-Signed, DevCert, Manufacturer --> | ||
| + | |capabilities=<!-- Capabilities required (e.g. Location, NetworkServices.) --> | ||
| + | |author=[[User:Yashu]] | ||
}} | }} | ||
| Line 23: | Line 31: | ||
==Headers required== | ==Headers required== | ||
| − | <code cpp> | + | <code cpp-qt> |
#include <qsysteminfo.h> | #include <qsysteminfo.h> | ||
</code> | </code> | ||
| Line 33: | Line 41: | ||
Modify your''' .pro''' file as follows. | Modify your''' .pro''' file as follows. | ||
| − | <code cpp> | + | <code cpp-qt> |
QT += core \ | QT += core \ | ||
gui \ | gui \ | ||
| Line 48: | Line 56: | ||
==Header file== | ==Header file== | ||
| − | <code cpp> | + | <code cpp-qt> |
/* GetNetworkInfo.h */ | /* GetNetworkInfo.h */ | ||
| Line 84: | Line 92: | ||
==Source file== | ==Source file== | ||
| − | <code cpp> | + | <code cpp-qt> |
/* GetNetworkInfo.cpp */ | /* GetNetworkInfo.cpp */ | ||
#include "GetNetworkInfo.h" | #include "GetNetworkInfo.h" | ||
| Line 245: | Line 253: | ||
* [[Get System information using Qt for Symbian]] | * [[Get System information using Qt for Symbian]] | ||
| − | [[Category:Code Examples]] | + | [[Category:Code Examples]][[Category:MeeGo Harmattan]] [[Category:Symbian]] |
Latest revision as of 04:23, 11 October 2012
Article Metadata
Tested with
Devices(s): Nokia 5800 and Nokia N97.
Compatibility
Platform(s): S60 3rd Edition, FP1, FP2
S60 5th Edition
S60 5th Edition
Article
Keywords: QSystemNetworkInfo, QListWidget, QScrollArea
Created: Yashu
(30 Mar 2010)
Last edited: hamishwillee
(11 Oct 2012)
Contents |
Overview
This code example shows how to access network information from the system. For example to get current MCC, current MNC, home MCC, home MNC location area code, network name, network signal strength, network status and MAC address of the device. Code example use QSystemNetworkInfo API from QtMobility APIs.
This snippet requires ReadDeviceData capabilities. Self-signing is not possible, a Developer certificate is needed to sign application for testing.
Prerequisite
- Install Qt for Symbian:Installing Qt on Symbian
- Install QtMobility APIs:Setting up environment for Qt Mobility API
Headers required
#include <qsysteminfo.h>Capabilities required
.pro file
Modify your .pro file as follows.
QT += core \
gui \
network
INCLUDEPATH += d:/qtmobility/src/systeminfo
INCLUDEPATH += D:/QtMobility/src/global
CONFIG += mobility
MOBILITY = systeminfo
symbian:TARGET.CAPABILITY = ReadDeviceData
Header file
/* GetNetworkInfo.h */
#ifndef GETNETWORKINFO_H
#define GETNETWORKINFO_H
#include <QtGui/QMainWindow>
#include <qsysteminfo.h>
QTM_USE_NAMESPACE;
class QListWidget;
class QScrollArea;
class GetNetworkInfo : public QMainWindow
{
Q_OBJECT
public:
GetNetworkInfo(QWidget *parent = 0);
~GetNetworkInfo();
private:
void resizeEvent (QResizeEvent* event);
void setWidgetGeometry(QSize* screenSize);
void getNetworkInformation();
private:
QSystemNetworkInfo* sysInfo;
QListWidget *listWidget;
QScrollArea *scrollArea;
};
#endif // GETNETWORKINFO_H
Source file
/* GetNetworkInfo.cpp */
#include "GetNetworkInfo.h"
#include <QListWidget>
#include <QScrollArea>
#include <QResizeEvent>
GetNetworkInfo::GetNetworkInfo(QWidget *parent)
: QMainWindow(parent)
{
setWindowTitle("Get network information");
/*The sysInfo becomes a child of the GetDisplayInfo, and will be destroyed when the GetDisplayInfo is deleted */
sysInfo = new QSystemNetworkInfo(this);
/* To show items in list view */
listWidget = new QListWidget();
listWidget->setObjectName(QString::fromUtf8("listWidget"));
/* To make UI scrollable */
scrollArea = new QScrollArea();
setWidgetGeometry(&(size()));
/* scrollArea takes ownership of the listWidget, so no need to delete it */
scrollArea->setWidget(listWidget);
scrollArea->setAlignment(Qt::AlignLeft);
scrollArea->setWidgetResizable(true);
/* QMainWindow takes ownership of the scrollArea, so no need to delete it */
setCentralWidget(scrollArea);
getNetworkInformation();
}
GetNetworkInfo::~GetNetworkInfo()
{
}
void GetNetworkInfo::resizeEvent (QResizeEvent* event)
{
QSize widgetSize = event->size();
setWidgetGeometry(&widgetSize);
QMainWindow::resizeEvent(event);
}
void GetNetworkInfo::setWidgetGeometry(QSize* screenSize)
{
resize(*screenSize);
QRect rect(0,0,screenSize->width(), screenSize->height());
listWidget->setGeometry(rect);
}
void GetNetworkInfo::getNetworkInformation()
{
QString strInfo;
/* get Cell Id */
strInfo.setNum(sysInfo->cellId());
strInfo.prepend("Cell ID: ");
listWidget->addItem(strInfo);
/* get current MCC */
strInfo.clear();
strInfo.append("Current MCC: " + sysInfo->currentMobileCountryCode());
listWidget->addItem(strInfo);
/* get current MNC */
strInfo.clear();
strInfo.append("Current MNC: " + sysInfo->currentMobileNetworkCode());
listWidget->addItem(strInfo);
/* get home MCC */
strInfo.clear();
strInfo.append("home MCC: " + sysInfo->homeMobileCountryCode());
listWidget->addItem(strInfo);
/* get home MNC */
strInfo.clear();
strInfo.append("home MNC: " + sysInfo->homeMobileNetworkCode());
listWidget->addItem(strInfo);
/* get location area code */
strInfo.clear();
strInfo.setNum(sysInfo->locationAreaCode());
strInfo.prepend("Location area code: ");
listWidget->addItem(strInfo);
/* get network name */
strInfo.clear();
strInfo.append("Network name: " + sysInfo->networkName(QSystemNetworkInfo::GsmMode));
listWidget->addItem(strInfo);
/* get network signal strength*/
strInfo.clear();
strInfo.setNum(sysInfo->networkSignalStrength(QSystemNetworkInfo::GsmMode));
strInfo.prepend("Network signal strength: ");
strInfo.append("%");
listWidget->addItem(strInfo);
/* get network status */
strInfo.clear();
strInfo.append("Network status: ");
switch(sysInfo->networkStatus(QSystemNetworkInfo::GsmMode))
{
case QSystemNetworkInfo::Roaming:
strInfo.append("On Roaming network.");
break;
case QSystemNetworkInfo::Denied:
strInfo.append("Network access denied.");
break;
case QSystemNetworkInfo::Home Network:
strInfo.append("On Home Network");
break;
case QSystemNetworkInfo::Connected:
strInfo.append("Connected to network.");
break;
case QSystemNetworkInfo::Busy:
strInfo.append("Network is busy.");
break;
case QSystemNetworkInfo::Searching:
strInfo.append("Searching for or connecting with the network.");
break;
case QSystemNetworkInfo::EmergencyOnly:
strInfo.append("Emergency calls only.");
break;
case QSystemNetworkInfo::NoNetworkAvailable:
strInfo.append("There is no network available.");
break;
case QSystemNetworkInfo::UndefinedStatus:
Default:
strInfo.append("There is no network device, or error.");
break;
}
listWidget->addItem(strInfo);
/* get IMSI of device */
strInfo.clear();
strInfo.append("MAC address: " + sysInfo->macAddress(QSystemNetworkInfo::GsmMode));
listWidget->addItem(strInfo);
}
Postconditions
The code snippet is expected to read network information from device and show it on screen.
Download Code Example
- Working Code Example is available to download from here.


