My friend and I are trying to establish a connection between C ++ and Javascript. We want to display some spheres at a given position using WebGL built into the C ++ / Qt application (version 5.6).
To do this, we use QWebView , which reads an HTML file with all the JS code for WebGL. This works well, we have great high-level 3D rendering inside our application. The problem is that the position is known in C ++ and should be split into JavaScript .
More precisely, C ++ knows the number of areas we need to display, and their position, and I have to tell this to the side of Javascript. What is the best way to do this?
Note. We tried to use Qt Canvas3D, but it was too slow compared to classic WebGL.
EDIT 1 - after the first attempt
I tried to do what you wrote and also read some examples (like one ), but I can't get it to work.
I created a subclass of QWebEngineView, where I perform all my initializations and set my data.
.h:
#ifndef WEBGLVIEW_H #define WEBGLVIEW_H #include <QObject> #include <QtWebEngineWidgets> #include <QtWebChannel/QtWebChannel> class WebGLView : public QWebEngineView { Q_OBJECT Q_INVOKABLE int getNumber(); Q_PROPERTY(int num READ getNumber) public: WebGLView(QWidget *parent = 0); private : QWebChannel* m_channel; }; #endif // WEBGLVIEW_H
.cpp:
#include "WebGLView.h" #include <QCoreApplication> #include <QUrl> #include <QDebug> WebGLView::WebGLView(QWidget *parent) : QWebEngineView(parent) { // Set up the communications channel //C++ to Java test m_channel = new QWebChannel(this); this->page()->setWebChannel(m_channel); m_channel->registerObject(QString("test"),this); QString path = QCoreApplication::applicationDirPath() + "/test6.html"; this->setUrl(QUrl::fromLocalFile(path)); } int WebGLView::getNumber() { qDebug() << "getNumber"; return 10; }
At the beginning of JS:
<script type="text/javascript" src="qrc:///qtwebchannel/qwebchannel.js"></script> ... var JSobject; if (typeof qt != 'undefined') {new QWebChannel(qt.webChannelTransport, function(channel) { JSobject = channel.objects.test; } );
When I want to access the value:
if (typeof widget !== 'undefined') { var nspherefromc = JSobject.getNumber; } else {var nspherefromc = 50;}
When I ran it:
- getNumber is called once and it seems to come after a call to QWebChannel
- I have a warning: the 'num' property of the 'WebGLView' object has no notification signal and is not constant, updating the values ββin HTML will be broken!
- nspherefromc - the number of spheres to be displayed. 50, not 10.
Any ideas?
EDIT 2 - second attempt
I still can't get it to work. I tried adding a new variable called more to see if the function was called or the if statement was entered:
var more = 0; if (typeof qt != 'undefined') { new QWebChannel(qt.webChannelTransport, function(channel) { JSobject = channel.objects.test; more = 1000; } ); }
I still have:
if (typeof JSobject !== 'undefined') { nspherefromc = JSobject.num; } else {nspherefromc = 50;} ... var spheresNum = nspherefromc + more;
When working with him, I have only 50 areas. Function in QWebChannel is not called. Perhaps I should call it myself? And when? I also tried more debugging in the if line (typeof qt! = 'Undefined'), and I got 1050 spheres.
EDIT 3 - With HTML Debugger
function init() { var JSobject; var nspherefromc = 0; if (typeof qt !== 'undefined') { new QWebChannel(qt.webChannelTransport, function(channel) { JSobject = channel.objects.test; console.log(JSobject);
This means that outside the function I can no longer get to the JSobject. Do you know, why?
EDIT - at the end of OpenGL is already initialized when receiving data from C ++. Therefore, the number of displayed spheres does not match C ++ requests.