QDoubleSpinBox not giving correct result

Hi, I have completed a temperature conversion C++ program. It gave me the correct converted amounts when I use lineEdit but when I use a QDoubleSpinBox it gives the result of "32 F" every time.

How can I let the QDoubleSpinBox give me the correct result? I am using Qt Creator

Header File:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#ifndef CONVERTTEMP_H
#define CONVERTTEMP_H

#include <QDialog>

namespace Ui {
class ConvertTemp;
}

class ConvertTemp : public QDialog
{
    Q_OBJECT

public:
    explicit ConvertTemp(QWidget *parent = 0);
    ~ConvertTemp();

private slots:
    void on_buttonBox_accepted();

private:
    Ui::ConvertTemp *ui;
};

#endif // CONVERTTEMP_H 


CPP File:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "converttemp.h"
#include "ui_converttemp.h"
#include <QInputDialog>
#include <QMessageBox>

ConvertTemp::ConvertTemp(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ConvertTemp)
{
    ui->setupUi(this);
}

ConvertTemp::~ConvertTemp()
{
    delete ui;
}

void ConvertTemp::on_buttonBox_accepted()
{
    QString strCelsius = ui->celsiusLine->text();
    double celsius = strCelsius.toDouble();
    double fahrenheit = (9*celsius/5)+32;

    QString strFahrenheit;
    strFahrenheit.setNum(fahrenheit, 'f', 0.0);
    //display output in QMessageBox
    QMessageBox msgBox;
    msgBox.setWindowTitle("Result");
    msgBox.setText("Temperature in Fahrenheit:");
    msgBox.setInformativeText(strFahrenheit +" °F");
    msgBox.exec();
}


Main File:
1
2
3
4
5
6
7
8
9
10
11
#include "converttemp.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    ConvertTemp w;
    w.show();

    return a.exec();
}


Last edited on
I have not used Qt Creator, but if you are always getting 32 as your output, that appears to mean that your celsius variable is always 0.0. Which then means that your strCelsius.toDouble() call is most likely failing.

So the first thing I would do is use a debugger and put a breakpoint at line 21, to examine the value of strCelsius before it is converted into a double.

You can also pass a bool by pointer as a parameter to the toDouble function, and this value will be filled with true for success, false for failure.
https://doc.qt.io/qt-5/qstring.html#toDouble
Last edited on
QDoubleSpinBox does have member text(), inherited from parent interface https://doc.qt.io/qt-5/qdoublespinbox-members.html
but it also has member double value() https://doc.qt.io/qt-5/qdoublespinbox.html

If you do use QDoubleSpinBox, then do you use text()?
Is ui->celsiusLine really a QDoubleSpinBox*?
@keskiverto i don't use text, the program accepts double values (temperature) in celsius and should print the converted Fahrenheit value in the QMessageBox
You don't?
QString strCelsius = ui->celsiusLine->text();
so what should i replace "text()" with?
double celsius = ui->celsiusLine->value();
assuming that ui->celsiusLine points to a QDoubleSpinBox.
Topic archived. No new replies allowed.