Namespaces
Variants
Actions
(Difference between revisions)

Get Storage information using Qt

Jump to: navigation, search
m (Hamishwillee - Bot change of template (Template:CodeSnippet) - now using Template:ArticleMetaData)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
 
(2 intermediate revisions by one user not shown)
Line 31: Line 31:
  
 
==Headers required==
 
==Headers required==
<code cpp>
+
<code cpp-qt>
 
#include <qsysteminfo.h>
 
#include <qsysteminfo.h>
 
</code>
 
</code>
Line 38: Line 38:
 
Modify your '''.pro''' file as follows.
 
Modify your '''.pro''' file as follows.
  
<code cpp>
+
<code cpp-qt>
 
QT += core \
 
QT += core \
 
     gui \
 
     gui \
Line 53: Line 53:
 
==Header file==
 
==Header file==
  
<code cpp>
+
<code cpp-qt>
 
/* GetStorageInfo.h */
 
/* GetStorageInfo.h */
 
#ifndef GETSTORAGEINFO_H
 
#ifndef GETSTORAGEINFO_H
Line 93: Line 93:
 
==Source file==
 
==Source file==
  
<code cpp>
+
<code cpp-qt>
 
/* GetStorageInfo.cpp */
 
/* GetStorageInfo.cpp */
 
#include "GetStorageInfo.h"
 
#include "GetStorageInfo.h"
Line 291: Line 291:
 
* [[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

Article
Keywords: QSystemStorageInfo, QListWidget, QScrollArea
Created: Yashu (30 Mar 2010)
Last edited: hamishwillee (11 Oct 2012)

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

This page was last modified on 11 October 2012, at 04:23.
248 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved