Results 1 to 10 of 10

Thread: QT Integrating C++ and Javascript

  1. #1
    Join Date
    May 2007
    Posts
    131
    Thanks
    17
    Thanked 4 Times in 2 Posts

    Default QT Integrating C++ and Javascript

    I'm currently developing a game (class requirement) similar to Are you Smarter than a 5th grader.

    Seeing this, I should be able to retrieve the questions from a .txt file which I can do. The problem is that I want to send the values that I got from the .txt file to my javascript for processing. How do I do this?

    Currently, I can get and save the questions from the text file using a QList<QString>. Although I don't know how to send these to Javascript. Based from research, I found out that you can use the QMetaObject::invokeMethod (here's the link where I found it http://doc.qt.nokia.com/4.7-snapshot/qtbinding.html) although I'm not sure how to integrate it with my QList.

    I need help! Thanks!

  2. #2
    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: QT Integrating C++ and Javascript

    How are you calling the scripts? Usually you'd just call QScriptEngine::evaluate() to trigger a script and possibly earlier expose some data to the script by setting some properties of either the global object of the script engine or the activation object of the script context.
    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.


  3. #3
    Join Date
    May 2007
    Posts
    131
    Thanks
    17
    Thanked 4 Times in 2 Posts

    Default Re: QT Integrating C++ and Javascript

    I stumbled upon this while researching about QT: http://doc.qt.nokia.com/4.7-snapshot...ys-and-objects

    JavaScript arrays and objects
    There is built-in support for automatic type conversion between QVariantList and JavaScript arrays, and QVariantMap and JavaScript objects.
    For example, the function defined in QML below left expects two arguments, an array and an object, and prints their contents using the standard JavaScript syntax for array and object item access. The C++ code below right calls this function, passing a QVariantList and a QVariantMap, which are automatically converted to JavaScript array and object values, respectively:
    Qt Code:
    1. // MyItem.qml
    2. Item {
    3. function readValues(anArray, anObject) {
    4. for (var i=0; i<anArray.length; i++)
    5. console.log("Array item:", anArray[i])
    6.  
    7. for (var prop in anObject) {
    8. console.log("Object item:", prop, "=", anObject[prop])
    9. }
    10. }
    11. }
    12. // C++
    13. QDeclarativeView view(QUrl::fromLocalFile("MyItem.qml"));
    14.  
    15. QVariantList list;
    16. list << 10 << Qt::green << "bottles";
    17.  
    18. QVariantMap map;
    19. map.insert("language", "QML");
    20. map.insert("released", QDate(2010, 9, 21));
    21.  
    22. QMetaObject::invokeMethod(view.rootObject(), "readValues",
    23. Q_ARG(QVariant, QVariant::fromValue(list)),
    24. Q_ARG(QVariant, QVariant::fromValue(map)));
    To copy to clipboard, switch view to plain text mode 

    I tried to integrate this to my application. Although, instead of using QVariantList, I used a QStringList to retrieve values from my text file.

    Qt Code:
    1. ...
    2.  
    3. QTextStream in(&inputFile);
    4.  
    5. while ( !in.atEnd() )
    6. {
    7. QString str = in.readLine();
    8. //qDebug() << "message: " << str;
    9. line.append(str);
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

    Afterwards, I used the QMetaObject thing to pass the QStringList to my javascript function.

    Qt Code:
    1. QMetaObject::invokeMethod(view.rootObject(), "readValues",
    2. Q_ARG(QVariant, QVariant::fromValue(line)));
    3.  
    4. //WHERE readValues is my Javascript function
    To copy to clipboard, switch view to plain text mode 

    It works. And it reads and displays the retrieved values when I use console.log() in my .qml file. HOWEVER, whenever this it executed, I get a TypeError: file:///C:/Users/MYCOMP/Qt/Projectproj-build-desktop/qml/Projectproj/main.qml:65: TypeError: Result of expression 'anArray' [undefined] is not an object.

    How come?

  4. #4
    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: QT Integrating C++ and Javascript

    Are you using QML because you want to use QML or because the snippet you found uses QML? Because what you are trying to do right now yields little sense.
    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.


  5. #5
    Join Date
    May 2007
    Posts
    131
    Thanks
    17
    Thanked 4 Times in 2 Posts

    Default Re: QT Integrating C++ and Javascript

    I have little knowledge regarding C++ (or C itself) although I know how to program in Java. I'm required by my professor to come up with an application using the QT platform and that is why I'm using this kind of approach.

    I've read tutorials regarding QML and by seeing these tutorials, it is possible to create a simple game with the use of only QML and Javascript. The reason why I'm using a bit of C++ is because of the fact that I have to retrieve data from the text file (according to my professor ).

  6. #6
    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: QT Integrating C++ and Javascript

    But why are you calling javascript code from C++ code? Shouldn't it be the other way round?
    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.


  7. #7
    Join Date
    May 2007
    Posts
    131
    Thanks
    17
    Thanked 4 Times in 2 Posts

    Default Re: QT Integrating C++ and Javascript

    The only reason why I utilized C++ is just to retrieve values from the text file because based from research, Javascript isn't allowed to do that. After retrieving the lines from my text file, what I plan to do is let the Javascript handle the logic.

    The only way I saw how this would be possible is if I used the QMetaObject thing that I saw in the QT documentation.

    If what you're saying is that I should call a C++ function using Javascript, then my answer to that is I have no clue on how to do it.

    EDIT:

    If ever you can suggest anything easier to do besides what I've tried, I would really appreciate it.
    Last edited by bmn; 9th August 2011 at 14:23.

  8. #8
    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: QT Integrating C++ and Javascript

    You clearly have issues with designing the program. If your game is driven by javascript then the whole control is also in hands of javascript. The C++ code is only required to retrieve some data so when the right time comes you only need to call a C++ function and when it returns with the data you need, simply use the data. So the real task is to expose a C++ function/method to QML environment and the docs have examples of doing that.
    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.


  9. #9
    Join Date
    May 2007
    Posts
    131
    Thanks
    17
    Thanked 4 Times in 2 Posts

    Default Re: QT Integrating C++ and Javascript

    I will try that. Thank you.

    The reason why I structured mine in such a manner is that once the application is opened, I want all lines of the text file automatically retrieved by the C++ then processed by the Javascript.

    I will read about it on the QT site and post here again.

    EDIT:

    Seeing that my poorly constucted method works, I would, for now, just want to know why I'm receiving a TypeError such as this whenever I try to manipulate the array (formerly the QStringList from the .cpp file) which contains the data from the text file that was passed to the javascript function using the QMetaObject method.
    Qt Code:
    1. TypeError: file:///C:/Users/MYCOMP/Qt/Projectproj-build-desktop/qml/Projectproj/main.qml:65: TypeError: Result of expression 'anArray' [undefined] is not an object.
    To copy to clipboard, switch view to plain text mode 

  10. #10
    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: QT Integrating C++ and Javascript

    No idea, I don't really want to analyze code that's inherently broken. Apparently you're accessing a non-object as an object.
    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. Replies: 9
    Last Post: 29th November 2010, 13:12
  2. integrating QT to ARM board
    By piyushpandey in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 31st August 2010, 15:15
  3. Integrating OpenVG with QT 4.6
    By ksrini in forum Qt Programming
    Replies: 1
    Last Post: 11th February 2010, 06:52
  4. Integrating C with Qt
    By deepakn in forum Qt Programming
    Replies: 2
    Last Post: 4th December 2007, 10:12
  5. KCModule integrating
    By lum in forum KDE Forum
    Replies: 0
    Last Post: 17th May 2006, 10:32

Tags for this Thread

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.