Very basic: How to send SpinBox's value to function, and use the new value



  • Hello,
    I have a big problem that I can't solve by myself. I have already spent hours searching for solution, and I am sure it's very easy.
    The important lines start from 20th line, but maybe others will be useful, so I'm showing it too.

    @#include <QtGui>
    #include "unit_converter.h"

    unit_converter::unit_converter(QWidget *parent) : QDialog(parent)
    {
    //MAIN LAYOUT'S DEFINITION
    unit_converterMainLayout = new QGridLayout;
    setLayout(unit_converterMainLayout);

    //DISTANCE'S GROUPBOX
    distanceGroupBox = new QGroupBox("Distancia");
    distanceGridLayout = new QGridLayout(distanceGroupBox);
    unit_converterMainLayout->addWidget(distanceGroupBox, 0, 0);
    a_distance = new QSpinBox;
    unitDistance = new QComboBox;
    unitDistance->addItem("Kilometros");
    distanceGridLayout->addWidget(a_distance, 0, 0);
    distanceGridLayout->addWidget(unitDistance, 0, 1);
    
    //I would like to pass a_distance's value to kmToM() function and write the new value to b
    int b;
    b = unit_converter::kmToM();
    
    //Then, I will want to use the value b in new QLabel:
    QLabel *bLabel = new QLabel("b");
    distanceGridLayout->addWidget(bLabel, 1, 0);
    

    }

    int unit_converter::kmToM(int a)
    {
    return a * 1000;
    }@

    Thank you very much for any help, and don't be angry because of my English. I hope I have explained my problem clearly enough.



  • You use QSpinBox::value() to get the current value of the QSpinBox, but it won't be very useful in the constructor (probably defaults to 0).

    You can use the valueChanged() signal connected to a slot to recompute the value as the spin box changes. Something like this;

    @
    class Example: public QWidget
    {
    Q_OBJECT
    public:
    Example(QWidget *p = 0): QWidget(p) {
    QSpinBox *spin = new QSpinBox(this);
    m_result = new QLabel("Result", this);
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(spin);
    layout->addWidget(m_result);
    setLayout(layout);

        connect(spin, SIGNAL(valueChanged(int)), SLOT(compute(int)));
        spin->setMinimum(0);
        spin->setMaximum(100);
        spin->setValue(10);
    }
    

    public slots:
    void compute(int v) {
    m_result->setText( QString::number(v * 1000) );
    }

    private:
    QLabel *m_result;
    };
    @

    Also look in Qt Assistant for the Spin Box Example


Log in to reply
 

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