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. Convert strings in a label to a function
QtWS25 Last Chance

Convert strings in a label to a function

Scheduled Pinned Locked Moved Solved General and Desktop
stringfunction
16 Posts 7 Posters 4.8k 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.
  • VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by
    #2

    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

    "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

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

      Hi
      Is the use case to make the app scriptable or what types of functions do you
      expect the user to supply?

      1 Reply Last reply
      0
      • W WuliTiger

        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.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #4

        @WuliTiger

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

        VRoninV 1 Reply Last reply
        0
        • JonBJ JonB

          @WuliTiger

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

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #5

          @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

          "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
          1
          • VRoninV VRonin

            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

            W Offline
            W Offline
            WuliTiger
            wrote on last edited by
            #6

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

            jsulmJ aha_1980A 2 Replies Last reply
            0
            • W WuliTiger

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

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

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

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

              1 Reply Last reply
              2
              • W WuliTiger

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

                aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @WuliTiger

                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.

                Qt has to stay free or it will die.

                VRoninV 1 Reply Last reply
                2
                • aha_1980A aha_1980

                  @WuliTiger

                  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.

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #9

                  @aha_1980 said in Convert strings in a label to a function:

                  you simply write your own interpreter

                  I love that you actually said "simply"

                  "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
                  3
                  • JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #10

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

                    W 1 Reply Last reply
                    1
                    • JonBJ JonB

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

                      W Offline
                      W Offline
                      WuliTiger
                      wrote on last edited by
                      #11

                      @JonB I have been starting to write an interpreter. lol

                      VRoninV JonBJ 2 Replies Last reply
                      0
                      • W WuliTiger

                        @JonB I have been starting to write an interpreter. lol

                        VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by
                        #12

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

                        "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
                        3
                        • W WuliTiger

                          @JonB I have been starting to write an interpreter. lol

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #13

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

                          W 1 Reply Last reply
                          3
                          • JonBJ JonB

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

                            W Offline
                            W Offline
                            WuliTiger
                            wrote on last edited by
                            #14

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

                            JKSHJ 1 Reply Last reply
                            0
                            • W WuliTiger

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

                              JKSHJ Offline
                              JKSHJ Offline
                              JKSH
                              Moderators
                              wrote on last edited by
                              #15

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

                              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                              1 Reply Last reply
                              2
                              • W Offline
                                W Offline
                                WuliTiger
                                wrote on last edited by
                                #16

                                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.

                                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