Saturday, April 27, 2013

Qt4 utility classes

In this part of the Qt4 C++ programming tutorial, we will talk about the utility classes available in the Qt4 library.
The Qt4 library consists of a large group of helper classes, that help programmers to do their job. These include classes for working with strings, files, XML files, streams, database or network. Here we will show only a tiny drop of the whole lake.
The Qt4 library can be used to create console and GUI applications. In this chapter, we will illustrate some of the helper classes in console based applications.

Printing text to the console

This is a simple console application. The application puts some text into the console window.
console.cpp
#include <iostream>

int main()
{
  std::cout << "console application\n";
}
In the first example, we print the text using the STL (Standard Template Library) library.
console2.cpp
#include <QTextStream>

int main()
{
   QTextStream out(stdout);
   out << "console application\n";
}
The second example shows, how we can print text using the Qt4 programming library.
Output
console application

QFile

The QFile is a class for reading from and writing to files.
In the first example, we write a sentence to a file.
file.cpp
#include <QTextStream>
#include <QFile>

int main()
{

   QFile data("myfile");

   if (data.open(QFile::WriteOnly)) {
     QTextStream out(&data);
     out << "You make me want to be a better man." << endl;
   }
}
Output
$ cat myfile 
You make me want to be a better man.
The next example, we print the contents of a file to the console. The text will be in Hungarian language, so we must set the correct codec.
szerelem
S a régi szeretőmér­
mit nem cselekednék,
tengerből a vizet
kanállal lemerném.

S a tenger fenekéről
apró gyöngyöt szednék,
s a régi szeretőmnek
gyöngykoszorút kötnék. 
szerelem.cpp
#include <QTextStream>
#include <QFile>

int main()
{
  QFile data("szerelem");

  QString line;

  if (data.open(QFile::ReadOnly)) {
    QTextStream in(&data);
    QTextStream out(stdout);

    out.setCodec("UTF-8");
    in.setCodec("UTF-8");

    do {
      line = in.readLine();
      out << line << endl;
    } while (!line.isNull());
  }
}
Output
 S a régi szeretőmér­
mit nem cselekednék,
tengerből a vizet
kanállal lemerném.

S a tenger fenekéről
apró gyöngyöt szednék,
s a régi szeretőmnek
gyöngykoszorút kötnék. 

QList

The QList is one of the generic Qt4's containers. It is used to store a list of values and provides fast index-based access as well as fast insertions and removals.
mlist.cpp
#include <QTextStream>
#include <QList>

int main()
{
  QTextStream out(stdout);

  QList<QString> list;

  list << "Balzac" << "Tolstoy" << "Guldbrassen"
       << "London" << "Galsworthy" << "Sienkiewicz";

  qSort(list);

  for (int i = 0; i < list.size(); ++i) {
    out << list.at(i) << endl;
  }

}
In the code example, we create a list of great novelists. We sort alphabetically the list and print it to the console.
Output
Balzac
Galsworthy
Guldbrassen
London
Sienkiewicz
Tolstoy

QDir

The QDir class provides access to directory structures and their contents.
home.cpp
#include <QTextStream>
#include <QDir>

int main()
{
  QTextStream out(stdout);
  QString home = QDir::homePath();
  out << home << endl;
}
This example determines the home directory and prints it to the console.
Output
/home/vronskij
In the following example, we use the QDir class
filters.cpp
#include <QTextStream>
#include <QDir>


int main()
{
  QTextStream out(stdout);
  QDir dir;

  QStringList filters;
  filters << "*.c" << "*.c~";
  dir.setNameFilters(filters);

  QFileInfoList list = dir.entryInfoList();

  for (int i = 0; i < list.size(); ++i) {
    QFileInfo fileInfo = list.at(i);
    out << QString("%1").arg(fileInfo.fileName());
    out << endl;
  } 
}
The code example determines all files in the current directory and applies a specific filter to the files.
Output
$ ls -F
anim*  anim.c  anim.c~  filters*
$ ./filters 
anim.c
anim.c~

QTime

The QTime class provides clock time functions.
In the following example, we print the current local time to the console.
mtime.cpp
#include <QTextStream>
#include <QTime>

int main()
{
   QTextStream out(stdout);

   QTime qtime = QTime::currentTime();
   QString stime = qtime.toString(Qt::LocalDate);

   out << stime << endl;
}
Watch out that the file must not be called time.cpp.
Output
$ ./time 
10:30:33 PM

QString

The QString class is used to work with strings. This is probably the most important utility class available in Qt4 programming library.
concat.cpp
#include <QTextStream>

int main()
{
   QString a = "Disziplin ";
   QString b = "ist ";
   QString c = "Macht.\n";

   QTextStream out(stdout);
   out << a + b + c;
}
The first example concatenates three strings.
Output
$ ./concat
Disziplin ist Macht.
The seconds example builds a string by means of appending text one after the other.
append.cpp
#include <QTextStream>

int main()
{

   QString string = "Whether I shall ";

   string.append("turn out to be the hero of my own life, \n");
   string.append("or whether that station will be held by anybody else, \n");
   string.append("these pages must show.\n");

   QTextStream out(stdout);
   out << string;
}
Output
$ ./append
Whether I shall turn out to be the hero of my own life, 
or whether that station will be held by anybody else, 
these pages must show.
The next example shows an argument substitution.
arg.cpp
#include <QTextStream>

int main()
{
   QString string = "What if I gave you %1 red roses?";
   int num = 21;

   QTextStream out(stdout);
   out << string.arg(num) << endl; 
}
Output
$ ./str3 
What if I gave you 21 red roses?
The following example shows, how we can determine the size of the string.
size.cpp
#include <QTextStream>

int main()
{
 QString string = "The history of my life.";

 QTextStream out(stdout);
 out << "The string has " + QString::number(string.size())
    + " characters." << endl; 
}
Output
 ./size
The string has 23 characters.
The last example is about making a string uppercase or lowercase.
uplow.cpp
#include <QTextStream>

int main()
{
  QString string = "The history of my life.";

  QTextStream out(stdout);
  out << string.toLower() << endl; 
  out << string.toUpper() << endl; 
}
Output
$ ./uplow
the history of my life.
THE HISTORY OF MY LIFE.
In this chapter, we have described some utility classes in Qt4.

No comments:

Post a Comment