将 QGLWidget 添加到 QMainWindow
我有一个带有 QGraphicsView 的 MainWindow 类,我想将其添加到 MainWindow 中,以便我能够看到实际发生的情况。我现在想做的就是渲染一个立方体。
Main Function
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
MainWindow Ctor
MainWindow::MainWindow( QWidget *parent )
: QMainWindow( parent ),
mUi( new Ui::MainWindow ),
mDisplay( new GLWidget ),
mScene( new QGraphicsScene )
{
mUi->setupUi( this );
//mScene->addWidget( mDisplay );
QGraphicsView *graphicsView = new QGraphicsView;
//QGraphicsView::setupViewport( this );
graphicsView->setViewport( mDisplay );
graphicsView->show();
}
GLWidget::initializeGL
void GLWidget::initializeGL( void )
{
glMatrixMode( GL_COLOR );
glClearColor( 0.0, 0.0, 0.0, 1.0 );
glMatrixMode( GL_MODELVIEW );
glClearDepth( 1.0f );
glEnable( GL_VERTEX_ARRAY );
glEnable( GL_NORMAL_ARRAY );
glEnable( GL_DEPTH_TEST );
glEnable( GL_CULL_FACE );
glShadeModel( GL_SMOOTH );
glEnable( GL_LIGHTING );
glEnable( GL_LIGHT0 );
glEnable( GL_MULTISAMPLE );
static GLfloat lightPosition[ 4 ] = { 0.5, 5.0, 7.0, 1.0 };
glLightfv( GL_LIGHT0, GL_POSITION, lightPosition );
qDebug() << "GL Initialized" << '\n';
}
如您所见,glClearColor
应该将背景设置为黑屏。问题是,当我渲染它时,我看到弹出两个窗口,而不是一个。虽然 MainWindow
类有一个应该渲染 GLWidget
的窗口框架,但它似乎不是将其添加到窗口框架,而是简单地生成两个框架以及使用QGraphicsView
的窗口。两个窗口都只显示白屏;其中之一至少应该显示黑色背景,如 glClearColor
所示。
我在这里做错了什么?
I have a MainWindow class with a QGraphicsView which I'd like to add to a MainWindow, so that I'm able to see what's actually going on. All I'm trying to do right now is render a cube.
Main Function
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
MainWindow Ctor
MainWindow::MainWindow( QWidget *parent )
: QMainWindow( parent ),
mUi( new Ui::MainWindow ),
mDisplay( new GLWidget ),
mScene( new QGraphicsScene )
{
mUi->setupUi( this );
//mScene->addWidget( mDisplay );
QGraphicsView *graphicsView = new QGraphicsView;
//QGraphicsView::setupViewport( this );
graphicsView->setViewport( mDisplay );
graphicsView->show();
}
GLWidget::initializeGL
void GLWidget::initializeGL( void )
{
glMatrixMode( GL_COLOR );
glClearColor( 0.0, 0.0, 0.0, 1.0 );
glMatrixMode( GL_MODELVIEW );
glClearDepth( 1.0f );
glEnable( GL_VERTEX_ARRAY );
glEnable( GL_NORMAL_ARRAY );
glEnable( GL_DEPTH_TEST );
glEnable( GL_CULL_FACE );
glShadeModel( GL_SMOOTH );
glEnable( GL_LIGHTING );
glEnable( GL_LIGHT0 );
glEnable( GL_MULTISAMPLE );
static GLfloat lightPosition[ 4 ] = { 0.5, 5.0, 7.0, 1.0 };
glLightfv( GL_LIGHT0, GL_POSITION, lightPosition );
qDebug() << "GL Initialized" << '\n';
}
As you can see, glClearColor
is supposed to set the background to a black screen. The problem is that when I render it, I see two windows which pop up and not one. While the MainWindow
class has a window frame which is supposed to render the GLWidget
, it appears that, rather than adding it to the window frame, it simply generates both of the frame as well as the window using the QGraphicsView
. Both windows only show a white screen; one of them is at least supposed to show a black background, as the glClearColor
states.
What am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
glClearColor 仅设置用于清除屏幕的颜色。
要真正清除屏幕,您必须调用 glCear 函数:
与 glClearDepth 相同 - 它仅设置要清除的深度值。要实际清除它,请使用相同的 glCler 函数。您可以使用一次 glClear 调用来清除颜色缓冲区和深度缓冲区,如下所示:
glClearColor only sets the color that will be used to clear the screen.
To actually clear the screen you must call glCear function:
Same thing with glClearDepth - it only sets value of depth to clear with. To actually clear it use the same glCler function. You can clear color buffer and depth buffer with one glClear call like this: