Results 1 to 11 of 11

Thread: Accessing array in both C++ and QtScript

  1. #1
    Join Date
    Feb 2009
    Posts
    24
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Accessing array in both C++ and QtScript

    I have an array of floats that I'd like to be able to access from QtScript. My initial thought was to use QVector, as I can get a pointer to the data to treat it as an array (which is necessary for some 3rd party software I'm interfacing with), but the script bridge only seems to support QLists, not QVectors. Any suggestions how to make this scenario possible? I'd hate to have to draw a penalty for constantly recreating the array in C++ every time a script needs to change its contents.

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Accessing array in both C++ and QtScript

    Hi!

    There are several possibilities.

    1. Simply make QVector a known sequence type:
      Qt Code:
      1. qScriptRegisterSequenceMetaType<QVector<double> >(engine);
      To copy to clipboard, switch view to plain text mode 
      Then you can call a function from scripting that returns a QVector.
      -- Before you can call ScriptRegister, you need to declare the QVector<double> metatype, first: Q_DECLARE_METATYPE(QVector<double>)
    2. The most elaborate possibility would be to subclass a QScriptClass as is shown for a ByteArray in the "Custom Script Class Example" in the documentation. This will also give you full Integration - use of [], Length ..
    3. Just use access functions get(i) and set(i)..


    Cheers

    Joh
    Last edited by JohannesMunk; 2nd September 2009 at 18:21. Reason: Forgot Q_DECLARE_METATYPE

  3. #3
    Join Date
    Feb 2009
    Posts
    24
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Accessing array in both C++ and QtScript

    That's great, thanks. Would this also make it available to Javascript in WebKit? There's a possibility I'd like to do the same thing there.

  4. #4
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Accessing array in both C++ and QtScript

    I have no idea. Anybody else?

    Johannes

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Accessing array in both C++ and QtScript

    Quote Originally Posted by abernat View Post
    That's great, thanks. Would this also make it available to Javascript in WebKit?
    No, they are two completely distinct engines.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Feb 2009
    Posts
    24
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Accessing array in both C++ and QtScript

    Quote Originally Posted by wysota View Post
    No, they are two completely distinct engines.
    Any ideas, wysota, on how to accomplish something similar for WebKit? Thanks.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Accessing array in both C++ and QtScript

    I don't think you can. At least not currently. What do you need it for? Can't you use regular javascript arrays?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Feb 2009
    Posts
    24
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Accessing array in both C++ and QtScript

    I'd use a regular Javascript array, but I'm not sure what the best method would be to get it to the C++ side. The application is using sampled data, so an array of floats makes the most sense. I'd like to build a tutorial module for the application, but I don't have a whole lot of time to spend on it, so I'd really like to use WebKit and just develop some HTML as the interface for the tutorial. That means either have the sample data created on the C++ side and exposed to Javascript or creating it in Javascript and exposing it to C++. I don't really have a preference either way. The problem is figuring out the correct (best?) way to share data between the two different sides of the application. If I can just share the Javascript array, that's great, but I've been unable to figure out how to do it thus far.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Accessing array in both C++ and QtScript

    Quote Originally Posted by abernat View Post
    I'd use a regular Javascript array, but I'm not sure what the best method would be to get it to the C++ side.
    Well... you can't access WebKit's "memory space" from C++ (yet!) so this is really meaningless.

    That means either have the sample data created on the C++ side and exposed to Javascript or creating it in Javascript and exposing it to C++. I don't really have a preference either way. The problem is figuring out the correct (best?) way to share data between the two different sides of the application. If I can just share the Javascript array, that's great, but I've been unable to figure out how to do it thus far.
    What do you need javascript for? Maybe importing the web technology is more of a burden than real help?

    You can use QtScript in C++ code and Qt is quite good in providing visual interface to the logic behind the application.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Feb 2009
    Posts
    24
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Accessing array in both C++ and QtScript

    No doubt that Qt's great for building interfaces (I do it all the time), but for this particular usage of WebKit could have been a great fit. By having the tutorials themselves be on an intranet as web pages, I'd be free to update them as time permits without having to worry about a new release of the software -- just update the HTML and Javascript on the servers. At the same time, I'd be able to allow the users to provide their own sample data for use in the tutorial. There are still ways to do it, but it would have been nice for the Javascript to have direct access to the float arrays I'm using in the application for display in HTML.

    It's not a showstopper or anything, it would have just been a nice solution for this particular component.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Accessing array in both C++ and QtScript

    Quote Originally Posted by abernat View Post
    By having the tutorials themselves be on an intranet as web pages, I'd be free to update them as time permits without having to worry about a new release of the software -- just update the HTML and Javascript on the servers.
    That's really not an argument as you can download files from a webserver without WebKit as well. And users can still manipulate the environment using QtScript.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QTScript - checking variable names?
    By android_ in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2009, 18:40
  2. QtScript Binding problem with QWidget
    By zack in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2009, 09:38
  3. QtScript : passing array as argument into function
    By derek_r in forum Qt Programming
    Replies: 4
    Last Post: 27th October 2007, 10:46
  4. QtScript Q_ENUM problem
    By oc2k1 in forum Qt Programming
    Replies: 0
    Last Post: 14th May 2007, 16:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.