Naberius
Naberius

Reputation: 255

Strange behavior with QT and OpenGL

When trying to draw on an OpenGL widget with QT, the window display ends up rather distorted. All that's being done is the screen is cleared.

Screenshot

#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);
}

Upvotes: 1

Views: 475

Answers (2)

G Sree Teja Simha
G Sree Teja Simha

Reputation: 505

I guess, the backend is receiving garbage into the draw buffers. Using glClear might help

Upvotes: 0

genpfault
genpfault

Reputation: 52083

Try clearing the color buffer occasionally. Note glClearColor() just latches some state and doesn't actually clear any buffers.

Upvotes: 6

Related Questions