qt common global macro definitions (qglobal)

explain

1 qt version related macros

1.1 QT_VERSION
This macro is expanded into the numerical form 0xMMNNPP (MM = major, NN = minor, PP = patch), indicating the Qt compiler version. For example, the Qt compiler version is Qt 5.9 1, then QT_VERSION is 0x050901. This macro is often used for conditional compilation settings. Different code segments are compiled according to different Qt versions.

1.2 QT_VERSION_CHECK
This macro is expanded into an integer representation of Qt version number, which is used for conditional compilation during version judgment, as shown in the following example:

#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
	#include <QtWidgets>
#else
	#include <QtGui>
#endif

1.3 QT_VERSION_STR
This macro is expanded into a string of qt version number for display. For example, set the interface title to qt version number:

    this->setWindowTitle(QT_VERSION_STR);

I use Qt5 Version 9.5:

2 memory byte order related macro (size end used by memory data)

Q_BYTE_ORDER,Q_BIG_ENDIAN and Q_LITTLE_ENDIAN
Q_BYTE_ORDER indicates the byte order used by the data in the system memory;
Q_BIG_ENDIAN indicates large end byte order;
Q_LITTLE_ ENDIAN indicates small endian byte order.
These macros are only used when it is necessary to judge the byte order of the system. Examples are as follows:

#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN 			// Whether the current system uses a small terminal
  ...
#else
  ...
#endif

3 shared library related macro definitions

3.1 Q_DECL_IMPORT

3.2 Q_DECL_EXPORT

4 virtual function overload macro

Q_DECL_OVERRIDE
In the class definition, it is used to overload a virtual function. For example, overload the virtual function paintemem() in a class. It can be defined as follows:

void paintEvent(QPaintEvent*) Q_DECL_OVERRIDE;

Use Q_ DECL_ After the override macro, if the overloaded virtual function does not perform any overload operation, the compiler will report an error.

5 do not use parameters to process macros in functions

Q_UNUSED
This macro is used to define parameters in the function that are not used in the function body. If the macro definition is not used for processing, the compiler will give a warning that the parameters are not used; Examples of using macro definitions are as follows: id is not required:

void MainWindow::on_imageSaved(int id, const QString &fileName)
{
    Q_UNUSED(id);
    ui->LabInfo->setText (fileName);
}

6 debugging macro definition

6.1 qDebug
This is the information debugging macro in qt. There are other debugging macro definitions of different levels. The source code of the macro definition in qt is as follows:

#define qDebug QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).debug
#define qInfo QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).info
#define qWarning QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).warning
#define qCritical QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).critical
#define qFatal QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).fatal

The above macro definitions are used for debugging information output, as follows:

    qDebug()<<"this is a debug example";

6.2 Q_FUNC_INFO
The function information macro contains the class and parameter information of the function. It is used as follows:

    qDebug()<<Q_FUNC_INFO<<"this is a debug example";

This macro is generally used in conjunction with qDebug to quickly locate the location of debugging information.
Debugging information output is as follows:

7 circular statement macro

7.1 forever
The definition of an endless loop macro is similar to that of while(1), and its underlying layer uses for(;) The implementation method is as follows:

    forever
    {
        ...
    }

7.2 foreach
The traversal macro of container class. I like the definition of this macro very much. It can traverse the data structures of various container classes such as linked list, vecter and hash. Use examples are as follows:

//The following circular statement is used to find the serial port that can be used
//I'm not sure how many serial ports are available, so I don't know how many times to cycle, so I use foreach (Baidu if I don't know)
foreach (const QSerialPortInfo &info,QSerialPortInfo::availablePorts())		
{
	QSerialPort serial;		//Instantiate the class inside the loop statement, because there is not necessarily only one available serial port
	serial.setPort(info);   //Set the serial port that can be used
	if(serial.open(QIODevice::ReadWrite))		//Open the serial port in ReadWrite read-write mode
	{
		//Record the available uart names serial Portname(), the UI - > portbox - > addItem in front of it doesn't matter. This is in the interface.
		ui->PortBox->addItem(serial.portName());
		//Then close the serial port, because this code opens the serial port just to find out whether the serial port can be used or not.
		serial.close();			
	}
}

The above is the core code segment of serial port search, source: https://blog.csdn.net/weixin_42887343/article/details/83866730

Note: there are many macro definitions of qt, which will be updated continuously in the future.

Keywords: C++ Qt

Added by dniry on Sun, 19 Dec 2021 04:02:14 +0200