How to send string from html script to c++ in QT
-
This is the code in C++ to connect html and c++ ->
QDir dir(app_path+"/minified/web/"); setWindowTitle(pdf_path); pdf_path = dir.relativeFilePath(pdf_path); m_communicator = new Communicator(this); m_communicator->setUrl(pdf_path); m_webView = new QWebEngineView(this); QWebChannel * channel = new QWebChannel(this); channel->registerObject(QStringLiteral("communicator"), m_communicator); m_webView->page()->setWebChannel(channel); m_webView->load(url); setCentralWidget(m_webView);This is the code in html for calling the function in c++ ->
var webChannel = new QWebChannel(qt.webChannelTransport, function (channel) { var m_communicator = channel.objects.m_communicator; // Replace communicator with the object name used in C++ // Now you can call functions on the communicator window.foo = m_communicator; foo.someFunction(selectedText); });This is the function in cpp that I have trying to call ->
#include <QApplication> #include "communicator.h" #include <iostream> #include <QMessageBox> void Communicator::setUrl(const QString & data) { if (data == m_url) return; m_url = data; emit urlChanged(m_url); } void Communicator::someFunction(const QVariant& data) { QMessageBox::information(NULL, "my", "wcecw"); }But this is giving me an error
js: Uncaught TypeError: Cannot read properties of undefined (reading 'someFunction')can someone help me resolve this?
-
Hi and welcome to devnet,
ˋfoo` alone does not exist hence the message error.
Also, please use coding tags when providing code. That will make it readable. I have added them for you this time.
-
This is the code in C++ to connect html and c++ ->
QDir dir(app_path+"/minified/web/"); setWindowTitle(pdf_path); pdf_path = dir.relativeFilePath(pdf_path); m_communicator = new Communicator(this); m_communicator->setUrl(pdf_path); m_webView = new QWebEngineView(this); QWebChannel * channel = new QWebChannel(this); channel->registerObject(QStringLiteral("communicator"), m_communicator); m_webView->page()->setWebChannel(channel); m_webView->load(url); setCentralWidget(m_webView);This is the code in html for calling the function in c++ ->
var webChannel = new QWebChannel(qt.webChannelTransport, function (channel) { var m_communicator = channel.objects.m_communicator; // Replace communicator with the object name used in C++ // Now you can call functions on the communicator window.foo = m_communicator; foo.someFunction(selectedText); });This is the function in cpp that I have trying to call ->
#include <QApplication> #include "communicator.h" #include <iostream> #include <QMessageBox> void Communicator::setUrl(const QString & data) { if (data == m_url) return; m_url = data; emit urlChanged(m_url); } void Communicator::someFunction(const QVariant& data) { QMessageBox::information(NULL, "my", "wcecw"); }But this is giving me an error
js: Uncaught TypeError: Cannot read properties of undefined (reading 'someFunction')can someone help me resolve this?
-
@QtsCOde Oh okay got it. And yes, sorry for coding tags, will keep in mind from next time. Thank you.
Since you have it working now, please mark the thread as solved using the "Topic Tools" button or the three dotted menu beside the answer you deem correct so that other forum users may know a solution has been found :-)