我在主窗口上添加了一个 QOpenGLWidget 对象,这使得控制台有很多消息,例如
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文件是常规的,比如QtCreator生成的。
我尝试添加行 QApplication::setAttribute(Qt::AA_ForceRasterWidgets, false);但是无论有没有这行都会有错误:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QApplication::setAttribute(Qt::AA_ForceRasterWidgets, false);
MainWindow w;
w.show();
return a.exec();
}
然后我删除了ui并手动添加了QOpenGLWidget:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QWidget *centralWidget = new QWidget(this);
this->setCentralWidget( centralWidget );
QGridLayout * gridLayout = new QGridLayout( centralWidget );
QOpenGLWidget* ow = new QOpenGLWidget(this);
gridLayout->addWidget(ow);
}
在这种情况下,手动添加 QOpenGLWidget 时没有错误。但是没有Qt Designer做表单就不舒服了
您能建议如何摆脱有关 QOpenGLContext 和 QRhiGles2 的错误吗?
我的Qt版本是6.5,VS2019编译器,Windows 10。
考虑添加一个虚拟小部件:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) {
new QOpenGLWidget(this);
// ...
}