You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Qt Designer添加QOpenGLWidget出现QOpenGLContext运行时警告的解决方案

解决Qt Designer添加QOpenGLWidget后的QOpenGLContext/QRhiGles2错误

环境与问题

在Qt 6.5版本、VS2019编译器、Windows 10系统下,通过Qt Designer在主窗口中添加QOpenGLWidget后,控制台持续输出大量错误信息。手动通过代码创建并添加QOpenGLWidget时无此报错,尝试设置QApplication::setAttribute(Qt::AA_ForceRasterWidgets, false);无效。

报错信息

QOpenGLContext::makeCurrent() called with non-opengl surface 0x2623b7b7460
QRhiGles2: Failed to make context current. Expect bad things to happen.
QOpenGLContext::makeCurrent() called with non-opengl surface 0x2623dab3450
QRhiGles2: Failed to make context current. Expect bad things to happen.
QOpenGLContext::makeCurrent() called with non-opengl surface 0x2623b7b7460
QRhiGles2: Failed to make context current. Expect bad things to happen.

现有配置

.pro文件

QT       += core gui openglwidgets

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

mainwindow.ui文件

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <widget class="QOpenGLWidget" name="openGLWidget"/>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>17</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

mainwindow.h和mainwindow.cpp为Qt Creator默认生成文件。

解决方案

方法1:启用OpenGL上下文共享

在main.cpp中,在QApplication实例化之前添加上下文共享属性设置:

int main(int argc, char *argv[])
{
    QApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

该属性确保所有OpenGL控件共享同一个上下文,避免Qt Designer创建的控件出现上下文绑定错误。

方法2:自定义QOpenGLWidget子类并提升控件

创建自定义的QOpenGLWidget子类,实现必要的OpenGL初始化方法,然后在Qt Designer中将默认的QOpenGLWidget提升为该子类:

  1. 创建MyOpenGLWidget类:
// myopenglwidget.h
#ifndef MYOPENGLWIDGET_H
#define MYOPENGLWIDGET_H

#include <QOpenGLWidget>
#include <QOpenGLFunctions>

class MyOpenGLWidget : public QOpenGLWidget, protected QOpenGLFunctions
{
    Q_OBJECT
public:
    explicit MyOpenGLWidget(QWidget *parent = nullptr);

protected:
    void initializeGL() override;
    void paintGL() override;
    void resizeGL(int w, int h) override;
};

#endif // MYOPENGLWIDGET_H
// myopenglwidget.cpp
#include "myopenglwidget.h"

MyOpenGLWidget::MyOpenGLWidget(QWidget *parent) : QOpenGLWidget(parent)
{
}

void MyOpenGLWidget::initializeGL()
{
    initializeOpenGLFunctions();
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
}

void MyOpenGLWidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT);
}

void MyOpenGLWidget::resizeGL(int w, int h)
{
    glViewport(0, 0, w, h);
}
  1. 在Qt Designer中右键点击QOpenGLWidget,选择「提升为...」,输入类名MyOpenGLWidget并添加,确认后完成提升。

自定义子类的实现会强制触发正确的上下文初始化流程,避免Qt Designer生成的控件出现上下文绑定问题。

方法3:升级Qt版本

该错误可能是Qt 6.5的已知BUG,建议升级到Qt 6.5.1或更高版本,后续版本已修复此类上下文绑定相关问题。

内容的提问来源于stack exchange,提问作者skill_upping

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 05:30:03