Use changed spinbox-value in another void-function



  • Hi!

    I was implementing a spinbox, which worked. Now I´d like to use the value, if changed by the spinbox for a calulation in another void-function.

    @
    QGroupBox *Tab::spin()
    {
    QGroupBox *groupBox = new QGroupBox(tr("Calculation"));
    QLabel *iLabel = new QLabel(tr("Enter a value between "
    "%1 and %2:").arg(2).arg(20));
    QSpinBox *intSpinBox = new QSpinBox;
    intSpinBox->setRange(2, 20);
    intSpinBox->setSingleStep(1);
    intSpinBox->setValue(2);

    QVBoxLayout *vbox = new QVBoxLayout;
     vbox->addWidget(iLabel);
     vbox->addWidget(intSpinBox);
     vbox->addStretch(1);
     groupBox->setLayout(vbox);
     return groupBox;
    
     connect(intSpinBox, SIGNAL(valueChanged(int)), this, SLOT(setVal()));
    

    }
    ....

    void Tab::setVal(int value)
    {
    value = k;
    }

    void Tab::calculate()
    {
    double result, a;
    result = a/k;
    .....
    }
    @

    with
    @
    private:
    int k;
    @
    in the header-File.

    I´m sorry, but why it doesn´t work?

    Please let me know if you see the problem...



  • I beleive you wanted to write the following (the assigment was wrong in your original):

    @void Tab::setVal(int value) {
    k = value;
    }@

    And the parameter type of setVal is missing in your connect call.



  • Thanks so far.

    I changed:
    @
    void Tab::setVal(int value)
    {
    k = value;
    }
    @

    and:
    @
    ....
    connect(intSpinBox, SIGNAL(valueChanged(int)), this, SLOT(setVal(int)));
    }
    @

    but the "void Tab::calculate()" function still does not get the value of "k"...



  • Additionally, your connect statement is wrong. This is the correct version:

    @
    connect(intSpinBox, SIGNAL(valueChanged(int)), this, SLOT(setVal(int)));
    @

    Spot the int parameter in SLOT (setVal(int)).



  • [quote author="newe1" date="1305371816"]but the "void Tab::calculate()" function still does not get the value of "k"... [/quote]

    What does happen? Does it raise division by zero exception? Have you tried to debug it and check if the setVal function is called? And check what is the value of k in the calculate function?



  • Nothing bad happens. The connect just fails (connect returns false) and Qt prints a warning to the console indicating that there is no slot with that signature (setVal without parameters). Just try and watch :)



  • Hi again!

    Thanks Volker, I changed it. We were posting at the same time :-)

    VCsala: Well it happens nothing. The divison by zero exception is not made, because of my range 2 to 20.

    This are the private Slots:
    @
    private slots:
    void calculate();
    void setVal(int);
    @

    I´m using "k" like this:
    @
    void Tab::calculate()
    {
    double result, result1, result2, a, ....;

    if(radio1->isChecked())
    {
       result = a/k;
    }
    if(k==2)
    {
       if(radio2->isChecked())
        {
            result = a/k;               //1. 
            result1 = a/k*2;          //2.
            QString result1String = "";
            re1->setText(result1String.setNum(result));
            QString result2String = "";
            re2->setText(result2String.setNum(result1));
        }
        else
        {
           QString result1String = "";
           re1->setText(result1String.setNum(result));
           re2->setText(result1String.setNum(result));
        }
     if(k==3)
         if(radio2->isChecked())
        {
            result = a/k;         //1.
            result1 = a/k*2;    //2.
            result2 = a/k*3;    //3.
    

    .....
    @

    When I debug, I don´t get any error... but it does not get "k"...

    VCsala: How do I check if the setval function is called?

    Thanks in advance!



  • Just set a breakpoint in the function, run the app in debugger and see if it stops there or not.

    What does happen if you declare the slot to public?



  • From where is your calculate function called?



  • Hi!

    Thank you VCsala and Volker for your ideas. It took me quite some time to solve the problem but now everything runs fine.

    Finally I rebuilt everything without the GroupBox and it got the value of "k". I´m not 100% sure, why it could not get it before (maybe because of other interferring problems) but now it works. :-)

    But I didn´t declare the slots to public. Just private slots, with the code above and it gets the value of "k".

    Thanks anyway!



  • Your welcome. I am glad that you solved the problem finally!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.