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. executing contents of QString?
Qt 6.11 is out! See what's new in the release blog

executing contents of QString?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.3k 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.
  • S Offline
    S Offline
    Scottish_Jason2
    wrote on last edited by
    #1

    Hi guys.

    I have a little problem here I am trying to work. If I have for example the following string:

    QString rule = "if (a=1 && b=1) {c=1}";

    Is there any way I can get my application to actually execute this if statement inside a loop?
    I have done this before using linux bash and PHP but not sure how it's done using QT.

    Thanks guys

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

      Hi and welcome
      You can have a look at
      http://doc.qt.io/qt-5/qtscript-index.html

      It would also be possible to run a bash script from Qt program and grab any output so it
      depends on what you need.

      S 1 Reply Last reply
      1
      • mrjjM mrjj

        Hi and welcome
        You can have a look at
        http://doc.qt.io/qt-5/qtscript-index.html

        It would also be possible to run a bash script from Qt program and grab any output so it
        depends on what you need.

        S Offline
        S Offline
        Scottish_Jason2
        wrote on last edited by
        #3

        @mrjj

        Hi thanks for the link. I shall read it in full now but I'm a bit anxious as it does look complicated.
        An external bash script would not work as I need QT to execute this code using it's local variables. These variables are read from a database and then concatenated into that rule string as shown above. I simply want to get it to execute the contents.

        Anyway thanks again. I will look into this scripting stuff

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

          C++ is not a scripting language. It is compiled into a binary and you can't execute arbitrary code at runtime like in bash or php.
          To have it evaluated at runtime you would have to bulk a c++ compiler with your app, put the string into some executable unit like a function, compile it, link into a library, load and execute.

          The only other solution is scripting i.e. using another language that has an interpreter evaluating expressions. One such language is JavaScript that Qt has some support for. Be aware that QtScript, as suggested by @mrjj, is not really maintained anymore and is going to be removed from Qt soon. The replacement for it is possibly going to be QJSEngine, although it's still under development and there's no feature parity yet.

          S 1 Reply Last reply
          1
          • Chris KawaC Chris Kawa

            C++ is not a scripting language. It is compiled into a binary and you can't execute arbitrary code at runtime like in bash or php.
            To have it evaluated at runtime you would have to bulk a c++ compiler with your app, put the string into some executable unit like a function, compile it, link into a library, load and execute.

            The only other solution is scripting i.e. using another language that has an interpreter evaluating expressions. One such language is JavaScript that Qt has some support for. Be aware that QtScript, as suggested by @mrjj, is not really maintained anymore and is going to be removed from Qt soon. The replacement for it is possibly going to be QJSEngine, although it's still under development and there's no feature parity yet.

            S Offline
            S Offline
            Scottish_Jason2
            wrote on last edited by
            #5

            @Chris-Kawa

            Ok understood. I appreciate your time. I will just not bother with this particular functionality

            1 Reply Last reply
            0

            • Login

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