QT学习:06 常用的全局变量与宏定义
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_ORDER、Q_BIG_ENDIAN 和 Q_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,则不作任何输出。
类似的宏还有 qWarning、qCritical、qFatal、qInfo 等,也是用于在 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 常用的全局变量与宏定义的更多相关文章
- [PHP] PHP源码常用代码中的宏定义
PHP源码常用代码宏定义:#define 宏名 字符串#表示这是一条预处理命令,所有的预处理命令都以#开头.define是预处理命令.宏名是标识符的一种,命名规则和标识符相同.字符串可以是常数.表达式 ...
- C语言学习笔记--C语言中的宏定义
1. C 语言中的宏定义 (1)#define 是预处理器处理的单元实体之一(因此,预处理器只是简单的进行替换,并不(2)#define 定义的宏可以出现在程序的任意位置(包括函数体的内部)(3)#d ...
- Qt学习笔记常用容器
主要说Qt的以下几种容器 1.QList<T> 2.QLinkedList<T> 3.Map<T> 和一些常用的容器方法的使用 qSort qCopy qFind ...
- QT学习之常用类的总结
QApplication 应用程序类 管理图形用户界面应用程序的控制流和主要设置 QPalate QLabel 标签类 提供文本或者图像的显示 QPushButton 按钮类 提供 ...
- object-c 常用判断null的宏定义,如果是null直接返回@""
#define checkNull(__X__) (__X__) == [NSNull null] || (__X__) == nil ? @"" : [NSString stri ...
- Qt 学习之路 2(65):访问网络(1)
Home / Qt 学习之路 2 / Qt 学习之路 2(65):访问网络(1) Qt 学习之路 2(65):访问网络(1) 豆子 2013年10月11日 Qt 学习之路 2 18条评论 现在 ...
- Qt 学习之路 2(4):信号槽
Home / Qt 学习之路 2 / Qt 学习之路 2(4):信号槽 Qt 学习之路 2(4):信号槽 豆子 2012年8月23日 Qt 学习之路 2 110条评论 信号槽是 Qt 框架引以 ...
- Qt学习笔记-Widget布局管理
Qt学习笔记4-Widget布局管理 以<C++ GUI Programming with Qt 4, Second Edition>为参考 实例:查找对话框 包含三个文件,f ...
- JavaScript学习06 JS事件对象
JavaScript学习06 JS事件对象 事件对象:当事件发生时,浏览器自动建立该对象,并包含该事件的类型.鼠标坐标等. 事件对象的属性:格式:event.属性. 一些说明: event代表事件的状 ...
- QT学习第1天
QT学习第一天 坚持住!! 一 Qt概述 1.Qt发展历史 (1)1991年诞生(Haavard Nord/Eirik Chambe-Eng), (2)1994年创立Troll Tech(奇趣科技) ...
随机推荐
- ES_CCS/R(一):建立集群之间的安全互信
为了能够实现 CCR(跨集群复制 Cross-cluster replication) 及 CCS(跨集群搜索 Cross-cluster search ),我们必须让集群之间能够互信,这样才可以建立 ...
- 智能体Agent-书生浦语大模型实战营学习笔记6&大语言模型10
大语言模型学习:10.智能体Agent 书生浦语大模型实战营学习笔记6 定义 即P(感知)-> P(规划)->A(行动).类似人类「做事情」的过程,Agent的核心功能,可以归纳为三个步骤 ...
- 对于Docker和Podman的一点使用经验
前言:本文会以多个实际的线上例子,分享自己对于Docker和Podman的一点使用经验及踩过的坑,希望对读者有一点帮助. 本文bash脚本初步加工后可直接使用(兼容mac和linux系统),对于关键点 ...
- radmin远程控制软件怎么样,有没有替代品
Radmin 是流行的.屡获殊荣的安全远程控制软件,它使您能够在远程计算机上实时工作,就像使用它自己的键盘和鼠标一样. 您可以从多个地方远程访问同一台计算机,是网络和管理类别中流行的远程桌面工具. R ...
- 数据库实验—DDL
使用SQL语句,在D盘的Data文件夹下,创建一个名为jxdb+学号后2位的教学管理数据库(如:学号后两位为01,则数据库名为jxdb01).把教学管理数据库文件增长参数设置为4MB,文件最大大小参数 ...
- linux 文件的特殊权限:suid sgid sticky
目录 一.关于文件的特殊权限 二.suid 三.SGID 四.Stickybit 一.关于文件的特殊权限 1.文件与目录设置不止基础权限:r,w,x,还有所谓的特殊权限.特殊权限会拥有一些" ...
- Django与前端框架协作开发实战:高效构建现代Web应用
title: Django与前端框架协作开发实战:高效构建现代Web应用 date: 2024/5/22 20:07:47 updated: 2024/5/22 20:07:47 categories ...
- Vue cli构建项目
一.创建项目 vue create hello-world 你会被提示选取一个 preset.你可以选默认的包含了基本的 Babel + ESLint 设置的 preset,也可以选"手动选 ...
- .NET 将多个程序集合并成单一程序集的 4+3 种方法
将 .NET 程序集与依赖合并到一起的方法有下面四种: 使用 .NET Core 3.0 自带的 PublishSingleFile 属性合并依赖使用 Fody使用 SourceYard 源代码包使用 ...
- nginx的11个阶段
nginx处理请求的11个阶段 阶段 模块 第一阶段 POST_READ realip 第二阶段 SERVER_REWRITE rewrite 第三阶段 FIND_CONFIG 第四阶段 REWRIT ...