Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Segmentation fault with parent widget
QtWS25 Last Chance

Segmentation fault with parent widget

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 3.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TheDestroyer
    wrote on last edited by
    #1

    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
    rotatingFieldFreqSlider->setOrientation(Qt::Horizontal);
    rotatingFieldFreqSlider->setRange(0,sliderLength);
    rotatingFieldFreqSlider->setValue(setSliderIntValue(minRotatingFreq,maxRotatingFreq,sliderLength,((MainWindow
    )parentWidget())->openGLApp->blochsim->getOscillatorFreq()));

    connect(rotatingFieldFreqSlider,SIGNAL(valueChanged(int)),this,SLOT(setRotatingFieldFrequency(int)));
    qreal OptionsWidget::getSliderDoubleValue(qreal minRealValue, qreal maxSliderValue, int bins, int sliderValue) //public member function
    {
    double result = minRealValue + ((maxSliderValue-minRealValue)/(qreal)bins) * sliderValue;
    return result;
    }
    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 :-)

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      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.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • T Offline
        T Offline
        TheDestroyer
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • T Offline
          T Offline
          TheDestroyer
          wrote on last edited by
          #4

          AH MAN!!! IT WORKED WITH YOUR METHOD!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

          Could someone please tell me why can't I access variables that way????????

          The new code is:

          @
          connect(rotatingFieldFreqSlider,SIGNAL(valueChanged(int)),((MainWindow*)parentWidget()),SLOT(setRotatingFieldFrequency(int)));
          @

          and the functions are located in MainWindow now!!! how could this be different??

          Thaaaaaaaaaaaaaaaaaaaaanks :D

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            I would do the connect like this:

            @
            connect(rotatingFieldFreqSlider,SIGNAL(valueChanged(int)),parentWidget(),SLOT(setRotatingFieldFrequency(int)));
            @

            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.

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • T Offline
              T Offline
              TheDestroyer
              wrote on last edited by
              #6

              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!

              Thanks man!!!

              1 Reply Last reply
              0
              • G Offline
                G Offline
                giesbert
                wrote on last edited by
                #7

                For auto completion you need it, for the connect itself, a QObject pointer is enough, as the rest are virtual function calls :-)

                Nokia Certified Qt Specialist.
                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved