QQuick3D Class
用于选择正确表面格式的辅助类。更多...
| Header: | #include <QQuick3D> |
静态公共成员
| QSurfaceFormat | idealSurfaceFormat(int samples = -1) |
详细说明
在将Qt Quick 3D 与OpenGL配合使用时,初始化Qt Quick 时必须采取额外步骤来定义所使用的surface format 类型。这是因为当Qt Quick 检测到正在使用3D内容时,OpenGL上下文和窗口表面已经初始化完毕。 因此提供了此辅助类,用于向Qt Quick 3D 请求理想的表面格式,以便在初始化前将其设置为Qt Quick 的默认表面。
如果在使用 OpenGL 以外的任何其他渲染后端时运行此辅助类,则它只会返回当前默认QSurfaceFormat 的副本,其中包含请求的采样。
如果在使用 OpenGL 渲染后端时运行此辅助函数,它将检测 OpenGL 版本是否足够新,并在请求时检查是否支持多采样。 通常,Qt Quick 会请求 OpenGL 2.0 或 OpenGL ES 2.0 上下文,这会限制使用Qt Quick 3D 时可用的功能,因此需要额外一步来请求功能更强大的上下文。
正确的用法模式是调用QSurfaceFormat::setDefaultFormat 来设置由QQuick3D::idealSurfaceFormat 返回的QSurfaceFormat 。关键在于,必须在构建完QGuiApplication 之后,但在加载Qt Quick 应用程序内容之前调用此方法。以下代码片段展示了正确的用法模式:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtGui>
#include <QtQuick3D/qquick3d.h>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QSurfaceFormat::setDefaultFormat(QQuick3D::idealSurfaceFormat(4));
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}另请参阅 OpenGL 相关说明。
成员函数文档
[static] QSurfaceFormat QQuick3D::idealSurfaceFormat(int samples = -1)
返回该平台的理想曲面格式。可选地,可指定 `samples ` 参数来选择用于抗锯齿的多重采样次数。
© 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.