Common Qt Classes
Most Qt classes are derived from the QObject
class. It encapsulates the central concepts of Qt. But there are many more classes in the framework. Before we continue looking at QML and how to extend it, we will look at some basic Qt classes that are useful to know about.
The code examples shown in this section are written using the Qt Test library. This way, we can ensure that the code works, without constructing entire programs around it. The QVERIFY
and QCOMPARE
functions from the test library to assert a certain condition. We will use {}
scopes to avoid name collisions. Don't let this confuse you.
QString
In general, text handling in Qt is Unicode based. For this, you use the QString
class. It comes with a variety of great functions which you would expect from a modern framework. For 8-bit data, you would use normally the QByteArray
class and for ASCII identifiers the QLatin1String
to preserve memory. For a list of strings you can use a QList<QString>
or simply the QStringList
class (which is derived from QList<QString>
).
Below are some examples of how to use the QString
class. QString can be created on the stack but it stores its data on the heap. Also when assigning one string to another, the data will not be copied - only a reference to the data. So this is really cheap and lets the developer concentrate on the code and not on the memory handling. QString
uses reference counters to know when the data can be safely deleted. This feature is called Implicit Sharing and it is used in many Qt classes.
Below you can see how to convert a number to a string and back. There are also conversion functions for float or double and other types. Just look for the function in the Qt documentation used here and you will find the others.
Often in a text, you need to have parameterized text. One option could be to use QString("Hello" + name)
but a more flexible method is the arg
marker approach. It preserves the order also during translation when the order might change.
Sometimes you want to use Unicode characters directly in your code. For this, you need to remember how to mark them for the QChar
and QString
classes.
This gives you some examples of how to easily treat Unicode aware text in Qt. For non-Unicode, the QByteArray
class also has many helper functions for conversion. Please read the Qt documentation for QString
as it contains tons of good examples.
Sequential Containers
A list, queue, vector or linked-list is a sequential container. The mostly used sequential container is the QList
class. It is a template based class and needs to be initialized with a type. It is also implicit shared and stores the data internally on the heap. All container classes should be created on the stack. Normally you never want to use new QList<T>()
, which means never use new
with a container.
The QList
is as versatile as the QString
class and offers a great API to explore your data. Below is a small example of how to use and iterate over a list using some new C++ 11 features.
Associative Containers
A map, a dictionary, or a set are examples of associative containers. They store a value using a key. They are known for their fast lookup. We demonstrate the use of the most used associative container the QHash
also demonstrating some new C++ 11 features.
File IO
It is often required to read and write from files. QFile
is actually a QObject
but in most cases, it is created on the stack. QFile
contains signals to inform the user when data can be read. This allows reading chunks of data asynchronously until the whole file is read. For convenience, it also allows reading data in blocking mode. This should only be used for smaller amounts of data and not large files. Luckily we only use small amounts of data in these examples.
Besides reading raw data from a file into a QByteArray
you can also read data types using the QDataStream
and Unicode string using the QTextStream
. We will show you how.
More Classes
Qt is a rich application framework. As such it has thousands of classes. It takes some time to get used to all of these classes and how to use them. Luckily Qt has a very good documentation with many useful examples includes. Most of the time you search for a class and the most common use cases are already provided as snippets. Which means you just copy and adapt these snippets. Also, Qt’s examples in the Qt source code are a great help. Make sure you have them available and searchable to make your life more productive. Do not waste time. The Qt community is always helpful. When you ask, it is very helpful to ask exact questions and provide a simple example which displays your needs. This will drastically improve the response time of others. So invest a little bit of time to make the life of others who want to help you easier :-).
Here some classes whose documentation the author thinks are a must read:
That should be enough for the beginning.
Last updated