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. How can I expose a C++ function to a QJSEngine javascript enviroment?

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

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

    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-worxR 1 Reply Last reply
    0
    • S SqushiBune

      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-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @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
      3
      • S Offline
        S Offline
        SqushiBune
        wrote on last edited by
        #3

        This worked, thank you.

        Pablo J. RoginaP 1 Reply Last reply
        1
        • S SqushiBune

          This worked, thank you.

          Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on last edited by
          #4

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

          • Login

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