Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QtWebEngine
  4. How to send string from html script to c++ in QT
Qt 6.12 LTS is out! See what's new in the release blog

How to send string from html script to c++ in QT

Scheduled Pinned Locked Moved Unsolved QtWebEngine
4 Posts 2 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    QtsCOde
    wrote on last edited by SGaist
    #1

    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?

    Q 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Q QtsCOde

        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?

        Q Offline
        Q Offline
        QtsCOde
        wrote on last edited by
        #3

        @QtsCOde Oh okay got it. And yes, sorry for coding tags, will keep in mind from next time. Thank you.

        SGaistS 1 Reply Last reply
        0
        • Q QtsCOde

          @QtsCOde Oh okay got it. And yes, sorry for coding tags, will keep in mind from next time. Thank you.

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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 :-)

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt
          • Unsolved