Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Segmentation fault with parent widget
Hello guys, :-)
I'm trying to connect a slider to a double variable. The slider is located in an options widget (dialog), so I call the MainWindow through parents and reach the main window, and then access the class from mainWindow that contains the variable I'm trying to change.
Whenever I move the slider, I get a crash with a segmentation fault. I changed the variable by other means and I got no crash, so I doubt that the crash is due to some bad variable (or pointer) calling. Could you guys help me with this?
So in MainWindow, I create and OptionsWidget (which is the dialog that contains the slider). I create also a GLWidget object for the openGL window that contains an object (called blochsim) which in turn contains the variable I want to edit.
So in MainWindow.cpp, I create objects for my widgets this way:
openGLApp = new GLWidget(this); // GLWidget *openGLApp; in MainWindow.h
optionsWidgetDialog = new OptionsWidget(this); // OptionsWidget *optionsWidgetDialog; in MainWindow.h
in OptionsWidget.cpp, I do the "connect" of the slider with a function that converts an integer to double, and then send this double to the variable I want to change:
sliderLength = 100000;
rotatingFieldFreqSlider = new QSlider; //QSlider rotatingFieldFreqSlider; in OptionsWidget.h
qreal OptionsWidget::getSliderDoubleValue(qreal minRealValue, qreal maxSliderValue, int bins, int sliderValue) //public member function
double result = minRealValue + ((maxSliderValue-minRealValue)/(qreal)bins) * sliderValue;
void OptionsWidget::setRotatingFieldFrequency(int value) //public slot
((MainWindow*)parentWidget())->openGLApp->blochsim->setOscillatorFreq(getSliderDoubleValue(minRotatingFreq, maxRotatingFreq, sliderLength, value));
//here comes my doubt. Is this call correct?????????
Do you guys see any problem with this code? I don't really know how to start debugging this problem!!!
Any efforts are highly appreciated!
Thank you in advance :-)
I would connect the signal to a signal of OptionsWidget. Then connect OptionsWidget signal with a slot of MainWindow and there set the value.
Don't access members of other classes in that way.
Thanks for the fast answer!
What's the difference if I access the members of other classes that way or slots that way? I'm going to, anyway, access the slot in the parents way but with "less" hierarchical steps, right? Could you please explain why it's different to do it this way?
AH MAN!!! IT WORKED WITH YOUR METHOD!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Could someone please tell me why can't I access variables that way????????
The new code is:
and the functions are located in MainWindow now!!! how could this be different??
I would do the connect like this:
If it is really a MainWindow or not does not hurt the connect.
For all other things, it was guessing, as otherwise I would need the complete code to check that.
Wow! It's interesting that it doesn't need casting to MainWindow*!!!! I thought this is necessary because QtCreator does autocompletion for the function if I do the casting!
For auto completion you need it, for the connect itself, a QObject pointer is enough, as the rest are virtual function calls :-)