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. Formula in JSON

Formula in JSON

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 843 Views
  • 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.
  • N Offline
    N Offline
    NotYourFan
    wrote on last edited by
    #1

    Hey i have a JSON like:

    .
    .
    .
    "math": "(2.0 * value_x) + 5 * 25.0"
    .
    .
    
    

    Now i want to define the value in my JSON ("value_x") in my Qt-Programm.

    in Qt try this:

    double value_x = 12.4;
    double function = value.at(0)->getValue().at(0)->math() // HERE I READ MY JSON AND I GET A STRING LIKE: "(2.0 * value_x) + 5 * 25.0"
    
    double result = HERE I WANT TO GET 2*12.4 +5 * 25.0
    
    

    Any Idea how i can define a value in JSON and get the result from the formula (also from JSON) ?

    jsulmJ 1 Reply Last reply
    0
    • N NotYourFan

      Hey i have a JSON like:

      .
      .
      .
      "math": "(2.0 * value_x) + 5 * 25.0"
      .
      .
      
      

      Now i want to define the value in my JSON ("value_x") in my Qt-Programm.

      in Qt try this:

      double value_x = 12.4;
      double function = value.at(0)->getValue().at(0)->math() // HERE I READ MY JSON AND I GET A STRING LIKE: "(2.0 * value_x) + 5 * 25.0"
      
      double result = HERE I WANT TO GET 2*12.4 +5 * 25.0
      
      

      Any Idea how i can define a value in JSON and get the result from the formula (also from JSON) ?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @NotYourFan You can replace value_x with the number in the JSON string before evaluating it.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

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

        Hi,

        Isn't that the same question as your other thread here ?

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

        J.HilkJ 1 Reply Last reply
        2
        • N Offline
          N Offline
          NotYourFan
          wrote on last edited by
          #4

          @jsulm
          i should define the "value_x" in Qt.
          "value_x" is an Input in my GUI ... so i dont know what is value_x before i defined in my GUI ....
          The next Point is that the "unknow value" (in this case value_x) has every time an other name.

          jsulmJ 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            Isn't that the same question as your other thread here ?

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @SGaist
            also very similar to his other 2 topics

            https://forum.qt.io/topic/101564/mathematische-funktion-aus-einer-json-datei-lesen-und-berechnen
            and
            https://forum.qt.io/topic/100609/berechnungen-in-qt

            @NotYourFan
            like I said before your best bet
            is a Scriptinglanguage binding
            JavaScript, Python, QML, Lua, Swift .....


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            3
            • N NotYourFan

              @jsulm
              i should define the "value_x" in Qt.
              "value_x" is an Input in my GUI ... so i dont know what is value_x before i defined in my GUI ....
              The next Point is that the "unknow value" (in this case value_x) has every time an other name.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @NotYourFan That's why I suggested to replace value_x with the number, didn't I?
              See https://doc.qt.io/qt-5/qstring.html#replace-5
              "has every time an other name" - then how do you know what you have to define?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • N Offline
                N Offline
                NotYourFan
                wrote on last edited by
                #7

                Thanks @all

                i solve my problem like this:

                #include <QScriptEngine> 
                .
                .
                
                QScriptEngine engine;
                engine.globalObject().setProperty(value.at(0)->getValue.at(0).name(), value.at(0)->getValue.at(0).math());
                qDebug() << "foo times two is:" << engine.evaluate("foo * 2").toNumber();
                
                

                https://doc.qt.io/qt-5/qtscript-index.html

                THANKS specially @J-Hilk for the bullet point "script" :)

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  You are using a bazooka to kill a fly: https://en.wikipedia.org/wiki/Shunting-yard_algorithm

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  2
                  • N Offline
                    N Offline
                    NotYourFan
                    wrote on last edited by
                    #9

                    @VRonin

                    Thank you for your Feedback.

                    I do this way because my JSON-File is every time diffrent.

                    So with this solution my formula works fine.

                    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