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 {
    ...
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM