我的 main.cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSurfaceFormat format;
format.setDepthBufferSize(24);
format.setStencilBufferSize(8);
format.setVersion(3, 2);
format.setProfile(QSurfaceFormat::CoreProfile);
format.setSamples(4);
QSurfaceFormat::setDefaultFormat(format);
MyGLWidget w;
w.setFormat(format);
QGraphicsScene scene;
QGraphicsProxyWidget* proxy = scene.addWidget(&w);
scene.addText("Hello");
QGraphicsView view(&scene);
view.show();
return a.exec();
}
我的 MyGLWidget.h
class MyGLWidget :
public QOpenGLWidget
, protected QOpenGLFunctions
{
public:
MyGLWidget(QWidget* parent = nullptr);
protected:
void initializeGL() override;
void resizeGL(int w, int h) override;
void paintGL() override;
};
MyGLWidget.cpp
MyGLWidget::MyGLWidget(QWidget* parent) : QOpenGLWidget(parent)
{
}
void MyGLWidget::initializeGL()
{
initializeOpenGLFunctions();
glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
}
void MyGLWidget::resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
}
void MyGLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT);
QPainter p(this);
p.drawText(rect(), Qt::AlignCenter, "Testing");
p.drawEllipse(100, 100, 100, 100);
}
似乎没有调用 void MyGLWidget::paintGL() 。我怎么称呼paintGL?我可以将其设置为自动更新渲染吗?
另外,我得到了奇怪的例外。
如果我做出这样的改变,
MyGLWidget w;
w.setFormat(format);
w.show();
//QGraphicsScene scene;
//QGraphicsProxyWidget* proxy = scene.addWidget(&w);
//scene.addText("Hello");
//QGraphicsView view(&scene);
//view.show();