Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. call c++ class (qt) from javascript
Forum Updated to NodeBB v4.3 + New Features

call c++ class (qt) from javascript

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 1 Posters 437 Views
  • 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.
  • C Offline
    C Offline
    chiffaCff
    wrote on last edited by chiffaCff
    #1

    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!

    1 Reply Last reply
    0
    • C Offline
      C Offline
      chiffaCff
      wrote on last edited by
      #2

      no any idea?...

      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 Extensions
      • Unsolved