C++ Qt OpenGL和小部件:什么';怎么了?
所以,我已经花了OpenGL一段时间,现在我想迁移到Qt,这样我就可以在我的窗口中嵌入小部件了。 因为我需要Qt小部件,所以我不想使用C++ Qt OpenGL和小部件:什么';怎么了?,c++,qt,opengl,C++,Qt,Opengl,所以,我已经花了OpenGL一段时间,现在我想迁移到Qt,这样我就可以在我的窗口中嵌入小部件了。 因为我需要Qt小部件,所以我不想使用QWindow;因此,我使用我放在容器窗口中的QGLWidget。 我确实创建了一个正确的上下文并设置了一个清晰的颜色,但我没有画任何东西 你能帮我弄明白为什么吗? (顺便说一句,我觉得我在做肮脏的事情。如果你有更干净的东西要告诉我,允许我将GL曲面作为小部件嵌入,请告诉我!) 我的主播: int main(int argc, char **argv) { Q
QWindow
;因此,我使用我放在容器窗口中的QGLWidget
。
我确实创建了一个正确的上下文并设置了一个清晰的颜色,但我没有画任何东西
你能帮我弄明白为什么吗?
(顺便说一句,我觉得我在做肮脏的事情。如果你有更干净的东西要告诉我,允许我将GL曲面作为小部件嵌入,请告诉我!)
我的主播:
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QGLFormat glFormat;
glFormat.setVersion(3, 3);
glFormat.setProfile(QGLFormat::CoreProfile); // Requires >=Qt-4.8.0
glFormat.setSampleBuffers(true);
QWidget window;
window.resize(1000, 800);
WidgetOpenGL* GL = new WidgetOpenGL(glFormat, &window);
GL->resize(500, 500);
window.show();
//GL->show();
return app.exec();
}
class WidgetOpenGL : public QGLWidget, protected QGLFunctions
{
Q_OBJECT
public:
WidgetOpenGL(const QGLFormat& format, QWidget* parent = nullptr);
protected:
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
signals:
public slots :
};
#include "widgetopengl.h"
WidgetOpenGL::WidgetOpenGL(const QGLFormat& format, QWidget* parent) : QGLWidget(format, parent), QGLFunctions()
{
makeCurrent();
initializeGLFunctions(context());
}
void WidgetOpenGL::initializeGL()
{
glClearColor(0.0f, 0.5f, 1.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
}
void WidgetOpenGL::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float vertices[] = {-0.5, -0.5, 0.0, 0.5, 0.5, -0.5};
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
}
void WidgetOpenGL::resizeGL(int width, int height)
{
int side = qMin(width, height);
glViewport((width - side) / 2, (height - side) / 2, side, side);
}
WidgetOpenGL.h:
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QGLFormat glFormat;
glFormat.setVersion(3, 3);
glFormat.setProfile(QGLFormat::CoreProfile); // Requires >=Qt-4.8.0
glFormat.setSampleBuffers(true);
QWidget window;
window.resize(1000, 800);
WidgetOpenGL* GL = new WidgetOpenGL(glFormat, &window);
GL->resize(500, 500);
window.show();
//GL->show();
return app.exec();
}
class WidgetOpenGL : public QGLWidget, protected QGLFunctions
{
Q_OBJECT
public:
WidgetOpenGL(const QGLFormat& format, QWidget* parent = nullptr);
protected:
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
signals:
public slots :
};
#include "widgetopengl.h"
WidgetOpenGL::WidgetOpenGL(const QGLFormat& format, QWidget* parent) : QGLWidget(format, parent), QGLFunctions()
{
makeCurrent();
initializeGLFunctions(context());
}
void WidgetOpenGL::initializeGL()
{
glClearColor(0.0f, 0.5f, 1.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
}
void WidgetOpenGL::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float vertices[] = {-0.5, -0.5, 0.0, 0.5, 0.5, -0.5};
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
}
void WidgetOpenGL::resizeGL(int width, int height)
{
int side = qMin(width, height);
glViewport((width - side) / 2, (height - side) / 2, side, side);
}
实施:
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QGLFormat glFormat;
glFormat.setVersion(3, 3);
glFormat.setProfile(QGLFormat::CoreProfile); // Requires >=Qt-4.8.0
glFormat.setSampleBuffers(true);
QWidget window;
window.resize(1000, 800);
WidgetOpenGL* GL = new WidgetOpenGL(glFormat, &window);
GL->resize(500, 500);
window.show();
//GL->show();
return app.exec();
}
class WidgetOpenGL : public QGLWidget, protected QGLFunctions
{
Q_OBJECT
public:
WidgetOpenGL(const QGLFormat& format, QWidget* parent = nullptr);
protected:
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
signals:
public slots :
};
#include "widgetopengl.h"
WidgetOpenGL::WidgetOpenGL(const QGLFormat& format, QWidget* parent) : QGLWidget(format, parent), QGLFunctions()
{
makeCurrent();
initializeGLFunctions(context());
}
void WidgetOpenGL::initializeGL()
{
glClearColor(0.0f, 0.5f, 1.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
}
void WidgetOpenGL::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float vertices[] = {-0.5, -0.5, 0.0, 0.5, 0.5, -0.5};
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
}
void WidgetOpenGL::resizeGL(int width, int height)
{
int side = qMin(width, height);
glViewport((width - side) / 2, (height - side) / 2, side, side);
}
最后我发现了问题所在 好吧,至少我找到了一个更好的方法来实现我想要的 我使用的是QGLWidget和QGLFunctions,它们已被弃用。
通过使用QOpenGLWidget和QopenglFunction,我得到了我想要的。这真的很简单,只要看看文档就可以了,我在这方面还不是专家,但是:小部件是否需要显式地添加到属于窗口的布局中?不知道你的意思是什么,但我确实希望小部件有一个父窗口我的意思是类似这样的(如果你愿意,可以使用QBoxLayout以外的布局类型):自动布局=新的QBoxLayout(QBoxLayout::TopToBottom);设置布局(布局);布局->添加小部件(GL)@mjwach:聪明调整大小的好方法(我不知道这个)。谢谢你。但我认为我的问题在于OpenGL或Qt的OpenGL模块