Qt Forum

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

    Update: Forum Guidelines & Code of Conduct

    JavaScript types in C++

    Qt WebKit
    3
    5
    4683
    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.
    • K
      kyrie last edited by

      Hi all!

      I have a number of JavaScript events (like MouseMoved, etc.), which brings as parameter some JavaScript type (like JSPoint, for example). I want to emit C++ signals from JavaScript code, so I install object using addToJavaScriptWindowObject() with a number of signals, and then simply call them in js code. Everything works fine for build-in types like QString.
      Question: how can I instruct Qt to perform auto-conversion for the signal arguments types? For example, I want to create C++ type CPoint, and I want Qt to convert JSPoint to CPoint when I emit signal.
      I think that this problem may be solved using QMetaType - register new type - but what else? I found articles in documentation, but they tell how to make C++ object accessible in script envirinment (js or Qt script).

      Any ideas?

      1 Reply Last reply Reply Quote 0
      • D
        danilocesar last edited by

        Can you transform them to json objects?

        They will be recognized by c++ as QVariantMap.

        <a href="http://www.danilocesar.com">Danilo Cesar Lemes de Paula</a>
        Software Engineer

        1 Reply Last reply Reply Quote 0
        • B
          benjamin.poulain last edited by

          bq. For example, I want to create C++ type CPoint, and I want Qt to convert JSPoint to CPoint when I emit signal.

          This is not possible. Think about it, how would we implement that?

          1 Reply Last reply Reply Quote 0
          • K
            kyrie last edited by

            As 'fixme' solution I use conversion to string before emit, then emit QString, and then decode data from string. I am lucky that all JavaScript types what I need have 'toString()' method.
            Conversion to json objects is not suitable, but I knew nothing about this variant, thanks a lot.
            Concerning the possible implementation, quick dirty idea is to allow conversion to those types which are registered in Qt metatype system. Valid type is selected according to name comparison. The same way we select qt property in C++ object correspondent to js object field: field name comparison. But before generating ideas -) , it's better to find how Qt now supports conversion between some build-in types, for example - QString - I don't know how it was done.

            Thanks to all.

            1 Reply Last reply Reply Quote 0
            • B
              benjamin.poulain last edited by

              bq. it’s better to find how Qt now supports conversion between some build-in types, for example – QString – I don’t know how it was done.

              Here is the code:
              "http://gitorious.org/webkit/qtwebkit/blobs/qtwebkit-2.1/WebCore/bridge/qt/qt_runtime.cpp":http://gitorious.org/webkit/qtwebkit/blobs/qtwebkit-2.1/WebCore/bridge/qt/qt_runtime.cpp

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