P. 1
Qt for Symbian

Qt for Symbian

|Views: 1,456|Likes:
Published by burhay

More info:

Published by: burhay on Sep 10, 2010
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

05/24/2012

pdf

text

original

To demonstrate the use of files and data streams, we will use a console application without a GUI.
In contrast to the previous examples, this type of application only requires QtCore. Adding the
configuration console to the project file (.pro) enables console output on Windows. For Mac
OS X, the project settings also tell qmake not to put the executable inside a bundle:

Qt in General 53

1 TEMPLATE = app

2 QT = core

3 CONFIG += console

4 CONFIG -= app_bundle

5 SOURCES += main.cpp

This applicationdoes not use aQCoreApplicationobject, asQFiledoes not need the event
loop – our example is finished after sequentially working throughthe source code inmain(). First,
the example creates a file and then serializes three different variables. In the second part, these
variables are read back in again:

1 #include

2 #include

3 #include

4 #include

5

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

7 {

8

// Writing a file

9

QFile outFile("data.dat");

10

if (!outFile.open(QIODevice::WriteOnly)) {

11

std::cerr << "Cannot open file for writing: " <<

12

outFile.errorString().toStdString() << std::endl;

13

return 1;

14

}

15

16

QDataStream out(&outFile);

17

out.setVersion(QDataStream::Qt_4_5);

18

19

quint32 outValue = 12345678;

20

QTime outTime = QTime::currentTime();

21

QVariant outVar("Some text");

22

23

out << outValue << outTime << outVar;

24

outFile.close();

25

26

// (continued in next listing)

At first, our application tries to open the file data.dat for writing. If this operation fails, an
error message is printed. This is done using the standard error stream (std::cerr), which is by
default directed to the console. The QString returned by outFile.errorString() needs
to be converted to a std::string using toStdString(), for which has a <<
overload.

If the file was opened correctly, a QDataStream is constructed using the QFile object
(based on QIODevice). In our example, the task of the stream class is to write three different
variable types to the file using operator«(), which is overloaded to handle various data
types. QDataStream’s binary representation of objects has changed in the many years since
Qt 1.0 was released, and will continue to do so in the future. In order to maintain forward and

54 Qt for Symbian

Figure 3.14 Properties of the QDataStreamin the debug view of Qt Creator.

backward compatibility, it is helpful to hard-code the version number in the application (e.g.
QDataStream::Qt_4_5). This ensures that the data is written and read in exactly the same
way.

Additionally, the data format is platform independent. You can transfer data via a network
connection from an ARM-based mobile device to an Intel-based PC without problems. In
Figure 3.14 you can see that Big Endian data encoding is used by default (see the byte-order
property of the out class instance). The data format version of Qt_4_5 equals decimal 11.
Especially for settings files, it is a good idea to extend the example to include a brief file header.
This should contain a magic string that allows you to check if the file is really from your application.
A version number helps to import correctly old versions of settings files if you decide to expand the
file in future versions. See the documentation of QDataStreamfor an example.

Qt in General 55

The QVariant data type used for the third variable is a powerful data type. It acts like a union
of the most common Qt data types. It stores one type of data and can additionally convert between
differenttypes.Inourcase,theQVariantcontainsaQString.Inadditiontoserializingtheactual
content, QVariant additionally saves the type of the variable to the stream, so that it can be read
back using a QVariant.
As the outFile object was created on the stack, it will close the file automatically when the
variable goes out of scope. However, to be on the safe side and to save resources, it is still a good
idea to close resources as soon as you no longer need them.
Reading the file back in works in a similar way to writing the data. After opening the file
and creating a QDataStream, we set the stream version to ensure compatibility. Next, the three
variables are read back in exactly the same order:

1

// Reading a file (continued from previous listing)

2

QFile inFile("data.dat");

3

if (!inFile.open(QIODevice::ReadOnly)) {

4

std::cerr << "Cannot open file for reading: " <<

5

inFile.errorString().toStdString() << std::endl;

6

return 1;

7

}

8

9

QDataStream in(&inFile);

10

in.setVersion(QDataStream::Qt_4_5);

11

12

quint32 inValue;

13

QTime inTime;

14

QVariant inVar;

15

16

// Read values in same order as they were written

17

in >> inValue >> inTime >> inVar;

18

19

qDebug() << "Variant type:" << inVar.typeName() <<

20

", contents:" << inVar.toString();

21

22

inFile.close();

23

return 0;

24 }

The variable inVar of type QVariant recognized the type that was previously written to the
file. The debug output line prints the type of the variable (as text) and then returns the variant as a
QString:

Variant type: QString , contents: "Some text"

Insteadofusingthestandardstreams providedby,theqDebug()globalfunction
(available when including ) is used as the output stream. By default, this prints the text
to stderr output under Unix/X11 and Mac OS X and to the console (for console apps) or the
debugger under Windows. An additional advantage is that Qt data types like QString can be
directly serialized, without conversion to a std::stringor char*.

56 Qt for Symbian

You can store data in a very compact way using binary data formats, as demonstrated in this
example. Usage is easy through the convenient QDataStream class. As mentioned above, the
QTextStream class is specialized on serializing text and can take care of different character
encodings. If you prefer a lower level way of handling data, you can use the QIODevice’s
write()and readAll()functions directly without using higher level stream classes.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->