---
title: framework-cpp-qt-06-常用的全局变量与宏定义
EntryName: framework-cpp-qt-06-global
date: 2020-04-10 11:18:16
categories:
tags:
- qt
- c/c++
---

章节描述:

QtGlobal头文件包含了 Qt 类库的一些全局定义,包括基本数据类型、函数和宏,一般的 Qt 类的头文件都会包含该文件,所以不用显式包含这个头文件也可以使用其中的定义。

全局变量定义

为了确保在各个平台上各数据类型都有统一确定的长度,Qt 为各种常见数据类型定义了类型符号,如 qint8 就是 signed char 的类型定义,即:typedef signed char qint8;

Qt 数据类型 等效定义 字节数
qint8 signed char 1
qint16 signed short 2
qint32 signed int 4
qint64 long long int 8
qlonglong long long int 8
quint8 unsigned char 1
quint16 unsigned short 2
quint32 unsigned int 4
quint64 unsigned long long int 8
qulonglong unsigned long long int 8
uchar unsigned char 1
ushort unsigned short 2
uint unsigned int 4
ulong unsigned long 8
qreal double 8,4
qfloat16 2

其中 qreal 缺省是 8 字节 double 类型浮点数,如果 Qt 使用 -qreal float 选项进行配置,就是 4 字节 float 类型的浮点数。

qfloat16 是 Qt 5.9.0 中新增的一个类,用于表示 16 位的浮点数,要使用 qfloat16,需要包含头文件 <QFloat16>

全局函数定义

<QtGlobal> 头文件包含一些常用函数的定义,这些函数多以模板类型作为参数,返回相应的模板类型,模板类型可以用任何其他类型替换。

若是以 double 或 float 类型数作为参数的,一般有两个参数版本的同名函数,如qFuzzyIsNull(double d) qFuzzyIsNull(float f)

