QAbstractOpenGLFunctions Class
QAbstractOpenGLFunctions 类是一个类族的基类,该类族公开了每个 OpenGL 版本和配置文件的所有函数。更多内容...
| 头文件: | #include <qopenglversionfunctions.h> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS OpenGL) target_link_libraries(mytarget PRIVATE Qt6::OpenGL) |
| qmake: | QT += opengl |
| 被继承类: | 27 种类型QOpenGLFunctions_1_0、QOpenGLFunctions_1_1 、QOpenGLFunctions_1_2 、QOpenGLFunctions_1_3 、QOpenGLFunctions_1_4 、QOpenGLFunctions_1_5 、QOpenGLFunctions_2_0 、QOpenGLFunctions_2_1 、QOpenGLFunctions_3_0 、QOpenGLFunctions_3_1 、QOpenGLFunctions_3_2_Compatibility 、QOpenGLFunctions_3_2_Core 、QOpenGLFunctions_3_3_Compatibility 、QOpenGLFunctions_3_3_Core 、QOpenGLFunctions_4_0_Compatibility 、QOpenGLFunctions_4_0_Core 、QOpenGLFunctions_4_1_Compatibility 、QOpenGLFunctions_4_1_Core 、QOpenGLFunctions_4_2_Compatibility 、QOpenGLFunctions_4_2_Core 、QOpenGLFunctions_4_3_Compatibility 、QOpenGLFunctions_4_3_Core 、QOpenGLFunctions_4_4_Compatibility 、QOpenGLFunctions_4_4_Core 、QOpenGLFunctions_4_5_Compatibility 、QOpenGLFunctions_4_5_Core ,以及QOpenGLFunctions_ES2 |
公共函数
| QAbstractOpenGLFunctions(const QAbstractOpenGLFunctions &) | |
| virtual | ~QAbstractOpenGLFunctions() |
| QAbstractOpenGLFunctions & | operator=(const QAbstractOpenGLFunctions &) |
受保护函数
详细说明
不同平台上的 OpenGL 实现能够根据该平台上的 OpenGL ABI,链接到数量不定的 OpenGL 函数。例如,在 Microsoft Windows 上,仅能在构建时链接到 OpenGL 1.1 及以下版本中的函数。所有其他函数都必须在运行时解析。 对此问题的传统解决方案是使用QOpenGLContext::getProcAddress()或QOpenGLFunctions 。前者操作繁琐且容易出错,还意味着需要直接处理函数指针;而后者仅公开了 OpenGL ES 2 与桌面版 OpenGL 共有的那些函数。然而,在编写实际的 OpenGL 应用程序时,还有许多新的 OpenGL 功能非常有用。
Qt 现在提供了一组类,它们都继承自 `QAbstractOpenGLFunctions`,并通过相应的成员函数公开了所有核心 OpenGL 函数。对于每种有效的 OpenGL 版本与配置文件的组合,都对应一个类。每个类都遵循以下命名约定:
QOpenGLFunctions_<MAJOR VERSION>_<MINOR VERSION>[_PROFILE]对于 OpenGL 1.0 至 3.0 版本,由于没有配置文件,因此对应的类如下:
- QOpenGLFunctions_1_0
- QOpenGLFunctions_1_1
- QOpenGLFunctions_1_2
- QOpenGLFunctions_1_3
- QOpenGLFunctions_1_4
- QOpenGLFunctions_1_5
- QOpenGLFunctions_2_0
- QOpenGLFunctions_2_1
- QOpenGLFunctions_3_0
其中每个类都继承自 QAbstractOpenGLFunctions。
OpenGL 3.1 版本移除了许多已弃用的函数,从而使 API 变得更加简单和通用。
OpenGL 3.2 引入了配置文件的概念。目前为 OpenGL 定义了两个配置文件:Core 和 Compatibility。
Core 配置文件不包含 OpenGL 3.1 中移除的任何函数。 “兼容性”配置文件包含同版本“核心”配置文件中的所有函数,以及 OpenGL 3.1 中移除的所有函数。因此,“兼容性”配置文件类既允许使用较新的 OpenGL 功能,又允许您继续使用旧版 OpenGL 代码。对于新的 OpenGL 代码,应优先选用“核心”配置文件。
请注意,某些厂商(尤其是 Apple)并未实现兼容性配置文件。因此,如果您希望在 macOS 上使用新的 OpenGL 功能,则应确保通过 `QSurfaceFormat::setProfile()` 请求核心配置文件上下文。
Qt 为所有版本以及“Qt Core”和“兼容”配置文件的各种组合都提供了相应的类。适用于 OpenGL 3.1 至 4.3 版本的类如下:
- QOpenGLFunctions_3_1
- QOpenGLFunctions_3_2_Core
- QOpenGLFunctions_3_2_Compatibility
- QOpenGLFunctions_3_3_Core
- QOpenGLFunctions_3_3_Compatibility
- QOpenGLFunctions_4_0_Core
- QOpenGLFunctions_4_0_Compatibility
- QOpenGLFunctions_4_1_Core
- QOpenGLFunctions_4_1_Compatibility
- QOpenGLFunctions_4_2_Core
- QOpenGLFunctions_4_2_Compatibility
- QOpenGLFunctions_4_3_Core
- QOpenGLFunctions_4_3_Compatibility
其中每个类都继承自 QAbstractOpenGLFunctions。
可以通过QOpenGLVersionFunctionsFactory::get() 获取与当前使用的 OpenGL 版本和配置文件对应的类对象的指针。如果通过此方式获取,请注意QOpenGLContext 会保留该对象的所有权。这是为了使该实例能够被缓存和共享。
在调用任何公开的 OpenGL 函数之前,必须确保该对象已解析指向 OpenGL 函数的函数指针。这只需通过 initializeOpenGLFunctions() 方法对每个实例执行一次。初始化完成后,即可使用该对象调用对应版本和配置的任何 OpenGL 函数。 请注意,在某些情况下 initializeOpenGLFunctions() 可能会失败,因此请检查其返回值。初始化可能失败的情况包括:您拥有的某个版本或配置文件对应的函数对象中包含的函数,不属于用于解析函数指针的上下文所支持的范围。
如果您仅使用函数对象,则在尝试使用未包含在该版本和配置文件中的函数时,将会引发编译时错误。这显然比运行时出现未定义行为要容易调试得多。
另请参阅 QOpenGLVersionFunctionsFactory::get()。
成员函数文档
[protected] QAbstractOpenGLFunctions::QAbstractOpenGLFunctions()
创建一个 QAbstractOpenGLFunctions 对象。
[delete] QAbstractOpenGLFunctions::QAbstractOpenGLFunctions(const QAbstractOpenGLFunctions &)
复制并构造一个QAbstractOpenGLFunctions 实例。该函数已被删除。
[virtual noexcept] QAbstractOpenGLFunctions::~QAbstractOpenGLFunctions()
销毁一个QAbstractOpenGLFunctions 对象。
[delete] QAbstractOpenGLFunctions &QAbstractOpenGLFunctions::operator=(const QAbstractOpenGLFunctions &)
将other 复制并赋值给此QAbstractOpenGLFunctions 实例。该函数已被删除。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.