03-2_Qt 5.9 C++ Development Guide_Qt global definition, container class, iterator of container class, module of Qt class library

This article briefly introduces the data types, functions, and macro definitions in Qt's global definition; in container classes: sequential container classes, associated container classes; iterators for container classes; modules of the Qt class library .

1. Qt global definition

<QtGlobal>The header file contains some globals of the Qt class library, including basic data types, functions and macros. General Qt class library header files will include this file, so there is no need to explicitly include this header file.

1.1 Data type definition

For details, see "Qt5.9 C++ Development Guide"

1.2 Functions

<QtGlobal>The header file contains the definition of some commonly used functions. These functions mostly take template types as parameters and return corresponding template types. Template types can be replaced by any other types. If a double or float type number is used as a parameter, there are generally two parameter versions of the function of the same name, such as qFuzzyIsNull(double d) and qFuzzyIsNull(float f). For commonly used global functions, see "Qt5.9 C++ Development Guide".

1.3 Macro definition

<QtGlobal>There are many macros defined in the header file, the following are more commonly used: (only a brief introduction, see "Qt5.9 C++ Development Guide" for details)

1.3.1 QT_VERSION

This macro expands to the numerical form 0xMMNNPP (MM= major, NN = minor, PP = patch) indicating the Qt compiler version, for example, if the Qt compiler version is Qt5.9.1, then QT_VERSION is 0x050901. This macro is often used in conditional compilation settings to compile different code segments depending on the Qt version.

1.3.2 QT_VERSION_CHECK

This macro expands to an integer representation of the Qt version number.

1.3.3 QT_VERSION_STR

This macro expands to a string of the Qt version number.

1.3.4 Q_BYTE_ORDER、Q_BIG_ENDIAN 和 Q_LITTLE_ENDIAN

Q_BYTE_ORDER indicates the byte order of the data in the system memory, Q_BIG_ENDIAN indicates the big-endian byte order, and Q_LITTLE_ENDIAN indicates the little-endian byte order. It will be used when it is necessary to judge the byte order of the system.

1.3.5 Q_DECL_IMPORT 和Q_DECL_EXPORT

When using or designing a shared library, the content used to import or export the library will be introduced later.

1.3.6 Q_DECL_OVERRIDE

In the class definition, it is used to overload a virtual function. For example, the virtual function paintEvent() is overloaded in a certain class, which can be defined as follows: After using the
void paintEvent(QPaintEvent*) Q_DECL_OVERRIDE;
Q_DECL_OVERRIDE macro, if the overloaded virtual function does not perform any overloading operations, the compiler will report an error.

1.3.7 Q_DECL_FINAL

This macro defines a virtual function as final level and can no longer be overloaded, or defines a class that can no longer be inherited.

1.3.8 Q_UNUSED(name)

This macro is used to define parameters in a function that are not used in the function body.

1.3.9 foreach(variable,container)

Traversal for container classes

1.3.10 forever

Used to construct an infinite loop

1.3.11 qDebug(const char* message, …)

Display information in the debugger window, if the compiler sets Qt_NO_DEBUG_OUTPUT, no output will be made.
Similar macros include: qWarning, qCritical, qFatal, qInfoetc., which are also used to display information in the debugger window

2. Container class

2.1 Overview of container classes

  • Qt's container classes are lighter, safer, and easier to use than those in the Standard Template Library (STL). These container classes are 隐式共享consistent 可重入, and they are optimized for speed and storage, thus reducing the size of the executable. Additionally, they are thread-safe, which means they can be accessed by multiple threads as read-only containers.
  • The container class is a template-based class, such as the commonly used container class QList, T is a specific type, which can be a simple type such as int, float, or a class such as Qstring, QDate, etc., but it cannot be QObject or any of its subclasses. T must be an assignable type, that is, T must provide a default constructor, a copyable constructor and an assignment operator.
    The following is an example of defining a string list container and using it for QList:
QList<QString>aList;

aList.append("Monday");
aList.append("Tuesday");
aList.append("Wednesday");
Qstring str=aList[0]:
  • Qt's container classes are divided into 顺序容器 (sequential containers)and 关联容器 (associative containers).
    The container selection class is used to traverse the data items in the container, including Java 类型的选代类and STL 类型的送代类. The Java type iterator class is easy to use and provides advanced functionality, while the STL type iterator class is more efficient.
  • Qt also provides foreach macros for traversing all data items in the container.

2.2 Sequential container classes

The sequential container classes of Qt are QList, QLinkedList, QVector, QStack and QQueue. These container classes are introduced in the
reference link QList, QLinkedList, and QVector classes , and compared in terms of time complexity, QListwhich is the first choice .

2.2.1 QList

It is the most commonly used container class. Although it is implemented in the form of an array list (array-list) and based on index access (index-based), it is very fast to add data before or after it. QList accesses data items in the form of a subscript index.
QList functions for adding, inserting, replacing, moving, and deleting data items are: insert(), replace(), removeAt(), move(), swap(), append(), prepend(), removeFirst(), and removeLast(), etc.
QList provides a subscript index to access data items, just like an array, it also provides an at() function

2.2.2 QlinkedList

