Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

call c++ class (qt) from javascript



  • Hello! I try to call qt c++ function from javascript (QWebEngineView)

    test.html:

    <script type="text/javascript" src="qrc:///qtwebchannel/qwebchannel.js"></script>
    
        <script>
    
        function test() {
    
    
        new QWebChannel(qt.webChannelTransport, function (channel) {
            var jshelper = channel.objects.FromJS;
            // do what you gotta do
        });
    }
    
        </script>
    
        <a href='#' onclick='test();'>TEST</a></div>
    

    When i click TEST in html form in qt-debug it show like:

    Property 'documentMode'' of object 'MyForm' has no notify signal and is not constant, value updates in HTML will be broken!
    Property 'tabShape'' of object 'MyForm' has no notify signal and is not constant, value updates in HTML will be broken!
    Property 'dockNestingEnabled'' of object 'MyForm' has no notify signal and is not constant, value updates in HTML will be broken!
    Property 'dockOptions'' of object 'MyForm' has no notify signal and is not constant, value updates in HTML will be broken!
    Property 'unifiedTitleAndToolBarOnMac'' of object 'MyForm' has no notify signal and is not constant, value updates in HTML will be broken!
    

    MyForm.h:

    #ifndef LOGINFORM_H
    #define LOGINFORM_H
    
    #include <QMainWindow>
    #include <QtWebChannel/QtWebChannel>
    #include <QtWebEngineWidgets>
    #include <QMessageBox>
    
    namespace Ui {
    class MyForm;
    }
    
    class MyForm : public QMainWindow
    {
        Q_OBJECT
    
    
    public:
        explicit MyForm(QWidget *parent = nullptr);
        ~MyForm();
    
    
    private:
        Ui::MyForm *ui;
            bool eventFilter(QObject *object, QEvent *event);
            void resizeEvent(QResizeEvent* event);
            QString m_oUserName;
            QString m_oPassword;
    
    public slots:
        void FromJS();
        void SetSetting();
        void connectToJs(bool result);
    
    };
    
    #endif // LOGINFORM_H
    

    MyForm.cpp:

    #include "myform.h"
    #include "function.h"
    #include "ui_myform.h"
    //825 ---- 690
    
    
    
    
    MyForm::MyForm(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MyForm)
    {
    
    
    
        ui->setupUi(this);
    
        ui->setupUi(this);
       // ui->preview->page()->  mainFrame()->addToJavaScriptWindowObject("myoperations", new MyJavaScriptOperations);
    
        QWebChannel *channel = new QWebChannel(ui->preview->page());
        channel->registerObject(QString("FromJS"), this);
    
        ui->preview->page()->setWebChannel(channel);
    
    
    
        ui->preview->load(QUrl("qrc:/html_doc/test.html"));
    
    
         ui->preview->show();
    ui->preview->installEventFilter(this);
        qDebug () << "Load";
        QWidget::resize(825, 690);
    
        QFile file("settings.cfg");
        if(!file.open(QIODevice::ReadOnly)) {
            QMessageBox::information(0, "error", file.errorString());
        }
    
        QTextStream in(&file);
    
        while(!in.atEnd()) {
            QString line = in.readLine();
            Settings.insert(line.split("#")[0].split("=")[0].trimmed(), line.split("#")[0].split("=")[1].trimmed());
        }
    
        qDebug() << Settings;
    
        file.close();
    
    
    
    
    }
    
    
    
    void MyForm::SetSetting()
    {
    
         qDebug() << "Try call JS";
    
    ui->preview->page()->runJavaScript(QStringLiteral("SetSetting();"));
    }
    
    MyForm::~MyForm()
    {
        delete ui;
    }
    bool MyForm::eventFilter(QObject *object, QEvent *event)
    {
    
       if (object == ui->preview && event->type() == QEvent::ContextMenu)
        {
        qDebug () << event->type();
        event->setAccepted(false);
        return false;
    }
    
    
    return false;
    }
    
    void MyForm::FromJS()
    {
        qDebug () << "call from JS";
        QMessageBox::information(0, "test1", "TEST");
    }
    
    void MyForm::resizeEvent(QResizeEvent* event)
    {
       QMainWindow::resizeEvent(event);
    
       ui->preview->resize(QWidget::width(), QWidget::height());
    
       qDebug () << QWidget::width() << "----" << QWidget::height();
       //qDebug () << ui->preview->width();
    
       // Your code here.
    }
    
    void MyForm::connectToJs(bool result) {
    
        qDebug() << "connectToJs!" << result;
        if (result) {
            QWebChannel *channel = new QWebChannel(ui->preview->page());
            ui->preview->page()->setWebChannel(channel);
            channel->registerObject(QString("FromJS"), this);
            qDebug() << "connect is ok!" << result;
        }
        else
        {
          qDebug() << "connect error!" << result;
        }
    }
    

    Anyone can help, how to call c++ from JS? Thanks!



  • no any idea?...


Log in to reply