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. [Solved] Mathematical Operations on Input Strings. A short way?
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 5.8k 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.
  • D Offline
    D Offline
    DeanQt
    wrote on last edited by
    #1

    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);
    ui->slot_capacity_required->setText(outputstring);@

    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?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Seba84
      wrote on last edited by
      #2

      If you don't want to do all the QString conversion you should replace de QTextEdit with a "QDoubleSpinBox":http://qt-project.org/doc/qt-5.0/qtwidgets/qdoublespinbox.html

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

      If you don't want the arrows just do the following:
      @spinBox_1->setButtonSymbols(QAbstractSpinBox::NoButtons);@

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DeanQt
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          There's a typo in Seba's code

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DeanQt
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Skyrpex
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • D Offline
                D Offline
                DeanQt
                wrote on last edited by
                #7

                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?

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Seba84
                  wrote on last edited by
                  #8

                  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--) {
                  str.remove(i,1);
                  }
                  }
                  };
                  @

                  Code not tested.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    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.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      Seba84
                      wrote on last edited by
                      #10

                      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).

                      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