Get Network information using Qt
hamishwillee
(Talk | contribs) m (Hamishwillee - Bot change of template (Template:CodeSnippet) - now using Template:ArticleMetaData) |
hamishwillee
(Talk | contribs) m (Hamishwillee - Addition to article of: Category:MeeGo Category:Symbian. (Add platform categories)) |
||
| Line 253: | Line 253: | ||
* [[Get System information using Qt for Symbian]] | * [[Get System information using Qt for Symbian]] | ||
| − | [[Category:Code Examples]] | + | [[Category:Code Examples]][[Category:MeeGo]] [[Category:Symbian]] |
Revision as of 08:33, 15 February 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
(15 Feb 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.


