Rounding to selected decimal places
-
I'm trying to create a small application that
i) intakes any float value from user[in QLineEdit] ,
ii) asks to select number of places to round that value [to select from QSpinBox]
iii) show the output in QLineEditHere's my code
mainwindow.cpp#include "mainwindow.h" #include <QDebug> #include<QSpinBox> #include<QDebug> #include<QLineEdit> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { float val=0.0; //QLineEdit *user_ins = QLineEdit("Enter the values"); --> says wrong syntax qDebug() << "Enter a value" <<val; QSpinBox *sbx = new QSpinBox(this); // int a=this->x(); --> i want to shift the position of spinbox from its by default position which is (0,0) qDebug()<<a; sbx->setRange(0,10); sbx->setPrefix("Round to "); sbx->setSuffix(" decimal places"); sbx->setFixedSize(200,100); int val_spin; val_spin =sbx->value(); qDebug()<<val_spin; float output=qRound(val,val_spin); }Can someone suggest me how to achieve it?
-
I'm trying to create a small application that
i) intakes any float value from user[in QLineEdit] ,
ii) asks to select number of places to round that value [to select from QSpinBox]
iii) show the output in QLineEditHere's my code
mainwindow.cpp#include "mainwindow.h" #include <QDebug> #include<QSpinBox> #include<QDebug> #include<QLineEdit> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { float val=0.0; //QLineEdit *user_ins = QLineEdit("Enter the values"); --> says wrong syntax qDebug() << "Enter a value" <<val; QSpinBox *sbx = new QSpinBox(this); // int a=this->x(); --> i want to shift the position of spinbox from its by default position which is (0,0) qDebug()<<a; sbx->setRange(0,10); sbx->setPrefix("Round to "); sbx->setSuffix(" decimal places"); sbx->setFixedSize(200,100); int val_spin; val_spin =sbx->value(); qDebug()<<val_spin; float output=qRound(val,val_spin); }Can someone suggest me how to achieve it?
- multiply by
10 * SelectedDecimal Places - check if the value overflowed, if yes, than use the original value
- otherwise, round to integer, use your preferred method and divide the result by
10 * SelectedDecimal Places
- multiply by
-
- multiply by
10 * SelectedDecimal Places - check if the value overflowed, if yes, than use the original value
- otherwise, round to integer, use your preferred method and divide the result by
10 * SelectedDecimal Places
@J-Hilk
While this is fine, may I ask why for simplicity you do not refer the user to e.g. QString QString::number(double n, char format = 'g', int precision = 6)? - multiply by
-
@J-Hilk
While this is fine, may I ask why for simplicity you do not refer the user to e.g. QString QString::number(double n, char format = 'g', int precision = 6)?@JonB said in Rounding to selected decimal places:
@J-Hilk
While this is fine, may I ask why for simplicity you do not refer the user to e.g. QString QString::number(double n, char format = 'g', int precision = 6)?because the op's expected output is a float, and I don't like needlessly converting to and from QString :D
-
@JonB said in Rounding to selected decimal places:
@J-Hilk
While this is fine, may I ask why for simplicity you do not refer the user to e.g. QString QString::number(double n, char format = 'g', int precision = 6)?because the op's expected output is a float, and I don't like needlessly converting to and from QString :D
@J-Hilk said in Rounding to selected decimal places:
because the op's expected output is a float, and I don't like needlessly converting to and from QString
The OP writes:
iii) show the output in QLineEdit
-
@J-Hilk said in Rounding to selected decimal places:
because the op's expected output is a float, and I don't like needlessly converting to and from QString
The OP writes:
iii) show the output in QLineEdit
-
- multiply by
10 * SelectedDecimal Places - check if the value overflowed, if yes, than use the original value
- otherwise, round to integer, use your preferred method and divide the result by
10 * SelectedDecimal Places
@J-Hilk
with your suggestion
multiply by 10MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { float val=0.0; // QLineEdit *user_ins = QLineEdit("Enter a value"); --> says wrong syntax qDebug() << "Enter a value" <<val; QSpinBox *sbx = new QSpinBox(this); int a=this->x(); qDebug()<<a; sbx->setRange(0,10); sbx->setPrefix("Round to "); sbx->setSuffix(" decimal places"); sbx->setFixedSize(200,100); int val_spin; val_spin =sbx->value(); val_spin*=10; ----> multiplication by 10 qDebug()<<val_spin; }I am not able to see the result ( no output in Application Output) . I want the result tobe displayed on the window.
- multiply by
-
@J-Hilk
with your suggestion
multiply by 10MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { float val=0.0; // QLineEdit *user_ins = QLineEdit("Enter a value"); --> says wrong syntax qDebug() << "Enter a value" <<val; QSpinBox *sbx = new QSpinBox(this); int a=this->x(); qDebug()<<a; sbx->setRange(0,10); sbx->setPrefix("Round to "); sbx->setSuffix(" decimal places"); sbx->setFixedSize(200,100); int val_spin; val_spin =sbx->value(); val_spin*=10; ----> multiplication by 10 qDebug()<<val_spin; }I am not able to see the result ( no output in Application Output) . I want the result tobe displayed on the window.
@Swati777999 said in Rounding to selected decimal places:
I am not able to see the result ( no output in Application Output) .
I do not believe this. You have
qDebug()<<val_spin;; same forqDebug()<<a;. If you run under debugger/from Qt Creator you should see it there, tryqDebug() << "Hello";, do you see that?I want the result tobe displayed on the window.
What "window"? The Application Output pane?
In the long run you cannot use the Application Output pane and
qDebug()statements to do your work.QSpinBox *sbx = new QSpinBox(this);
....
val_spin =sbx->value();You cannot ask for a value from a widget as soon as you have created it, as your code does. Your code will simply get the spin box's initial value, probably 0. You have to show the widget and allow the user to interact with it, getting the value when the user has finished doing so. Your understanding/implementation of a UI with widgets and an event loop is missing.
-
@Swati777999 said in Rounding to selected decimal places:
I am not able to see the result ( no output in Application Output) .
I do not believe this. You have
qDebug()<<val_spin;; same forqDebug()<<a;. If you run under debugger/from Qt Creator you should see it there, tryqDebug() << "Hello";, do you see that?I want the result tobe displayed on the window.
What "window"? The Application Output pane?
In the long run you cannot use the Application Output pane and
qDebug()statements to do your work.QSpinBox *sbx = new QSpinBox(this);
....
val_spin =sbx->value();You cannot ask for a value from a widget as soon as you have created it, as your code does. Your code will simply get the spin box's initial value, probably 0. You have to show the widget and allow the user to interact with it, getting the value when the user has finished doing so. Your understanding/implementation of a UI with widgets and an event loop is missing.
@JonB said in Rounding to selected decimal places:
@Swati777999 said in Rounding to selected decimal places:
I am not able to see the result ( no output in Application Output) .
I do not believe this. You have
qDebug()<<val_spin;; same forqDebug()<<a;. If you run under debugger/from Qt Creator you should see it there, tryqDebug() << "Hello";, do you see that?I want the result tobe displayed on the window.
What "window"? The Application Output pane?
In the long run you cannot use the Application Output pane and
qDebug()statements to do your work.QSpinBox *sbx = new QSpinBox(this);
....
val_spin =sbx->value();You cannot ask for a value from a widget as soon as you have created it, as your code does. Your code will simply get the spin box's initial value, probably 0. You have to show the widget and allow the user to interact with it, getting the value when the user has finished doing so. Your understanding/implementation of a UI with widgets and an event loop is missing.
I am a beginner in Qt and from a non-IT/computer science background. I'm learning through various resources and of course through this platform.
I have used qDebug() at many places just to be sure at the intermediary steps if I'm going in the right direction.
By using
a, I wanted to shift QSpinBox from its default position,i.e origin (0,0) of the window.
What's the correct approach for intaking values from users, showing them on the screen, rounding it through the value chosen through Spinbox, and displaying the desired result?? -
@JonB said in Rounding to selected decimal places:
@Swati777999 said in Rounding to selected decimal places:
I am not able to see the result ( no output in Application Output) .
I do not believe this. You have
qDebug()<<val_spin;; same forqDebug()<<a;. If you run under debugger/from Qt Creator you should see it there, tryqDebug() << "Hello";, do you see that?I want the result tobe displayed on the window.
What "window"? The Application Output pane?
In the long run you cannot use the Application Output pane and
qDebug()statements to do your work.QSpinBox *sbx = new QSpinBox(this);
....
val_spin =sbx->value();You cannot ask for a value from a widget as soon as you have created it, as your code does. Your code will simply get the spin box's initial value, probably 0. You have to show the widget and allow the user to interact with it, getting the value when the user has finished doing so. Your understanding/implementation of a UI with widgets and an event loop is missing.
I am a beginner in Qt and from a non-IT/computer science background. I'm learning through various resources and of course through this platform.
I have used qDebug() at many places just to be sure at the intermediary steps if I'm going in the right direction.
By using
a, I wanted to shift QSpinBox from its default position,i.e origin (0,0) of the window.
What's the correct approach for intaking values from users, showing them on the screen, rounding it through the value chosen through Spinbox, and displaying the desired result??@Swati777999 said in Rounding to selected decimal places:
What's the correct approach for intaking values from users
Signals/slots
https://doc.qt.io/qt-5/qspinbox.html#valueChanged -
when sticking to my "solution":
int main(int argc, char *argv[]) { QApplication a(argc, argv); auto w = new QWidget; QVBoxLayout *layout = new QVBoxLayout(w); auto lineEdit = new QLineEdit(w); auto sBox = new QSpinBox(w); sBox->setRange(0,10); sBox->setPrefix("Round to"); sBox->setSuffix(" decimal places"); auto label = new QLabel(w); layout->addWidget(lineEdit); layout->addWidget(sBox); layout->addWidget(label); w->resize( 200, 50); w->show(); auto onStuffChanged =[=]()->void{ bool ok{false}; auto value = lineEdit->text().toDouble(&ok); if(!ok){ label->setText("Invalid Input"); return; } if(auto decimal = sBox->value(); decimal == 0){ label->setText(QString::number(qRound(value))); } else { auto newValue = value * (10 * decimal); newValue = qRound(newValue); newValue /= (10 * decimal); label->setText(QString::number(newValue)); } }; QObject::connect(lineEdit, &QLineEdit::textChanged, [=]()->void{onStuffChanged();}); QObject::connect(sBox, QOverload<int>::of(&QSpinBox::valueChanged), [=]()->void{onStuffChanged();}); return a.exec(); } -
@JonB said in Rounding to selected decimal places:
@Swati777999 said in Rounding to selected decimal places:
I am not able to see the result ( no output in Application Output) .
I do not believe this. You have
qDebug()<<val_spin;; same forqDebug()<<a;. If you run under debugger/from Qt Creator you should see it there, tryqDebug() << "Hello";, do you see that?I want the result tobe displayed on the window.
What "window"? The Application Output pane?
In the long run you cannot use the Application Output pane and
qDebug()statements to do your work.QSpinBox *sbx = new QSpinBox(this);
....
val_spin =sbx->value();You cannot ask for a value from a widget as soon as you have created it, as your code does. Your code will simply get the spin box's initial value, probably 0. You have to show the widget and allow the user to interact with it, getting the value when the user has finished doing so. Your understanding/implementation of a UI with widgets and an event loop is missing.
I am a beginner in Qt and from a non-IT/computer science background. I'm learning through various resources and of course through this platform.
I have used qDebug() at many places just to be sure at the intermediary steps if I'm going in the right direction.
By using
a, I wanted to shift QSpinBox from its default position,i.e origin (0,0) of the window.
What's the correct approach for intaking values from users, showing them on the screen, rounding it through the value chosen through Spinbox, and displaying the desired result??@Swati777999
I understand, but you have made a lot of posts. By now you must know the basics of how to write a Qt UI program and have it interact properly. You are asking me to write the basics of how any Qt UI works, and I don't have time to do that.Do you understand why
QSpinBox *sbx = new QSpinBox(this); .... val_spin =sbx->value();is not going to allow the user to interact with the spin box to enter a value before you read it back?
qDebug() << "Enter a value" <<val;The end user is not going to see this. Presumably you want to put that on a
QLabelnext to theQSpinBox?int a=this->x();This does nothing. If you want to move the widget --- and you are not using layouts as you ought to be --- use void QWidget::move(int x, int y).
You have created a spin box for the user to enter how many decimal places desired (
val_spin). But there is no widget for the user to enter thefloat val=0.0you have, so that stays at 0.0. -
when sticking to my "solution":
int main(int argc, char *argv[]) { QApplication a(argc, argv); auto w = new QWidget; QVBoxLayout *layout = new QVBoxLayout(w); auto lineEdit = new QLineEdit(w); auto sBox = new QSpinBox(w); sBox->setRange(0,10); sBox->setPrefix("Round to"); sBox->setSuffix(" decimal places"); auto label = new QLabel(w); layout->addWidget(lineEdit); layout->addWidget(sBox); layout->addWidget(label); w->resize( 200, 50); w->show(); auto onStuffChanged =[=]()->void{ bool ok{false}; auto value = lineEdit->text().toDouble(&ok); if(!ok){ label->setText("Invalid Input"); return; } if(auto decimal = sBox->value(); decimal == 0){ label->setText(QString::number(qRound(value))); } else { auto newValue = value * (10 * decimal); newValue = qRound(newValue); newValue /= (10 * decimal); label->setText(QString::number(newValue)); } }; QObject::connect(lineEdit, &QLineEdit::textChanged, [=]()->void{onStuffChanged();}); QObject::connect(sBox, QOverload<int>::of(&QSpinBox::valueChanged), [=]()->void{onStuffChanged();}); return a.exec(); }@J-Hilk
Thanks for your solution but QOverload seems to be creating trouble ;
auto onStuffChanged =[=]()->voidI don't understand the above notation [Is it in JavaScript ?] . Sorry for asking so basic questions as I am literally struggling with Qt and get overwhelmed to see stuff in code beyond my understanding. So sorry for troubling moderators; @JonB @jsulm with some of my basic doubts but for a self-developer like me, if I keep my doubts to myself, it won't do much benefit to me in a long run.
-
@J-Hilk
Thanks for your solution but QOverload seems to be creating trouble ;
auto onStuffChanged =[=]()->voidI don't understand the above notation [Is it in JavaScript ?] . Sorry for asking so basic questions as I am literally struggling with Qt and get overwhelmed to see stuff in code beyond my understanding. So sorry for troubling moderators; @JonB @jsulm with some of my basic doubts but for a self-developer like me, if I keep my doubts to myself, it won't do much benefit to me in a long run.
@Swati777999 what version of Qt and c++ compiler are you using ?
I don't understand the above notation [Is it in JavaScript ?]
no it's c++ its a lambda.
https://en.cppreference.com/w/cpp/language/lambda
a "local function"my example is a self containing minimal example, that fits completely into main.cpp. So I had to do some tricks and "advanced" technics, to make it all work. It's much less complex if you have an actual class with actual slots/functions to call/connect to.