[Solved] QSlider throws segmentation fault on create
-
First off, I am brand new to Qt. It seems pretty slick but I can't wrap my head around this one.
I'm starting off trying to port some old Windows opengl code to Qt. I used a sample project "hellogl" to begin with. This project, as it stands works fine when I insert my OpenGl classes (well, not exactly but that's another thread.) The UI renders 3 sliders which rotate around respective axes. I wanted to add 3 more sliders to translate. I merely copied the original code and repurposed it for my needs.
When I launch, the program will create the original 3 sliders, one of the new ones and then throw a segmentation fault. Commenting out the original sliders and just calling my own will still do the same thing. Here is the code:
@
#include <QtGui>#include "GLWidget.h"
#include "window.h"Window::Window() {
glWidget = new GLWidget; xSlider = createSlider(1); //original 3 sliders ySlider = createSlider(1); zSlider = createSlider(1); xTSlider = createSlider(2); //this call returns fine yTSlider = createSlider(2); //segmentation fault zTSlider = createSlider(2);
. . .
/*note: calling :
xTSlider = createSlider(1); //this call returns fine
yTSlider = createSlider(1); //segmentation fault
zTSlider = createSlider(1);
Does the same thing
*/
//The signals / slots and desktop and widget additions follow. The code never reaches this point though.
. . .
//The create method:
QSlider *Window::createSlider(int sliderType)
{
if (sliderType == 1) {
QSlider *slider = new QSlider(Qt::Vertical);
slider->setRange(0, 360 * 16);
slider->setSingleStep(16);
slider->setPageStep(15 * 16);
slider->setTickInterval(15 * 16);
slider->setTickPosition(QSlider::TicksRight);
return slider;
}
else {
QSlider *slider = new QSlider(Qt::Horizontal);
slider->setRange(0, 800);
slider->setSingleStep(8);
slider->setPageStep(8);
slider->setTickInterval(8);
slider->setTickPosition(QSlider::TicksBelow);
return slider;
}
}
@If you notice, I pass in a flag which dictates which type of slider to create. To test this, I set the new sliders flag to "1" and the program still crashes.
Here is the header file contents:
@
#ifndef WINDOW_H
#define WINDOW_H#include <QWidget>
QT_BEGIN_NAMESPACE
class QSlider;
QT_END_NAMESPACEclass GLWidget;
class Window : public QWidget
{
Q_OBJECTpublic:
Window();protected:
void keyPressEvent(QKeyEvent *event);private:
GLWidget *glWidget; QSlider *createSlider(int sliderType); QSlider *xSlider; QSlider *ySlider; QSlider *zSlider; QSlider *xTSlider; QSlider *yTSlider; QSlider *zTSlider;
};
#endif // WINDOW_H
@The majority of this code was merely copied over from an existing project provided by Qt which works just fine. Here are the tests I ran:
- Change the sliderType param to 1 for the new sliders - fails
- Comment out the three existing sliders and attempt to create the new ones only for both sliderType params of 1 and 2 - fails.
- (This is weird) Comment out the new sliders and change the names of the old ones to the new names - fails
- Create only one new slider - fails
Unfortunately, I have core dumps turned off on my machine. I am running Debian under the Linux 2.6.32-5-686 kernel Developing on Qt(4.6.3)
Thanks!
-
Have you tried rebuilding your project?
Do you have a QApplication object at the time the sliders are created?
Did you try running your application in debug mode to find out where it actually crashes? -
Yes, it crashes when I try to rebuild it. Stepping through the debugger throws the error where commented in the code. I do have a QApplication object created: From main:
QApplication app(argc, argv);
Window window;When I step through, it seems to generate infinitely recursive "this" objects.
-
UPDATE: I can set one slider from my code without it throwing a seg fault.
-
Looks like I fixed it. Apparently I had a typo in one of the functions I was registering with the signal / slots connection. That and Lukas comment about rebuilding. I was under the impression that the green "go" button rebuilt everything but, there were probably corrupt object files that got skipped. Please flag this post as solved. Thanks!
-
Rebuilding is almost always a good idea - solves strange problems from time immemorial. ;-)
You can flag this post as solved on your own. As there is no automatism for this (yet), just edit your inital post title and prepend it with [Solved].
-
Thanks! Whew. A bit of a learning curve here but I think that the concept of signals and slots is ingenious. Who would have thought that registering event listeners in c++ can be done with under 10 lines of code!!!!