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.
  • J JonB
    15 Mar 2018, 16:49

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

    V Offline
    V Offline
    VRonin
    wrote on 15 Mar 2018, 16:53 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
    • V VRonin
      15 Mar 2018, 16:12

      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 16 Mar 2018, 01:32 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.

      J A 2 Replies Last reply 16 Mar 2018, 05:58
      0
      • W WuliTiger
        16 Mar 2018, 01:32

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

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 16 Mar 2018, 05:58 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
          16 Mar 2018, 01:32

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

          A Offline
          A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on 16 Mar 2018, 07:02 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.

          V 1 Reply Last reply 16 Mar 2018, 08:15
          2
          • A aha_1980
            16 Mar 2018, 07:02

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

            V Offline
            V Offline
            VRonin
            wrote on 16 Mar 2018, 08:15 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
            • J Online
              J Online
              JonB
              wrote on 16 Mar 2018, 08:23 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 16 Mar 2018, 14:11
              1
              • J JonB
                16 Mar 2018, 08:23

                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 16 Mar 2018, 14:11 last edited by
                #11

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

                V J 2 Replies Last reply 16 Mar 2018, 14:14
                0
                • W WuliTiger
                  16 Mar 2018, 14:11

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

                  V Offline
                  V Offline
                  VRonin
                  wrote on 16 Mar 2018, 14:14 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
                    16 Mar 2018, 14:11

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

                    J Online
                    J Online
                    JonB
                    wrote on 16 Mar 2018, 14:48 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 18 Mar 2018, 02:29
                    3
                    • J JonB
                      16 Mar 2018, 14:48

                      @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 18 Mar 2018, 02:29 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.

                      J 1 Reply Last reply 18 Mar 2018, 09:47
                      0
                      • W WuliTiger
                        18 Mar 2018, 02:29

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

                        J Offline
                        J Offline
                        JKSH
                        Moderators
                        wrote on 18 Mar 2018, 09:47 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 19 Mar 2018, 02:18 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

                          14/16

                          18 Mar 2018, 02:29

                          • Login

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