Results 1 to 8 of 8

Thread: HTML and JS inside QML WebView pass variables

  1. #1
    Join Date
    Dec 2014
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default HTML and JS inside QML WebView pass variables

    Like the title says I have a QML WebView and I set it's url to a local HTML file from a folder outside the qrc:/ (resources). HTML file is calling a JS and a CSS file from the same folder. All work very well.
    The problem is that I want to pass JS variables to QML or to QT main (C++). I know it is possible to exchange messages from QML to C++ and vice verse but I want to pass JS variables from QML WebView to QML or to QT.
    Also I know that you can pass variables from JS to QML if you import in QML the JS file (import "myjs.JS" as D) and then you can access the variables. But the thing is that I copied the HTML, CSS, JS file from qrc:/ to another folder then I set the WebView url to that HTML file from folder. If I am to access with import I will get the default variables not the current ones. The reason why I copied the files to another folder is that I work on phone and I can't access or set the url to HTML file inside qrc:/ or asset:/

    The structure of my files is:
    1 QT main.cpp
    1 QML file
    1 HTML file
    1 CSS file
    1 JS file

  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: HTML and JS inside QML WebView pass variables

    Have a look at QWebChannel (avaialble since Qt 5.4). You can expose a number of objects to your webpage and expose the same objects to your QML environment.
    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. The following user says thank you to wysota for this useful post:

    alsi1400 (25th January 2015)

  4. #3
    Join Date
    Dec 2014
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: HTML and JS inside QML WebView pass variables

    Your page seems interesting. I didn't studied all.

    What I did, it was to follow the steps from: http://www.kdab.com/qt-webchannel-br...-gap-cqml-web/
    Qt Code:
    1. import QtWebChannel 1.0
    2.  
    3. import QtWebKit 3.0
    4. import QtWebKit.experimental 1.0
    5.  
    6. //Now, let’s create an object that we want to publish to the HTML/JavaScript clients:
    7.  
    8. QtObject {
    9. id: myObject
    10.  
    11. // the identifier under which this object
    12. // will be known on the JavaScript side
    13. WebChannel.id: "foo"
    14.  
    15. // signals, methods and properties are
    16. // accessible to JavaScript code
    17. signal someSignal(string message);
    18.  
    19. function someMethod(message) {
    20. console.log(message);
    21. someSignal(message);
    22. return "foobar";
    23. }
    24.  
    25. property string hello: "world"
    26. }
    27.  
    28. //Publishing the object to the HTML clients in your WebView is as simple as
    29.  
    30. WebView {
    31. experimental.webChannel.registeredObjects: [myObject]
    32. }
    To copy to clipboard, switch view to plain text mode 

    But I don't have the module "WebKit" build for my phone version so the example above doesn't work. I do have the "WebChannel" module though, and the "WebView" module has fewer features than the original: for example I can't set a "html: " attribute. I wanted to set the "html: " from QML to the HTML file contains, then put the content from my JS file inside QML and then I could access the variables.

    I will study your page and hope I get things done. Thank you again.
    Last edited by anda_skoa; 25th January 2015 at 11:13. Reason: missing [code] tags

  5. #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: HTML and JS inside QML WebView pass variables

    If you don't have a WebKit module then how are you instantiating WebView?
    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. #5
    Join Date
    Dec 2014
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: HTML and JS inside QML WebView pass variables

    I can't instantiate WebView in Main.cpp or elsewhere in C++, only in QML with "import QtWebView 1.0" and before that in .pro file: "QT += webview".
    Qt Code:
    1. WebView {
    2. id: webView
    3. objectName: "webView"
    4. anchors.fill: parent
    5. url: "http://www.yahoo.com"
    6. //html: doesn't work
    7. }
    To copy to clipboard, switch view to plain text mode 

    On \Qt5.4.0\5.4\android_armv7\include I have these "Web" folders: QtWebChannel, QtWebSockets, QtWebView. I mentioned only the "Web" folders, because I have more than that.

    Folders like QtWebKit or QtWebKitWidgets I have on \Qt5.4.0\5.4\mingw491_32 or \Qt5.4.0\5.4\Src, so I can use them only in Desktop version.

    EDIT1: I don't know yet how to compile QtWebKit and QtWebKitWidgets from \Qt5.4.0\5.4\Src to \Qt5.4.0\5.4\android_armv7 with android compiler.

  7. #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: HTML and JS inside QML WebView pass variables

    Where did you get that "webview" module from? It's not even listed on the official list of modules.
    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. #7
    Join Date
    Dec 2014
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: HTML and JS inside QML WebView pass variables

    I downloaded the latest Qt for Android offline installer on Windows 32 bit, and then I checked "build examples". The project is in "Qt5.4.0\Examples\QtWebView\1.0\webview\webvie w" last .pro file from hierarchy.
    If WebView have been build then there can be a possibility to build the WebKit and WebKitWidgets too for Android.

  9. #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: HTML and JS inside QML WebView pass variables

    I think what you downloaded makes use of Android's native web view component where Qt only acts as a proxy so it is likely you won't be able to use the same features as ones offered by QtWebKit. However there is a pure javascript implementation of QWebChannel which you can easily use with Android's version of WebView.
    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. Qt5 - print webview html doesn't fit the paper
    By patison in forum Qt Programming
    Replies: 0
    Last Post: 29th July 2013, 22:49
  2. How to pass variables as reference from C++ to QML
    By dmendizabal in forum Qt Quick
    Replies: 1
    Last Post: 17th February 2012, 14:39
  3. Replies: 1
    Last Post: 18th October 2011, 07:32
  4. Qml plugin in html inside WebView problem
    By xeento in forum Qt Quick
    Replies: 1
    Last Post: 29th January 2011, 13:27
  5. pass variables between 2 forms
    By restiz in forum Qt Programming
    Replies: 7
    Last Post: 17th June 2006, 16:13

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.