将 QGLWidget 添加到 QMainWindow

发布于 2024-12-28 13:10:15 字数 1716 浏览 1 评论 0原文

我有一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舂唻埖巳落 2025-01-04 13:10:15

glClearColor 仅设置用于清除屏幕的颜色。
要真正清除屏幕,您必须调用 glCear 函数:

glClear(GL_COLOR_BUFFER_BIT);

与 glClearDepth 相同 - 它仅设置要清除的深度值。要实际清除它,请使用相同的 glCler 函数。您可以使用一次 glClear 调用来清除颜色缓冲区和深度缓冲区,如下所示:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glClearColor only sets the color that will be used to clear the screen.
To actually clear the screen you must call glCear function:

glClear(GL_COLOR_BUFFER_BIT);

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:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文