函数 功能
T qAbs(const T &value) 返回变量 value 的绝对值
const T &qBound(const T &min, const T&value, const T &max) 返回 value 限定在 min 至 max 范围之内的値
bool qFuzzyComparc(doublc p1, double p2) 若 p1 和 p2 近似相等,返回 true
bool qFuzzyIsNulI(double d) 如果参数 d 约等于 0,返回 true
double qInf(() 返回无穷大的数
bool qIsFinite(double d) 若 d 是一个有限的数,返回 true
bool qIsInf(double d) 若 d 是一个无限大的数,返回 true
bool qIsNaN(double d) 若 d 不是一个数,返回 true
constT&qMax(const T&value1, const T&value2) 返回 value1 和 value2 中较大的值
const T &qMin(const T&value1, const T&value2) 返回 value1 和 value2 中较小的值
qint64 qRound64(double value) 将 value 近似为最接近的 qint64 整数
int qRound(double value) 将 value 近似为最接近的 int 整数
int qrand() 标准 C++ 中 rand() 函数的线程安全型版本,返回 0 至 RAND_MAX 之间的伪随机数
void qsrand(uint seed) 标准 C++ 中 srand() 函数的线程安全型版本,使用种子 seed 对伪随机数字序列初始化

还有一些基础的数学运算函数在 <QtMath> 头文件中定义,比如三角运算函数、弧度与角度之间的转换函数等。

全局宏定义

操作系统

Q_OS_AIX:Defined on AIX.

Q_OS_ANDROID:Defined on Android.

Q_OS_BSD4:Defined on Any BSD 4.4 system.

Q_OS_CYGWIN:Defined on Cygwin.

Q_OS_DARWIN:Defined on Darwin-based operating systems such as macOS, iOS, watchOS, and tvOS.

Q_OS_FREEBSD:Defined on FreeBSD.

Q_OS_HPUX:Defined on HP-UX.

Q_OS_HURD:Defined on GNU Hurd.

Q_OS_IOS:Defined on iOS.

Q_OS_LINUX:Defined on Linux.

Q_OS_LYNX:Defined on LynxOS.

Q_OS_MAC:Deprecated synonym for Q_OS_DARWIN. Do not use.

Q_OS_MACOS:Defined on macOS.

Q_OS_NETBSD:Defined on NetBSD.

Q_OS_OPENBSD:Defined on OpenBSD.

Q_OS_OSX:Deprecated synonym for Q_OS_MACOS. Do not use.

Q_OS_QNX:Defined on QNX Neutrino.

Q_OS_SOLARIS:Defined on Sun Solaris.

Q_OS_TVOS:Defined on tvOS.

Q_OS_UNIX:Defined on Any UNIX BSD/SYSV system.

Q_OS_WATCHOS:Defined on watchOS.

Q_OS_WIN32:Defined on 32-bit and 64-bit versions of Windows.

Q_OS_WIN64:Defined on 64-bit versions of Windows.

Q_OS_WIN:Defined on all supported versions of Windows. That is, if Q_OS_WIN32, Q_OS_WIN64, or Q_OS_WINRT is defined.

Q_OS_WINDOWS:This is a synonym for Q_OS_WIN.

Q_OS_WINRT:Defined for Windows Runtime (Windows Store apps) on Windows 8, Windows RT, and Windows Phone 8.

处理器架构

QString QSysInfo::buildCpuArchitecture()

Returns the architecture of the CPU that Qt was compiled for, in text format. Note that this may not match the actual CPU that the application is running on if there's an emulation layer or if the CPU supports multiple architectures (like x86-64 processors supporting i386 applications). To detect that, use currentCpuArchitecture().

Values returned by this function are stable and will not change over time, so applications can rely on the returned value as an identifier, except that new CPU types may be added over time.

Typical returned values are (note: list not exhaustive):

  • "arm"、"arm64"
  • "ia64"
  • "mips"、"mips64"
  • "power"、"power64"
  • "sparc""sparcv9"
  • "x86_64"、"i386"

Q_PROCESSOR_X86

Defined if the application is compiled for x86 processors. Qt currently supports two x86 variants: Q_PROCESSOR_X86_32 and Q_PROCESSOR_X86_64.

Q_PROCESSOR_S390

Defined if the application is compiled for S/390 processors. Qt supports one optional variant of S/390: Q_PROCESSOR_S390_X.

Q_PROCESSOR_ALPHA

Defined if the application is compiled for Alpha processors.

Q_PROCESSOR_ARM

Defined if the application is compiled for ARM processors. Qt currently supports three optional ARM revisions: Q_PROCESSOR_ARM_V5, Q_PROCESSOR_ARM_V6, and Q_PROCESSOR_ARM_V7.

Q_PROCESSOR_ARM_V5

Defined if the application is compiled for ARMv5 processors. The Q_PROCESSOR_ARM macro is also defined when Q_PROCESSOR_ARM_V5 is defined.

Q_PROCESSOR_ARM_V6

Defined if the application is compiled for ARMv6 processors. The Q_PROCESSOR_ARM and Q_PROCESSOR_ARM_V5 macros are also defined when Q_PROCESSOR_ARM_V6 is defined.

Q_PROCESSOR_ARM_V7

Defined if the application is compiled for ARMv7 processors. The Q_PROCESSOR_ARM, Q_PROCESSOR_ARM_V5, and Q_PROCESSOR_ARM_V6 macros are also defined when Q_PROCESSOR_ARM_V7 is defined.

Q_PROCESSOR_AVR32

Defined if the application is compiled for AVR32 processors.

Q_PROCESSOR_BLACKFIN

Defined if the application is compiled for Blackfin processors.

Q_PROCESSOR_IA64

Defined if the application is compiled for IA-64 processors. This includes all Itanium and Itanium 2 processors.

Q_PROCESSOR_MIPS

Defined if the application is compiled for MIPS processors. Qt currently supports seven MIPS revisions: Q_PROCESSOR_MIPS_I, Q_PROCESSOR_MIPS_II, Q_PROCESSOR_MIPS_III, Q_PROCESSOR_MIPS_IV, Q_PROCESSOR_MIPS_V, Q_PROCESSOR_MIPS_32, and Q_PROCESSOR_MIPS_64.

Q_PROCESSOR_MIPS_32

Defined if the application is compiled for MIPS32 processors. The Q_PROCESSOR_MIPS, Q_PROCESSOR_MIPS_I, and Q_PROCESSOR_MIPS_II macros are also defined when Q_PROCESSOR_MIPS_32 is defined.

Q_PROCESSOR_MIPS_64

Defined if the application is compiled for MIPS64 processors. The Q_PROCESSOR_MIPS, Q_PROCESSOR_MIPS_I, Q_PROCESSOR_MIPS_II, Q_PROCESSOR_MIPS_III, Q_PROCESSOR_MIPS_IV, and Q_PROCESSOR_MIPS_V macros are also defined when Q_PROCESSOR_MIPS_64 is defined.

Q_PROCESSOR_MIPS_I

Defined if the application is compiled for MIPS-I processors. The Q_PROCESSOR_MIPS macro is also defined when Q_PROCESSOR_MIPS_I is defined.

Q_PROCESSOR_MIPS_II

Defined if the application is compiled for MIPS-II processors. The Q_PROCESSOR_MIPS and Q_PROCESSOR_MIPS_I macros are also defined when Q_PROCESSOR_MIPS_II is defined.

Q_PROCESSOR_MIPS_III

Defined if the application is compiled for MIPS-III processors. The Q_PROCESSOR_MIPS, Q_PROCESSOR_MIPS_I, and Q_PROCESSOR_MIPS_II macros are also defined when Q_PROCESSOR_MIPS_III is defined.

Q_PROCESSOR_MIPS_IV

Defined if the application is compiled for MIPS-IV processors. The Q_PROCESSOR_MIPS, Q_PROCESSOR_MIPS_I, Q_PROCESSOR_MIPS_II, and Q_PROCESSOR_MIPS_III macros are also defined when Q_PROCESSOR_MIPS_IV is defined.

Q_PROCESSOR_MIPS_V

Defined if the application is compiled for MIPS-V processors. The Q_PROCESSOR_MIPS, Q_PROCESSOR_MIPS_I, Q_PROCESSOR_MIPS_II, Q_PROCESSOR_MIPS_III, and Q_PROCESSOR_MIPS_IV macros are also defined when Q_PROCESSOR_MIPS_V is defined.

Q_PROCESSOR_POWER

Defined if the application is compiled for POWER processors. Qt currently supports two Power variants: Q_PROCESSOR_POWER_32 and Q_PROCESSOR_POWER_64.

Q_PROCESSOR_POWER_32

Defined if the application is compiled for 32-bit Power processors. The Q_PROCESSOR_POWER macro is also defined when Q_PROCESSOR_POWER_32 is defined.

Q_PROCESSOR_POWER_64

Defined if the application is compiled for 64-bit Power processors. The Q_PROCESSOR_POWER macro is also defined when Q_PROCESSOR_POWER_64 is defined.

Q_PROCESSOR_S390_X

Defined if the application is compiled for S/390x processors. The Q_PROCESSOR_S390 macro is also defined when Q_PROCESSOR_S390_X is defined.

Q_PROCESSOR_SH

Defined if the application is compiled for SuperH processors. Qt currently supports one SuperH revision: Q_PROCESSOR_SH_4A.

Q_PROCESSOR_SH_4A

Defined if the application is compiled for SuperH 4A processors. The Q_PROCESSOR_SH macro is also defined when Q_PROCESSOR_SH_4A is defined.

Q_PROCESSOR_SPARC

Defined if the application is compiled for SPARC processors. Qt currently supports one optional SPARC revision: Q_PROCESSOR_SPARC_V9.

Q_PROCESSOR_SPARC_V9

Defined if the application is compiled for SPARC V9 processors. The Q_PROCESSOR_SPARC macro is also defined when Q_PROCESSOR_SPARC_V9 is defined.

Q_PROCESSOR_X86_32

Defined if the application is compiled for 32-bit x86 processors. This includes all i386, i486, i586, and i686 processors. The Q_PROCESSOR_X86 macro is also defined when Q_PROCESSOR_X86_32 is defined.

Q_PROCESSOR_X86_64

Defined if the application is compiled for 64-bit x86 processors. This includes all AMD64, Intel 64, and other x86_64/x64 processors. The Q_PROCESSOR_X86 macro is also defined when Q_PROCESSOR_X86_64 is defined.

版本有关

QT_VERSION

展开为数值形式 0xMMNNPP (MM = major, NN = minor, PP = patch) 表示 Qt 编译器版本,例如 Qt 编译器版本为 Qt 5.9.1,则 QT_VERSION 为 0x050901。这个宏常用于条件编译设置,根据 Qt 版本不同,编译不同的代码段。

#if QT_VERSION >= 0x040100
QIcon icon = style()->standardIcon(QStyle::SP_TrashIcon);
#else
QPixmap pixmap = style()->standardPixmap(QStyle::SP_TrashIcon);
Qlcon icon(pixmap);
#endif

QT_VERSION_CHECK

这个宏展开为 Qt 版本号的一个整数表示,例如:

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

QT_VERSION_STR

展开为 Qt 版本号的字符串,如“5.9.0”。

字节序有关

在需要判断系统字节序时会用到Q_BYTE_ORDERQ_BIG_ENDIANQ_LITTLE_ENDIAN

  • Q_BYTE_ORDER 表示系统内存中数据的字节序,
  • Q_BIG_ENDIAN 表示大端字节序,
  • Q_LITTLE_ ENDIAN 表示小端字节序。
#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
...
#endif

调试

qDebug(const char * message,…)

在debugger窗体显示信息,如果编译器设置了 Qt_NO_DEBUG_OUTPUT,则不作任何输出。

类似的宏还有 qWarningqCriticalqFatalqInfo 等,也是用于在 debugger 窗体显示信息。

Q_UNUSED(name)

这个宏用于在函数中定义不在函数体里使用的参数,示例如下:

void MainWindow::on_imageSaved(int id, const QString &fileName)
{
Q_UNUSED(id)
LabInfo->setText ("图片保存为:"+ fileName);
}

Q_ASSERT

在写代码的时候,难免会调试程序,确保某一个语句或运算结果符合预期,如果不是的话就将程序暂停,便于修改,那么这时候就要用到一个Qt 中的调试神器—Q_ASSERT(断言)。建议在程序中多使用断言来进行判断逻辑,有助于尽早的发现并解决程序中隐藏的错误点。

所谓的断言,其实很简单,它是一个宏定义,接受一个布尔值,当判断的语句为真时,不做任何操作,如果判断的语句为假,那么在 debug 模式下程序运行到该地方会自动断下,并弹出一个系统消息框,并且在程序输出栏会打印出断言的位置,可以快捷的进入到该位置,便于进行修改。

Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QPushButton * btn = NULL; Q_ASSERT(btn); btn = new QPushButton(this); Q_ASSERT(btn);
}

这里显示定义一个 QPushButton 对象,并初始化为 NULL,然后添加断言,运行程序后卡在出错的地方。

声明

Q_DECL_IMPORT 和 Q_DECL_EXPORT:在使用或设计共享库时,用于导入或导出库的内容,后续章节有其使用实例。

Q_DECL_OVERRIDE:在类定义中,用于重载一个虚函数,例如在某个类中重载虚函数 paintEvem(),可以定义如下:

void paintEvent(QPaintEvent*) Q_DECL_OVERRIDE;

使用 Q_DECL_OVERRIDE 宏后,如果重载的虚函数没有进行任何重载操作,编译器将会报错。

Q_DECL_FINAL:这个宏将一个虚函数定义为最终级别,不能再被重载,或定义一个类不能再被继承,示例如下:

Class QRect Q_DECL_FINAL { // QRect 不能再被继承
// ...
};

流程化

foreach(variable, container):foreach 用于容器类的遍历,例如

foreach (const QString &codecName, recorder->supportedAudioCodecs())
ui->comboCodec->addItem(codecName);

forever:forever用于构造一个无限循环,例如:

forever {
...
}

QT学习:06 常用的全局变量与宏定义的更多相关文章

  1. [PHP] PHP源码常用代码中的宏定义

    PHP源码常用代码宏定义:#define 宏名 字符串#表示这是一条预处理命令,所有的预处理命令都以#开头.define是预处理命令.宏名是标识符的一种,命名规则和标识符相同.字符串可以是常数.表达式 ...

  2. C语言学习笔记--C语言中的宏定义

    1. C 语言中的宏定义 (1)#define 是预处理器处理的单元实体之一(因此,预处理器只是简单的进行替换,并不(2)#define 定义的宏可以出现在程序的任意位置(包括函数体的内部)(3)#d ...

  3. Qt学习笔记常用容器

    主要说Qt的以下几种容器 1.QList<T> 2.QLinkedList<T> 3.Map<T> 和一些常用的容器方法的使用 qSort qCopy qFind ...

  4. QT学习之常用类的总结

    QApplication 应用程序类 管理图形用户界面应用程序的控制流和主要设置       QPalate   QLabel 标签类 提供文本或者图像的显示   QPushButton 按钮类 提供 ...

  5. object-c 常用判断null的宏定义,如果是null直接返回@""

    #define checkNull(__X__) (__X__) == [NSNull null] || (__X__) == nil ? @"" : [NSString stri ...

  6. Qt 学习之路 2(65):访问网络(1)

    Home / Qt 学习之路 2 / Qt 学习之路 2(65):访问网络(1) Qt 学习之路 2(65):访问网络(1)  豆子  2013年10月11日  Qt 学习之路 2  18条评论 现在 ...

  7. Qt 学习之路 2(4):信号槽

    Home / Qt 学习之路 2 / Qt 学习之路 2(4):信号槽 Qt 学习之路 2(4):信号槽  豆子  2012年8月23日  Qt 学习之路 2  110条评论 信号槽是 Qt 框架引以 ...

  8. Qt学习笔记-Widget布局管理

    Qt学习笔记4-Widget布局管理       以<C++ GUI Programming with Qt 4, Second Edition>为参考 实例:查找对话框 包含三个文件,f ...

  9. JavaScript学习06 JS事件对象

    JavaScript学习06 JS事件对象 事件对象:当事件发生时,浏览器自动建立该对象,并包含该事件的类型.鼠标坐标等. 事件对象的属性:格式:event.属性. 一些说明: event代表事件的状 ...

  10. QT学习第1天

    QT学习第一天  坚持住!! 一 Qt概述 1.Qt发展历史 (1)1991年诞生(Haavard Nord/Eirik Chambe-Eng), (2)1994年创立Troll Tech(奇趣科技) ...

