"Clean code" says his best works to create your own data type definitions. Qt contains some global definitions in its QtGlobal header file. Including basic data types, functions, and macros.
type of data
Data type definition is redefinition of a basic type, it is to ensure that each platform has a unified data type of each determined length. Other definitions associated with the processing of the Qt message type.
Qt Data Types |
Equivalent definitions |
description |
QFunctionPointer |
void (*)() |
a pointer to a function that takes no arguments and returns void |
QtMessageHandler |
|
This is a typedef for a pointer to a function with the following signature: void myMessageHandler(QtMsgType, const QMessageLogContext &, const QString &); |
QtMsgType { QtDebugMsg, QtInfoMsg, QtWarningMsg, QtCriticalMsg, QtFatalMsg, QtSystemMsg } |
|
This enum describes the messages that can be sent to a message handler (QtMessageHandler) |
qint8 |
signed char |
1 byte |
qint16 |
signed short |
2 bytes |
qint32 |
signed int |
4 bytes |
qint64 |
long long int |
8 bytes |
qintptr |
signed int * |
Integral type for representing pointers in a signed integer. 32-bit or 64-bit (with internet) |
qlonglong |
long long int |
With qint64 |
qptrdiff |
|
Integral type for representing pointer differences. |
qreal |
double |
8字节, Typedef for double unless Qt is configured with the -qreal float option. |
quint8 |
unsigned char |
1 byte |
quint16 |
unsigned short |
2 bytes |
quint32 |
unsigned int |
4 bytes |
quint64 |
unsigned long long int |
8 bytes |
quintptr |
|
Integral type for representing pointers in an unsigned integer |
qulonglong |
unsigned long long int |
With quint64 |
flying |
unsigned char |
With quint8 |
uint |
unsigned int |
With quint32 |
ulong |
unsigned long |
unsigned long |
ushort |
unsigned short |
With quint16 |
function
The header file contains definitions of commonly used functions, these functions mostly as a parameter template type, template type corresponding return. These types can be used any type of template is instantiated.
function |
description |
T qAbs(const T &value) |
Returns the absolute value for integer and floating point. |
const T & qBound(const T &min, const T &value, const T &max) |
Returns the value defined in the value range of min and max |
quint32 qFloatDistance(float a, float b) |
Returns floating point between a and b. |
quint64 qFloatDistance(double a, double b) |
Ibid double |
bool qFuzzyCompare(double p1, double p2) |
If p1 and p2 approximately equal Returns true |
bool qFuzzyCompare(float p1, float p2) |
Ditto |
bool qFuzzyIsNull(double d) |
If d is approximately equal to 0, return true |
bool qFuzzyIsNull(float f) |
Ditto |
double qInf() |
返回无穷大的数 |
QtMessageHandler qInstallMessageHandler(QtMessageHandler handler) |
注册Qt Message,用于输出log信息。 |
bool qIsFinite(double d) |
若d是一个有限的数,返回true |
bool qIsFinite(float f) |
同上 |
bool qIsInf(double d) |
若d是一个无限大的数,则返回true |
bool qIsInf(float f) |
同上 |
bool qIsNaN(double d) |
若d不是一个数,则返回true |
bool qIsNaN(float f) |
同上 |
const T & qMax(const T &value1, const T &value2) |
返回v1和v2中较大的值 |
const T & qMin(const T &value1, const T &value2) |
返回v1和v2中较小的值 |
double qQNaN() |
返回quiet NaN |
qint64 qRound64(double value) |
将value近似为最接近的64位整数 |
qint64 qRound64(float value) |
同上 |
int qRound(double value) |
将value近似为最接近的32位整数 |
int qRound(float value) |
同上 |
double qSNaN() |
返回signal NaN |
T * q_check_ptr(T *pointer) |
检查指针,返回该指针 |
int qrand() |
返回0至RAND_MAX之间的伪随机数 |
void qsrand(uint seed) |
使用种子初始化随机数列 |
QString qtTrId(const char *id, int n = -1) |
字符转换,类似于tr()函数 |
宏定义
头文件中定义了很多宏定义,这里就不一一列出了,下面挑出几个常见的介绍。
宏定义 |
描述 |
QT_VERSION |
标识Qt编译器版本,如0x050901标识Qt5.9.1。 |
QT_VERSION_CHECK |
Qt版本号的征整数表示,用以条件编译设置。 |
QT_VERSION_STR |
Qt版本号对应的字符串,如“5.9.1” |
Q_BYTE_ORDER |
标识内存中存储数据的字节顺序 |
Q_BIG_ENDIAN |
大端存储 #if QBYTE_OEDER== Q_BIG_ENDIAN |
Q_LITTLE_ENDIAN |
小端存储 #if QBYTE_OEDER== Q_ LITTLE _ENDIAN |
Q_DECL_IMPORT |
在设计或使用共享库时,用以导入内容。 |
Q_DECL_EXPORT |
在设计或使用共享库时,用以导出内容。 |
Q_DECL_OVERRIDE |
用于重载一个虚函数 |
Q_DECL_FINAL |
将虚函数定义为最终级别,不能被重载。 |
Q_UNUSED(name) |
在函数中定义不在函数体内使用的参数,避免警告。 |
Q_ASSERT(bool test) |
当test为false时打印警告信息 |
Q_CHECK_PTR(void *pointer) |
检查指针,打印内存错误信息 |
Q_OS_LINUX |
在使用Linux平台编译时被定义,其他平台类似 |
Q_PROCESSOR_X86_64 |
在x86平台用64位编译器编译时被定义,其他类似 |
qDebug(const char *message, ...) |
打印调试信息,还有qCritical,qFatal,qInfo, qWarning |