Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. String to Function ?

String to Function ?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 1.1k Views 2 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.
  • J Offline
    J Offline
    JeTSpice
    wrote on last edited by
    #1

    Is there a way to convert a string to a function in pure QML?

    for instance

    function hello()
    {
        console.log("hello")
    }
    
    var s = "{console.log('hi')}"
    hello = stringToFunction(s)
    hello()//outputs "hi"
    

    It looks like Qt.createComponent() and Qt.createQmlObject() will create an entire class.
    I want to re-assign a value to a pre-existing function.

    JonBJ 1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      There is no such function.

      Perhaps this will help you, though: https://stackoverflow.com/questions/13521833/javascript-add-method-to-object

      (Z(:^

      1 Reply Last reply
      1
      • J JeTSpice

        Is there a way to convert a string to a function in pure QML?

        for instance

        function hello()
        {
            console.log("hello")
        }
        
        var s = "{console.log('hi')}"
        hello = stringToFunction(s)
        hello()//outputs "hi"
        

        It looks like Qt.createComponent() and Qt.createQmlObject() will create an entire class.
        I want to re-assign a value to a pre-existing function.

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #3

        @JeTSpice
        Alternatively, you may use JavaScript eval(), provided the JS does not "block" it and you understand that it is considered a "security risk" (though not here, as you know what the text is). For a function definition:

        eval("function hello() { console.log('hi'); }");
        
        1 Reply Last reply
        5
        • J Offline
          J Offline
          JeTSpice
          wrote on last edited by
          #4

          Thanks. Marked as solved.

          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