Convert strings in a label to a function
-
Hello everyone,
I am trying to make a GUI about control systems. Users are able to type in the system model which are always described by a set of ODEs. I made Matlab GUI before with a built-in function 'str2fun', then I can convert the string to function with the specific unknown vector. I want to do the same thing with Qt. Could you give me suggestions? Thank you in advance.Have a good one.
-
In C++ (not just Qt), you can't. Matlab is an interpreted language while C++ needs a compiler.
You can do it in JavaScript using http://doc.qt.io/qt-5/qjsengine.html however
-
Hi
Is the use case to make the app scriptable or what types of functions do you
expect the user to supply? -
As @VRonin says, the C++ language does not allow you to map between strings and functions.
You can, of course, "emulate" (something like) this behaviour with a loop-up table which has the name of a function and a pointer to the function to execute, e.g. via a Qt
QHash
. But this is only for functions already written in the C++ code, not "generated dynamically". -
@JonB said in Convert strings in a label to a function:
the C++ language does not allow you to map between strings and functions
Qt kinda does with QMetaObject::invokeMethod but that's very limited usage and not nearly sufficient to do what OP wants
-
@VRonin The function evaluate() seems the one I need. However, JavaScript is totally new to me. I thought lambda function in C++ can do the same thing. I will post the question on other forums, and when I find the answer, I will share it with you guys.
-
@WuliTiger said in Convert strings in a label to a function:
I thought lambda function in C++ can do the same thing
No, C++ is not an interpreted language as already noted.
-
The only way to do this without using an embedded scripting language or a C++ interpreter (I heard Clang provides something in this direction), is indeed to write your own parser. If you know a bit about compiler theory (like in http://www.diku.dk/~torbenm/Basics/basics_lulu2.pdf) then you simply write your own interpreter that can evaluate whatever you want.
Note that it may take a bit more than a weekend to do so.
-
Embed a Python interpreter.
I believe this is perfectly doable/available (from C++).
The advantage is that Python/PyQt is ready-made to call Qt functions, so you gain immediate access to all things Qt in the GUI without any work!
Thinking about it, this is actually seriously possible/simple, I believe....
-
@WuliTiger said in Convert strings in a label to a function:
I have been starting to write an interpreter. lol
How about, before diving head first in this hell, you take a look at stuff like https://github.com/root-project/cling that's already done?
-
@WuliTiger said in Convert strings in a label to a function:
@JonB I have been starting to write an interpreter. lol
Also, I don't really understand what you want the interpreter for? Is it to be able to call Qt UI stuff, perhaps from within your own app you are writing in Qt, or does it have nothing to do with Qt and you just happen to be in this forum? Is it to write code? Is it to write arbitrary control systems control stuff?
My point being, it's not obvious to me why/whether you would wish to write/use any kind of C++ interpreter in the first place? It would not be my first thought for anything to "allow end users to write code" or "type in the system model"...
-
@JonB You are right. I am trying to write a GUI for arbitrary control systems. It seems useless. However, I am passionate about that. I am writing the interpret for myself. Almost done. I will post it on the forum or Github. Thanks for your kind advises.
-
@WuliTiger said in Convert strings in a label to a function:
@JonB You are right. I am trying to write a GUI for arbitrary control systems. It seems useless. However, I am passionate about that. I am writing the interpret for myself. Almost done. I will post it on the forum or Github. Thanks for your kind advises.
Is your main goal to allow the user to insert arbitrary UI/code at runtime? If so, using Qt Quick might be easier. QML files can be interpreted at run-time (although this is usually considered a security risk).
-
If you guys are still interested in the topic. See https://github.com/chalkwu/control-system.
I think it is a basic DIY solution. Cheers.