Important: Please read the Qt Code of Conduct -

[Solved] Mathematical Operations on Input Strings. A short way?

  • Hi all,

    I have an input text edit box and an output text edit box. The latters output is double the value of the input. IN order for me to do this, I executed 4 lines of code as follows:

    @ QString inputstring = ui -> A1 ->text();
    double inputstring_d = inputstring.toDouble();
    QString outputstring = QString::number (2*inputstring_d);

    It works just fine but its tedious work converting a QString to a double, doing a mathematical operation on it, then converting back to QString on output. Surely there must be a shortcut?

  • If you don't want to do all the QString conversion you should replace de QTextEdit with a "QDoubleSpinBox":

    For example:
    @ // pointers to ui objects
    QDoubleSpinBox spinBox_1 = new QDoubleSpinBox(this);
    QDoubleSpinBox spinBox_2 = new QDoubleSpinBox(this);
    // multiplication and value setting

    If you don't want the arrows just do the following:

  • Ah, okay the double spin box works better as it deals with numbers and not strings, thanks.

    However, I had to drag the boxes in the user interface as the statements QDoubleSpinBox spinBox_1 = new QDoubleSpinBox(this);
    QDoubleSpinBox spinBox_2 = new QDoubleSpinBox(this);
    give and error: conversion from 'QDoubleSpinBox*' to non-scalar type 'QDoubleSpinBox' requested

  • Lifetime Qt Champion

    There's a typo in Seba's code

  • Is there any way I can remove trailing zero's in the user input?

    Say I want 5 decimal places for accuracy and the user inputs 0.05, the widget will automatically show 0.0500.

    I searched for answers and someone mentions:
    You can subclass the QDoubleSpinBox class and reimplement textFromValue() and format the number any way you want it.

    and an other person:
    Subclass and reimplement textFromValue().

    How do I do that exactly?

  • What's your current problem? Subclassing and reimplementing methods?

  • My confusion is I dont even know what subclassing and re-implementing means. Is it possible to put a code snippet here considering the opening code?

  • Doesn't this do the job?
    @ spinBox->setDecimals(5); @

    EDIT: Sorry I now understand the problem, I have answered too fast! :)
    This should do the work:
    #include <QDoubleSpinBox>

    class mySpinBox: public QDoubleSpinBox {

    mySpinBox(QObject *_parent =0) : QDoubleSpinBox(_parent) { setDecimals(5); }

    QString textFromValue(double value) const {
    QString str = QWidget::locale().toString(value, QLatin1Char('f'), decimals());
    for(int i = str.size()-1; str[i]=='0'; i--) {

    Code not tested.

  • Lifetime Qt Champion

    Then I would recommend grabbing a good C++ book before going further. These two concepts are basics that you need to know. On a side note many examples in Qt's documentation uses both.

    Subclassing: creating a class that inherits from another one e.g. QWidget is a QObject

    Reimplementing: valid only for virtual methods, rewrite a method to do things differently from the original or do additional operations before/after calling the original implementation.

  • I agree with SGaist, you cannont work with Qt if you do not understand C++ and object-oriented programming first. I suggest the following book: Thinking in C++ by Bruce Eckel (you can find it online).

Log in to reply