Contact

If you like a new Web or Desktop
application, update a existing one
or add new modifications. Your at
the right place and hit the
hire me button

Follow

If your intersted you can follow
me on Twitter by clicking here

Web and Apps Building Refernce World Wild Web UNIX Apps AND Tips Programming languages

Strings and numbers

Convenient dealing with strings and numbers is the key feature for every library/framework. Let's look at the Qt4 way.

Strings

The class mainly responsible for string handling is QString. Coveniently QStringList handles list of strings. We'll show how to use both of them.

To define string variable:

QString myStr

or with initialization:

QString myStr="hello world";

Some fun with strings:

QString str1, str2, str3;
str1 = "I love";
str2 = "strings";

// strings concatenation

str3 = str1 + " " + str2; // str3 now contains "I love strings";
str3 += " very"; // str3 = "I love strings very"
str3.append(" much"); // "I love strings very much"
// append and + operator do the same thing
str3.prepend("OH, "); // "OH, I love strings very much"

// strings comparison

str1 = "some nice word";
str2 = "";

if ( str1 == "some nice word" ) // true
if ( str2.isEmpty() ) ... // true as well, str2 is empty string
if ( str1 == str2 ) ... // false

str1 = "word";
int size;
size = str.size(); // size of the string is 4

A really nice thing is that you don't have to care about memory allocation no matter how much string data you append. Qt handles it itself.

Strings are internaly represented as unicode characters. Sometimes conversion to 8bit form is needed. Use functions toAscii() or toLatin1() in such cases.

To get an upper or lowercase version of a string use toUpper() or toLower().

String to number conversion

A very common task is to convert a string to a number. Here's how to do it:

QString str;
int number;
double d;

str = "123";
int = str.toInt();

str = "10.987";
d = str.toDouble();

To check if the conversion was successfull (example from QString class documentation):

QString str = "FF";
bool ok;
int hex = str.toInt(&ok, 16); // hex == 255, ok == true
int dec = str.toInt(&ok, 10); // dec == 0, ok == false

Number to string conversion

This is maybe an even more common task then previous one, let's have a look at it.

// integer converstion
QString str;
str.setNum(1234); // str now contains "1234"

// double converstion
double d = 2.345;

str.setNum(d); // default format ("g"), default precision is 6, str is now "2.345000"
str.setNum(d, 'f', 3); // "2.345"
str.setNum(d, 'e', 3); // "2.345e+00"

Refer to arg() functions for format details.

In some situations it is more convenient to use static function number().

int myInt = 123;
QMessageBox(this, "Some label", "Hello, value of integer called myInt is " + QString::number(myInt) );

Please read QString class reference for complex information how to deal with strings in Qt.

QStringList class

This class is very handy if you need a list of strings.

QStringList list;

// add string into the list
// these 3 ways of adding string to list are equivalent
list.append("apple");
list += "banana";
list << "submarine";

// iterate over string list
QString str;
for (int i = 0; i < list.size(); ++i)
{
str = list[i]; // or list.at(i);
// do something with str
}

Allocation of memory is handled by Qt itself.

Please read QStringList class reference for details.