سؤال

I am trying to adapt the opengl Es example "Hello GL" featured here - http://qt-project.org/doc/qt-4.8/opengl-hellogl-es.html. I am basically looking for a simple way to get a 3D graphics rendering window into a form made in Qt creator.

The first thing I tried: Grid layout is a layout I created in Qt Creator.

#include <QProcess>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QTimer>
#include "glwidget.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    GLWidget *glwidget = new GLWidget(); // This is mandatory. No problems here.
    QTimer *timer = new QTimer(this);    // Need this for the example to work.

    timer->setInterval(10); // Also necessary.

    ui->gridLayout->addWidget(glwidget);

Which compiles, but then promptly crashes with a segmentation fault.

    ui->gridLayout->addWidget(new GLWidget); 

Segfaults the same way.

The debugger points me toward line 104 of qgridlayout.h:

   inline void addWidget(QWidget *w) { QLayout::addWidget(w); }

Not sure what to make of that. Perhaps the QGLWidget wants to do something before I call ui->setupUi(this)? Perhaps it can't add the widget to the layout for some reason?

Of course if I comment out the line where I am added the widget, the program works flawlessly.

Any ideas for what's going on here?

Edit: I have fixed this. It was problem with order of operations - I called updateui too quickly.

هل كانت مفيدة؟

المحلول

The setupUi function in the generated form class initializes all variables of the form class, so using the variables before the form is initialized is undefined behaviour, since the variables contain garbage.

So the solution is to call:

ui->setupUi(this)

Before any call that uses variables in the ui object.

نصائح أخرى

I'm not sure, but I think the problem is with the following line:

ui->gridLayout->addWidget(GLWidget);

I think, you should write it as follows:

ui->gridLayout->addWidget(glwidget);
// declare glwidget as member of your class 

GLWidget *glwidget;  


//in constructor use 

glwidget = new GLWidget();  
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top