×
Namespaces

Variants
Actions
Revision as of 10:12, 20 April 2010 by jimgilmour1 (Talk | contribs)

Get Storage information using Qt

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): Nokia 5800 and Nokia N97.
Compatibility
Platform(s): S60 3rd Edition, FP1, FP2
S60 5th Edition
Symbian
Article
Keywords: QSystemStorageInfo, QListWidget, QScrollArea
Created: (30 Mar 2010)
Last edited: jimgilmour1 (20 Apr 2010)

Contents

Overview

This code example shows how to access storage information from the device. For example to get name of available drives, available space on each drive, total space on each drive and type of drive of the device. Code example use QSystemStorageInfo 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


Headers required

#include <qsysteminfo.h>

.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

/* GetStorageInfo.h */
#ifndef GETSTORAGEINFO_H
#define GETSTORAGEINFO_H
 
#include <QtGui/QMainWindow>
#include <qsysteminfo.h>
 
QTM_USE_NAMESPACE;
 
class QListWidget;
class QScrollArea;
 
QTM_USE_NAMESPACE;
 
class GetStorageInfo : public QMainWindow
{
Q_OBJECT
 
public:
GetStorageInfo(QWidget *parent = 0);
~GetStorageInfo();
 
private:
void resizeEvent (QResizeEvent* event);
void setWidgetGeometry(QSize* screenSize);
void getStorageInformation();
 
private:
QSystemStorageInfo* sysInfo;
QListWidget *listWidget;
QScrollArea *scrollArea;
};
 
#endif // GETSTORAGEINFO_H

Source file

/* GetStorageInfo.cpp */
#include "GetStorageInfo.h"
#include <QListWidget>
#include <QScrollArea>
#include <QResizeEvent>
 
GetStorageInfo::GetStorageInfo(QWidget *parent)
: QMainWindow(parent)
{
setWindowTitle("Get storage information");
 
/*The sysInfo becomes a child of the GetDisplayInfo, and will be destroyed when the GetDisplayInfo is deleted */
sysInfo = new QSystemStorageInfo(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);
getStorageInformation();
}
 
GetStorageInfo::~GetStorageInfo()
{
 
}
 
void GetStorageInfo::resizeEvent (QResizeEvent* event)
{
QSize widgetSize = event->size();
setWidgetGeometry(&widgetSize);
QMainWindow::resizeEvent(event);
}
 
void GetStorageInfo::setWidgetGeometry(QSize* screenSize)
{
resize(*screenSize);
QRect rect(0,0,screenSize->width(), screenSize->height());
listWidget->setGeometry(rect);
}
 
void GetStorageInfo::getStorageInformation()
{
QString strInfo;
//* qlonglong defined in 4.2 same as qint64
 
qlonglong diskSpace;
 
/*get logical drives */
QStringList driveList = sysInfo->logicalDrives();
 
strInfo.append("Drives information: ") ;
listWidget->addItem(strInfo);
 
foreach ( QString str, driveList )
{
strInfo.clear();
strInfo.append(" Drive name: " + str);
listWidget->addItem(strInfo);
 
/* get available space on drive. */
diskSpace = sysInfo->availableDiskSpace(str);
 
strInfo.clear();
 
if(diskSpace > 1000000)
{
diskSpace /= 1000000;
strInfo.setNum(diskSpace);
strInfo.prepend(" Available space: ");
strInfo.append("MB");
 
}
else if(diskSpace > 1000)
{
diskSpace /= 1000;
strInfo.setNum(diskSpace);
strInfo.prepend(" Available space: ");
strInfo.append("KB");
 
}
else
{
strInfo.setNum(diskSpace);
strInfo.prepend(" Available space: ");
strInfo.append("Bytes");
}
 
listWidget->addItem(strInfo);
 
/* get total space on drive */
diskSpace = sysInfo->totalDiskSpace(str);
 
strInfo.clear();
 
if(diskSpace > 1000000)
{
diskSpace /= 1000000;
strInfo.setNum(diskSpace);
strInfo.prepend(" Total space: ");
strInfo.append("MB");
 
}
else if(diskSpace > 1000)
{
diskSpace /= 1000;
strInfo.setNum(diskSpace);
strInfo.prepend(" Total space: ");
strInfo.append("KB");
 
}
else
{
strInfo.setNum(diskSpace);
strInfo.prepend(" Total space: ");
strInfo.append("Bytes");
}
 
listWidget->addItem(strInfo);
 
strInfo.clear();
strInfo.append(" Type of drives: ");
 
/* get type of drive */
switch(sysInfo->typeForDrive(str))
{
case QSystemStorageInfo::InternalDrive:
{
strInfo.append("InternalDrive");
break;
}
case QSystemStorageInfo::RemovableDrive:
{
strInfo.append("RemovableDrive");
break;
}
case QSystemStorageInfo::RemoteDrive:
{
strInfo.append("RemoteDrive");
break;
}
case QSystemStorageInfo::CdromDrive:
{
strInfo.append("CdromDrive");
break;
}
case QSystemStorageInfo::NoDrive:
default:
{
strInfo.append("NoDrive available");
break;
}
}
 
listWidget->addItem(strInfo);
 
strInfo.clear();
strInfo.append(" ");
listWidget->addItem(strInfo);
}
}


Postconditions

The code snippet is expected to read storage information from device and show it on screen.


Storage information 1
QtStorageInfoSC1.jpg

Storage information 2
QtStorageInfoSC2.jpg

Download Code Example

Related articles

147 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×