Results 1 to 8 of 8

Thread: Accessing Javascript function from QT/C++

  1. #1
    Join Date
    May 2011
    Posts
    77
    Thanks
    6
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Accessing Javascript function from QT/C++

    I am re-writing my Qt/QML mobile app to enable more control and easier workflow.

    I have a main.cpp file and two supporting CPP files. I also have a main.qml and 'many' supporting QML files which are my 'pages' that get loaded into the main.qml window.

    Originally, I had all my javascript functions and QML pages defined in main.qml. Now I am separating it all out into individual files. Each QML page uses common javascript functions, so I moved all my javascript to a functions.js file. Now each QML imports the functions.js file like this: 'import "functions.js" as Funcs'...no problem, that all works OK.

    My issue is that I need to call a couple of javascript functions from my CPP files.

    Originally, I could access those via the QQmlApplicationEngine object, which I instantiated with the main.qml file.

    But with all the functions moved to a separate JS file...how can I go about calling them from my CPP files?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Accessing Javascript function from QT/C++

    Can you give an example?

    While possible, it is very uncommon to call from C++ into QML as this creates a dependency in the "wrong" direction

    Cheers,
    _

  3. #3
    Join Date
    May 2011
    Posts
    77
    Thanks
    6
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Accessing Javascript function from QT/C++

    This app utilizes QtPositioning.

    In main.cpp, I do an initial call to get the users location:

    Qt Code:
    1. FileIOqt myFileIO;
    2. gpsio myGpsIO(&myFileIO);
    3. myGpsIO.gpsOneShot();
    To copy to clipboard, switch view to plain text mode 

    FileIOqt and gpsio are defined in their respective fileio.cpp/.h and gpsio.cpp/.h...

    In gpsOneShot(), I do:

    Qt Code:
    1. if (sourceOneShot) {
    2. connect(sourceOneShot, SIGNAL(positionUpdated(QGeoPositionInfo)),
    3. this, SLOT(oneShotPosition(QGeoPositionInfo)));
    4. sourceOneShot->startUpdates();
    5. }
    To copy to clipboard, switch view to plain text mode 

    In oneShotPosition, I get the relevant data and update my QML text fields where necessary. Then, I call stopOneShotGPS() which in turn calls a javascript function:

    Qt Code:
    1. QObject *root = myFileIO->engine.rootObjects().first();
    2. QVariant returnedVal;
    3. QMetaObject::invokeMethod(root,"gpsOneShotUpdate",Q_RETURN_ARG(QVariant, returnedVal));
    To copy to clipboard, switch view to plain text mode 

    The gpsOneShotUpdate javascript function basically sets a few variables, and shows a new QML page.

    ALL THIS WORKS fine...when it's all in one QML file.

    What I need now..is to be able to still call the JavaScript functions from CPP, even when they are moved outside (but imported into) the QML.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Accessing Javascript function from QT/C++

    This really looks like something were a way better solution exists, but if you really need to have this reverse dependency then just add a gpsOneShotUpdate function to your root object and make it call the function in the script file.

    What is the returned value? I.e. what does it mean?

    Cheers,
    _

  5. #5
    Join Date
    May 2011
    Posts
    77
    Thanks
    6
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Accessing Javascript function from QT/C++

    There is no returned value...it only sets some text on a couple of labels, then hides & shows 2 QML 'pages'.

    I am open to any "way better solution"....

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Accessing Javascript function from QT/C++

    You use Q_RETURN_ARG, so I assume the return value was important to you.

    If your value API an exported object instance, i.e. one exported using setContextProperty(), or are you instantiating it from QML?
    In either case signalling the end of the GPS update would be done with a signal of that class, but depending on how you create the instance you will handle the signal differently.

    Cheers,
    _

  7. #7
    Join Date
    May 2011
    Posts
    77
    Thanks
    6
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Accessing Javascript function from QT/C++

    If your value API an exported object instance, i.e. one exported using setContextProperty(), or are you instantiating it from QML?
    I don't understand what you are asking...

    ==========================

    For now, I am trying to add "interface" functions in my Main.QML that calls my javascript functions as you suggested earlier.
    Last edited by scgrant327; 8th February 2016 at 20:15.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Accessing Javascript function from QT/C++

    You wrote that you are getting some values in C++ and then putting these into UI elements.

    I assume you are doing this by having that data in properties of an object and using bindings to these properties in QML.

    So that object just needs another signal.

    Cheers,
    _

Similar Threads

  1. Call JavaScript function with parameters from Qt C++
    By qt_developer in forum Qt Programming
    Replies: 5
    Last Post: 13th January 2015, 01:31
  2. Call a JavaScript function from C++ using QtWebkit
    By Rastersoft in forum Qt Programming
    Replies: 9
    Last Post: 14th May 2014, 13:03
  3. Replies: 4
    Last Post: 2nd August 2012, 08:42
  4. Pass data from QT to javascript function
    By thanhluanbk88 in forum Qt Programming
    Replies: 3
    Last Post: 3rd March 2011, 11:40
  5. WebKit - accessing javascript results + googleMap
    By giusepped in forum Qt Programming
    Replies: 10
    Last Post: 30th May 2009, 01:01

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.