PDA

View Full Version : Segfault openGL



qtnewb
7th November 2010, 20:27
Hello. I am trying to write a program that uses openGL. I create a subclass of the QGLWidget class, and then run it in my main program. Here is my header file for the subclass:




#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QGLWidget>
#include <QtOpenGL>

class GLWidget : public QGLWidget
{
Q_OBJECT

public:
GLWidget(QWidget *parent = 0);
void initializeGL();
void paintGL();
};
#endif


Here is the implementation file:




#include <QtGui>
#include <QGLWidget>
#include <QtOpenGL>
#include "glwidget.h"

GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent)
{
initializeGL();
paintGL();
}

void GLWidget::initializeGL()
{
}


void GLWidget::paintGL()
{
glBegin(GL_LINE_LOOP);
glVertex3d(0,0,0);
glVertex3d(1,0,0);
glEnd();
}



Here is the main file:




#include <QApplication>
#include "glwidget.h"
#include "QDir"
#include <QtGui>
#include <compiler.h>

int main(int argc, char *argv[]) {

QApplication *app = new QApplication(argc, argv);
QWidget *central = new QWidget();
GLWidget *widget = new GLWidget(central);
widget->show();
return app->exec();
}

When I run this code on unix with x11, I get a segmentation fault (that is the exact error). If I comment out everything in paintGL(), however, I do not get a segfault. In order to compile this code, I am using cmake with the addition of qt4 elements. Is the segfault a problem with my code, or with the make file?

DIMEDROLL
27th November 2010, 11:32
That is probably because you do this in constructor:


GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent)
{
initializeGL();
paintGL();
}


You should not explicitly call to initializeGL() or paintGL(); initializeGL is called automatically, and paintGL is called when widget is redrawed, to redraw your widget manually use updateGL(), but not in constructor. You can use QTimer for automatic redrawing. Please, look in documentation and Qt sample projects to see how it is done.