随机推荐

  1. aspnetcore插件开发dll热加载

    该项目比较简单,只是单纯的把业务的dll模块和controller的dll做了一个动态的添加删除处理,目的就是插件开发.由于该项目过于简单,请勿吐槽.复杂的后续可以通过泛型的实体.dto等做业务和接口 ...

  2. Multisim 教程

    Multisim 教程 Multisim主要是用来做电路图绘制.仿真的程序.本教程介绍Multisim的功能和使用方法. Multisim 界面简介 Multisim是电路设计套件里完成电路图绘制和仿 ...

  3. vue3.0 yarn启动项目

    linux 系统 在root账号下 yarn install yarn run serve 启动服务 ctrl+c //暂停服务 yarn build 打包服务 在公共目录里添加配置文件 优点:这样就 ...

  4. WEB服务与NGINX(20)- nginx 实现HTTP反向代理功能

    目录 1. nginx实现反向代理功能 1.1 nginx代理功能概述 1.2 NGINX实现HTTP反向代理 1.2.1 HTTP反向代理基本功能 1.2.1.1 反向代理配置参数 1.2.1.2 ...

  5. uniapp去除button的边框

    button { border: none !important; } button::after { border: none !important; }

  6. 回顾复习x学习笔记

    从头回顾(截至搜索) #define fo(x,y,z) for(int (x)=(y);(x)<=(z);(x)++) #define foo(x,y,z) for(int (x)=(y);( ...

  7. pageoffice6 版本实现在线打开word 文件禁止保存、禁止另存、禁止打印的需求

    在实际项目需求中,有时需要限制用户的保存.另存.打印文件操作,实现此效果只需在OnPageOfficeCtrlInit或AfterDocumentOpened事件中调用js设置PageOffice控件 ...

  8. pageoffice6 实现提取数据区域为子文件(Word拆分)

    在实际的开发过程中,有时会遇到希望提取Word文档中部分内容保存为子文件的需求,PageOffice支持提取Word文档数据区域中的内容为一个Word文件流,在服务器端创建PageOffice的Wor ...

  9. 基于webapi的websocket聊天室(二)

    上一篇 - 基于webapi的websocket聊天室(一) 消息超传缓冲区的问题 在上一篇中我们定义了一个聊天室WebSocketChatRoom.但是每个游客只分配了400个字节的发言缓冲区,大概 ...

  10. 基于深度学习的入侵检测系统综述文献概述——AI科研之路

    1.研究方向的背景是什么? (1)互联网发展迅速,网络安全态势严重 (2)现在的入侵检测准确率不够高,不能适应现在的需求 2.前人做了哪方面的工作获得了什么成果? 近代: 将网络作为入侵来源之后发展( ...