Ui passing widget to class causes segmentation fault
-
I'm trying to add an QGLwidget to a widget container in a UI via the Ui's cpp file:
@AffectCube::AffectCube(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::AffectCube)
{
if (!QGLFormat::hasOpenGL()) {
cerr << "This system has no OpenGL support" << endl;
close();
}
Tetrahedron tetrahedron(ui->widget);
tetrahedron.setWindowTitle(QObject::tr("Tetrahedron"));
tetrahedron.resize(300, 300);
tetrahedron.show();ui->setupUi(this);
}@
A segmentation fault occures in the construction of the tetrahedron:
@
Tetrahedron::Tetrahedron(QWidget *parent)
: QGLWidget(parent)
{
setFormat(QGLFormat(QGL::DoubleBuffer | QGL::DepthBuffer));
rotationX = -21.0;
rotationY = -57.0;
rotationZ = 0.0;
faceColors[0] = Qt::red;
faceColors[1] = Qt::green;
faceColors[2] = Qt::blue;
faceColors[3] = Qt::yellow;}@
I'm at a loss on why this is or how to fix it... help!
-
I tried doing the same thing after setup with the same result.
-
It should be
@
AffectCube::AffectCube(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::AffectCube)
{
if (!QGLFormat::hasOpenGL()) {
cerr << "This system has no OpenGL support" << endl;
close();
}
ui->setupUi(this);
Tetrahedron tetrahedron(ui->widget);
tetrahedron.setWindowTitle(QObject::tr("Tetrahedron"));
tetrahedron.resize(300, 300);
tetrahedron.show();
}@setupUI is the method, that creates all widgets and layouts them. Using the pointers before that call is using uninitialized memory. So take care, everything can happen then... :-)
-
Ok fixed the code and allocated tetrahedron on the heap, that was my intention.
Seems to work now thanks!
-
[quote author="mcosta" date="1296741507"]I think allocate Tetrahedron on the stack is wrong.
When the AffectCube constructor returns, the Tethraedron instance is destroyed[/quote]
ripspinner wrote that already in his last comment and it works.
Please read a thread until its end and only post comments if you have something new to add. Thanks.