Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Need use some eval() command



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



  • @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 ?



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



  • @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.



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


  • Moderators

    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.


  • Lifetime Qt Champion

    @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 ??



  • @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.


  • Lifetime Qt Champion

    @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();
    }
    

  • Lifetime Qt Champion

    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.



  • @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?


  • Lifetime Qt Champion

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



  • @mrjj
    Thanks, works really fine. Problem solved.


  • Lifetime Qt Champion

    @LCorona
    Splendid. Happy plotting :)


Log in to reply