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. Very basic: How to send SpinBox's value to function, and use the new value
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.9k Views 1 Watching
  • 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
    tomas373
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      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

      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