QLinkedList is a linked list (linked-list), data items are not stored in continuous memory, it accesses data items based on selectors, and the operation time of inserting and deleting data items is the same.
Except that it does not provide access to data items based on subscript index, other interface functions of QLinkedList are basically the same as QList

2.2.3 QVector

QVector provides the function of dynamic array, and accesses data with subscript index.
The function interface of QVector is almost the same as that of QList, and the performance of QVector is higher than that of QList, because the data items of QVector are stored continuously.

2.2.4 QStack

QStack is a container class that provides a stack-like last-in-first-out (LIFO) operation, and push() and pop() are the main interface functions.

QStack<int> stack;
stack.push(10);
stack.push(20);
stack.push(30);
while (!stack.isEmpty())
cout << stack.pop() << endl;

The program outputs 30, 20, 10 in sequence

2.2.5 Queue

QQueue is a container class that provides queue-like first-in-first-out (FIFO) operations. enqueue() and dequeue() are the main operation functions. For example:

QQueue<int> queue;
queue.enqueue(10);
queue.enqueue (20);
queue.enqueue(30);
while (!queue.isEmpty())
cout << queue.dequeue() << endl;

The program outputs 10, 20, 30 in sequence

2.3 Associated container classes

Qt also provides the associative container classes QMap, QMultiMap, QHash, QMultiHash and QSet. QMultiMap and QMultiHash support multiple values ​​associated with one key, and the QHash and QMultiHash classes use the hash (Hash) function for searching, which makes the search faster .

2.3.1 QSet

QSet is a collection template class based on a hash table. The order in which it stores data is uncertain, and the speed of looking up values ​​is very fast.
QSet is implemented internally with QHash

2.3.2 QMap

QMap<Key,T> provides a dictionary (associative array) mapping a key to a value. QMap stores data in the order of keys. If you don't care about the storage order, it will be faster to use QHash.

2.3.3 QMultiMap

QMultiMap is a subclass of QMap, a convenience class for handling multi-valued maps.
A multi-value map is a key that can correspond to multiple values. QMap normally does not allow multi-value mapping, unless key-value pairs are added using QMap::insertMulti0.

2.3.4 QHash

QHash is a template class based on a hash table to implement dictionary functions. The key-value pairs stored in QHash<Key,T> have a very fast lookup speed.
The function and usage of QHash and QMap are similar, the difference lies in the following points:

  • QHash is faster than QMap for searching;
  • When traversing on QMap, the data items are sorted according to the key, while the data items of QHash are in any order;
  • The keys of QMap must provide the "<" operator, and the keys of QHash must provide the "=" operator and a global hash function named qHash().

2.3.5 QMultiHash

QMultiHash is a subclass of QHash, which is a convenience class for handling multi-value mapping, and its usage is similar to QMultiMap.

3. Iteration of container classes

  • Iterators provide a unified method for accessing data items in container classes. Qt has two iterator classes: Java 类型的迭代器and STL类型的选代器.
  • The Java type iterators are easier to use and provide some advanced features, while the STL type iterators are more efficient.
  • Qt also provides a keyword foreach(actually a macro defined in <QtGlobal>) for easy access to all data items in the container.

3.1 Iterators of Java types

For each container class, there are two Java type iterators: one for read-only operations and one for read-write operations. The container class iterators of each Java type are as follows. For details, see "Qt5.9 C++ Development Guide"
insert image description here

3.2 Iterators of STL types

For each container class, there are two STL type selectors: one for read-only access and one for read-write access. Always use read-only iterators when you don't need to modify the data, because they are faster.
STL iterators are compatible with native algorithms of Qt and STL, and are optimized for speed. See the table below for specific types, and see "Qt5.9 C++ Development Guide" for specific usage.
insert image description here

3.3 foreach keyword

If you just want to iterate over all items in the container, you can use the foreach keyword. foreach is a macro defined in the header file. The syntax for using foreach is:

foreach (variable, container)

The code using foreach is more concise than using iterators. For example, the sample code for traversing a QLinkedList using foreach is as follows:

QLinkedList<QString> list;
...
QString str;
foreach (str,list)
gDebug() << str;

For other uses, see "Qt5.9 C++ Development Guide"

4. Modules of the Qt class library

A large number of classes in the Qt class library are divided into various modules according to their functions, and these modules are divided into several categories.

  • Qt Basic Module (Qt Essentials): Provides the basic functions of Qt on all platforms.
  • Qt Add-Ons (Qt Add-Ons): A module that provides added value to implement some specific functions
  • Value-Add Modules: Modules or tools released separately that provide additional value.
  • Technology Preview Modules: some modules that are in development but available as technology previews
  • Qt Tools (Qt Tools): Some tools to help application development.

Qt basic modules are the basic functions of Qt on all platforms, they are available on all development platforms and target platforms and are compatible with all versions of 源代码Ot5 二进制兼容.

Other modules are generally not automatically added to the project. If you need to use a certain module in the project, you can add this module in the project configuration. For example, if you need to use the Qt Multimedia and Qt Multimedia Widgets modules in the project, you need to add the following statement to the project configuration file:
QT += multimedia multimediawidgets
if you need to use the Qt SQL module in the project, add the following statement to the project configuration file:
QT += sql

For details, see "Qt5.9 C++ Development Guide".

Guess you like

Origin blog.csdn.net/Dasis/article/details/131621043