QT和OpenGL的奇怪行为

时间:2011-04-06 15:58:44

标签: c++ qt opengl

当尝试使用QT绘制OpenGL小部件时,窗口显示最终会变形。 所有这一切都是屏幕被清除。

Screenshot http://img508.imageshack.us/img508/9950/screenshotofd.png

#include "glwidget.h"

// Mainwidget is a sub-class of GLWidget

GLWidget::GLWidget(QWidget *parent) :
    QGLWidget(parent)
{
}

void GLWidget::resizeGL(int width,int height)
{
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, width, height, 0, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

GLWidget::~GLWidget()
{
    ;
}

#include <QtGui>
#include "mainwidget.h"

MainWidget::MainWidget()
{
    //this->showFullScreen();
    derp = 100;
}

void MainWidget::paintGL()
{
    glClearColor(0, 0, 0, 0);
    //drawTriangle(0, 0, 100, 100, derp, 0);
    derp = rand()%500;
}

void MainWidget::initializeGL()
{
    this->resizeGL(800, 600);
}

void MainWidget::drawTriangle(int x1,int y1, int x2, int y2, int x3, int y3)
{
    glBegin(GL_TRIANGLES);
    glVertex3f(x1, y1, 0.0f);
    glVertex3f(x2, y2, 0.0f);
    glVertex3f(x3, y3, 0.0f);
glEnd();
}

#include <QtGui>
#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QWidget(parent)
{
    QGridLayout *layout = new QGridLayout;
    QTimer *timer = new QTimer(this);
    MainWidget *View = new MainWidget();
    layout->addWidget(View, 0, 0);
    setLayout(layout);
    connect(timer, SIGNAL(timeout()), View, SLOT(paintGL()));
    timer->start(50);
}

2 个答案:

答案 0 :(得分:6)

偶尔尝试clearing the color buffer。注意glClearColor()只是锁定一些状态,实际上并没有清除任何缓冲区。

答案 1 :(得分:0)

我猜,后端正在接收绘制缓冲区中的垃圾。使用glClear可能会有所帮助