Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved How can I expose a C++ function to a QJSEngine javascript enviroment?

    QML and Qt Quick
    3
    4
    744
    Loading More Posts
    • 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
      SqushiBune last edited by

      If I have a C++ function that I want to be callable from javascript how do I add it to the enviroment, I only see functions for adding variables to the enviroment?

      What I am doing cant be done with a variable, the C++ function will write to device files in /dev to send data over a serial port.

      I have checked here:
      QJSValue
      QJSEngine

      raven-worx 1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators @SqushiBune last edited by raven-worx

        @SqushiBune
        Are you bound to QJSEngine?
        If so see this.

        But i suggest you to use QQmlEngine (also derives QJSEngine) and use:

        QObject* myObj;
        QQmlEngine engine;
        engine.rootContext()->setContextProperty("$MyObj", myObj)
        

        Anyway the method needs to be callable on the QObject, which means it either must be declared with Q_INVOKABLE or declared as slot.
        You may also read this.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply Reply Quote 3
        • S
          SqushiBune last edited by

          This worked, thank you.

          Pablo J. Rogina 1 Reply Last reply Reply Quote 1
          • Pablo J. Rogina
            Pablo J. Rogina @SqushiBune last edited by

            @SqushiBune if your issue is solved please don't forget to mark your post as such. Thanks.

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            1 Reply Last reply Reply Quote 0
            • First post
              Last post