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. Need use some eval() command
Forum Updated to NodeBB v4.3 + New Features

Need use some eval() command

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 5 Posters 1.5k Views 3 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.
  • L LCorona

    Recently installed QT Creator 4.14 with a standar free licence, for needs of the code, I need build a chain of commands into a QString and after that execute with some eval() command that is the common name for this in almost all programming languages. But this installation of QT Creator not have any package of command available that do the thing, there is not QExpressionEvaluator, neither QScriptEngine, or QDeclarativeExpressions or QJSEngine, I dont know how evaluate the QString with the command chain, for now Im completly stuck with the program.

    I found that there is some eval command in a QT Test functions documentation, but also, "eval" is not recognized by the QT Creator (file not found error).

    ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by
    #2

    @LCorona hi
    Did you install a Qt version ? QtCreator is only an IDE
    Are you albe to run un example project or create and run a new project ?

    1 Reply Last reply
    1
    • L Offline
      L Offline
      LCorona
      wrote on last edited by
      #3

      Yes, the QT is installed in the process, the QT version is 5.15.2 Im on Windows.
      Yes, Im able to run example projects, also Im able to run part of my app that is in developing.

      ODБOïO 1 Reply Last reply
      0
      • L LCorona

        Yes, the QT is installed in the process, the QT version is 5.15.2 Im on Windows.
        Yes, Im able to run example projects, also Im able to run part of my app that is in developing.

        ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by
        #4

        @LCorona
        Then in QML you can use eval() function

        var fn = "console.log(\"Hello World\")"
               eval(fn)
        

        but be aware of M23 Warning.

        Or in c++ you can use QJSEngine Class.

        1 Reply Last reply
        1
        • L Offline
          L Offline
          LCorona
          wrote on last edited by
          #5

          Ok, I put the needed instructions for use QJSEngine and apply the command evaluate, but maybe there is a missunderstanding, the evaluation returns an error, cant process the operator "->", I need to evaluate a commands that not are javascipt type, i need evaluate commands like objects assignments and other similar instrictios like this:

          GCPGraph graph1* = ui->plot1->addGraph;
          GCPGraph graph2* = ui->plot2->addGraph;
          GCPGraph graph3* = ui->plot1->addGraph;
          .
          .
          .
          etc..

          The needs are execute a repetitive set of instructions with several objects of the ui that the only way to avoid write all the code is construct the set using a QString and evaluate afte like this:

          QString command="GCPGraph graph1* = ui->plot1->addGraph; GCPGraph graph2* = ui->plot2->addGraph; GCPGraph graph3* = ui->plot1->addGraph;"
          eval(command);

          Apparently QJSEngine cant do this.

          mrjjM 1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #6

            C++ is a statically typed compiled language. There's no eval or anything like that in C++. You can't create and execute code from strings in runtime. That's what dynamic interpreted languages like javascript do.

            What you're describing sound like you just need a simple for loop over some objects.

            1 Reply Last reply
            5
            • L LCorona

              Ok, I put the needed instructions for use QJSEngine and apply the command evaluate, but maybe there is a missunderstanding, the evaluation returns an error, cant process the operator "->", I need to evaluate a commands that not are javascipt type, i need evaluate commands like objects assignments and other similar instrictios like this:

              GCPGraph graph1* = ui->plot1->addGraph;
              GCPGraph graph2* = ui->plot2->addGraph;
              GCPGraph graph3* = ui->plot1->addGraph;
              .
              .
              .
              etc..

              The needs are execute a repetitive set of instructions with several objects of the ui that the only way to avoid write all the code is construct the set using a QString and evaluate afte like this:

              QString command="GCPGraph graph1* = ui->plot1->addGraph; GCPGraph graph2* = ui->plot2->addGraph; GCPGraph graph3* = ui->plot1->addGraph;"
              eval(command);

              Apparently QJSEngine cant do this.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #7

              @LCorona
              Hi
              Can you explain what you are trying to do as
              "GCPGraph graph1* = ui->plot1->addGraph"
              makes little sense.

              I assume your are using QCustomPlot

              The GCPGraph is a class from there.

              so what should
              GCPGraph graph1* = ui->plot1->addGraph
              do ?

              You seem to call
              QCPGraph * QCustomPlot::addGraph

              so are you trying to assign some variables of type "QCPGraph *"
              from your plot1 and plot2 ??

              L 1 Reply Last reply
              0
              • mrjjM mrjj

                @LCorona
                Hi
                Can you explain what you are trying to do as
                "GCPGraph graph1* = ui->plot1->addGraph"
                makes little sense.

                I assume your are using QCustomPlot

                The GCPGraph is a class from there.

                so what should
                GCPGraph graph1* = ui->plot1->addGraph
                do ?

                You seem to call
                QCPGraph * QCustomPlot::addGraph

                so are you trying to assign some variables of type "QCPGraph *"
                from your plot1 and plot2 ??

                L Offline
                L Offline
                LCorona
                wrote on last edited by
                #8

                @mrjj
                My App have 48 widgets on UI, each widget draw a plot grid, but each widget is promoted to other class, then I need asign each widget to an QCPGraph objet with the command

                QCPGraph *graphX = ui->plotX->addGraph();

                Where X es a number for identify each object and plot grid. The task are repetitive, and is clear, that is not good write the 48 lines of code plus agregates for assign the objects and the widgets, then the solution is made it in a loop. For that the QCPGraph are in a QVector, and the lines chane by this:

                QVector<QCPGraph*>graphs(48);

                for (i=0;i<graphs.lenght();i++)
                {
                graphs[i] = ui->plotX->addGraph();
                }

                but, how iterate over the widgets???? , plotX->addGraph(), (I dont know if QT have some iterative array of the ui objects), then the idea es build in a for a command a chain of commands with all generating instructions. Also, later need other chain of instructios for generate the names of each plot.

                The main problem become for the several widgets of use, and because the promotion of them is made in the ui designer, not programatically, then iterate over the iu widgets is not possible for generalizing the code.

                jsulmJ 1 Reply Last reply
                0
                • L LCorona

                  @mrjj
                  My App have 48 widgets on UI, each widget draw a plot grid, but each widget is promoted to other class, then I need asign each widget to an QCPGraph objet with the command

                  QCPGraph *graphX = ui->plotX->addGraph();

                  Where X es a number for identify each object and plot grid. The task are repetitive, and is clear, that is not good write the 48 lines of code plus agregates for assign the objects and the widgets, then the solution is made it in a loop. For that the QCPGraph are in a QVector, and the lines chane by this:

                  QVector<QCPGraph*>graphs(48);

                  for (i=0;i<graphs.lenght();i++)
                  {
                  graphs[i] = ui->plotX->addGraph();
                  }

                  but, how iterate over the widgets???? , plotX->addGraph(), (I dont know if QT have some iterative array of the ui objects), then the idea es build in a for a command a chain of commands with all generating instructions. Also, later need other chain of instructios for generate the names of each plot.

                  The main problem become for the several widgets of use, and because the promotion of them is made in the ui designer, not programatically, then iterate over the iu widgets is not possible for generalizing the code.

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

                  @LCorona said in Need use some eval() command:

                  The task are repetitive, and is clear, that is not good write the 48

                  Then put all these ui->plotX pointers in a list and iterate over that list.

                  // Define your plots list and add the pointers to it, then:
                  for (i=0;i<graphs.lenght();i++)
                  {
                      graphs[i] = plots[i]->addGraph();
                  }
                  

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

                  1 Reply Last reply
                  3
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by mrjj
                    #10

                    Hi

                    • but, how iterate over the widgets????

                    You can use FIndChildren with QCustomPlot type to get list of all in UI

                    QList<QCustomPlot *> plots = findChildren<QCustomPlot*>();
                    
                    • the promotion of them is made in the ui designer, not programatically,

                    Actually, they are of right type in the code. the ui_xxx file generated is the right type even before runtime.
                    just try
                    ui->plot1-> and see that it suggest QCustomPlot functions.

                    So promotion does change type to the promoted one. just use compile file then code reflects the promoted type.

                    L 1 Reply Last reply
                    4
                    • mrjjM mrjj

                      Hi

                      • but, how iterate over the widgets????

                      You can use FIndChildren with QCustomPlot type to get list of all in UI

                      QList<QCustomPlot *> plots = findChildren<QCustomPlot*>();
                      
                      • the promotion of them is made in the ui designer, not programatically,

                      Actually, they are of right type in the code. the ui_xxx file generated is the right type even before runtime.
                      just try
                      ui->plot1-> and see that it suggest QCustomPlot functions.

                      So promotion does change type to the promoted one. just use compile file then code reflects the promoted type.

                      L Offline
                      L Offline
                      LCorona
                      wrote on last edited by
                      #11

                      @mrjj
                      Yes, thank you, seems good, only a last question, what is the order of elements of the QList?, the element 0 is the plot0, the element 1 is the plot1? , or depends of the order or creation in the interface?

                      mrjjM 1 Reply Last reply
                      0
                      • L LCorona

                        @mrjj
                        Yes, thank you, seems good, only a last question, what is the order of elements of the QList?, the element 0 is the plot0, the element 1 is the plot1? , or depends of the order or creation in the interface?

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @LCorona
                        hi
                        I think it comes in creation order as it searches from the top of the widget tree.
                        You can look in setupUI function to see what the order is.

                        L 1 Reply Last reply
                        1
                        • mrjjM mrjj

                          @LCorona
                          hi
                          I think it comes in creation order as it searches from the top of the widget tree.
                          You can look in setupUI function to see what the order is.

                          L Offline
                          L Offline
                          LCorona
                          wrote on last edited by
                          #13

                          @mrjj
                          Thanks, works really fine. Problem solved.

                          mrjjM 1 Reply Last reply
                          1
                          • L LCorona

                            @mrjj
                            Thanks, works really fine. Problem solved.

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #14

                            @LCorona
                            Splendid. Happy plotting :)

                            1 Reply Last reply
                            1

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved