c++ - 如何为 QtQuick 应用程序选择 OpenGL 上下文

标签 c++ qt opengl qtquick2

我正在写一个QtQuick应用程序,主要是在QML中,但我有一些部分更直接地使用OpenGL(一个OpenSceneGraph场景)。由于某些原因,我想使用 >= 3.3 的 OpenGL 版本,但 Qt 只选择了 3.0 的实现。我的代码是:

main.cpp:

#include <QApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    // Create the app.
    QApplication app(argc, argv);

    // Start the UI.
    QQmlApplicationEngine engine;
    engine.load(QUrl("main.qml"));

    return app.exec();
}

main.qml:

import QtQuick 2.7
import QtQuick.Controls 2.2

ApplicationWindow {
  id: root
  width: 800
  height: 600
  title: "App"

  visible: true

  Label {
    text: "OpenGL: " + OpenGLInfo.majorVersion + ' ' + OpenGLInfo.minorVersion + OpenGLInfo.profile
  }
}

最佳答案

一种方法是在 app.exec() 之前设置默认的 QSurfaceFormat。

#include <QSurfaceFormat>

...

QSurfaceFormat surfaceFormat;
surfaceFormat.setMajorVersion(3);
surfaceFormat.setMinorVersion(3);
surfaceFormat.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat(surfaceFormat);

关于c++ - 如何为 QtQuick 应用程序选择 OpenGL 上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55685351/

相关文章:

c++ - UTF8 文本编码 - Windows Phone 8.1 C++

c++ - 使用可见性属性(不带 __declspec(dllexport))时,Clang 能否生成导入库?

c++ - 是否可以查看 Q_SIGNALS、Q_SLOT、SLOT()、SIGNAL() 宏的定义? (Qt)

c++ - QProcess::kill() 不会在 linux 中杀死 child

c++ - 使用 GLFW 处理输入

C++语言符号分隔符

c++ - 为什么类可以定义在头文件中?

c++ - OpenGL 问题 : cannot render geometry on screen

linux - 在 Ubuntu 18.04 中为 Qt 生成独立的二进制包

c++ - 用曲线(正弦波)重新计算法线