• 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_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 {
        ...
    }
    
  • 相关阅读:
    BNUOJ 19792 Airport Express
    Poor Hanamichi
    BNUOJ 1206 A Plug for UNIX
    HDU 3507 Print Article
    一个程序猿试用有道云笔记VIP功能体验
    Cloud Foundry Session Affinity(Sticky Session)的实现
    SAP成都研究院廖婧:SAP C4C社交媒体集成概述
    SAP订单编排和流程增强概述
    在Kubernetes上运行SAP UI5应用(上)
    Docker入门系列之三:如何将dockerfile制作好的镜像发布到Docker hub上
  • 原文地址:https://www.cnblogs.com/schips/p/framework-cpp-qt-06-global.html
Copyright © 2020-2023  润新知