Implementation of interactive data between QT and javascript

Implementation of interactive data between QT and javascript

1. Data flows from QT to JS
1. QT calls the JS function, and JS obtains the value of QT through the parameter
2. JS calls QT's function, and the return value of the QT function enters JS
2. Data flows from JS to QT
1. JS calls QT's function, and QT obtains the value of JS through the formal parameter
2. QT calls the JS function, and the return value of the JS function enters QT

1. QT passes array to JS

Basic types can be passed directly, such as int, bool, string, double, etc.

When qt passes an array to js, ​​it is necessary to convert the array into QJsonArray, and then convert the QJsonArray into QString. In this way, js will receive a basic type string, and this string is directly a standard js array in js.

QT code example: Call a js function and pass an array as a parameter to the js function

    //Method 1: Construct QJsonArray and then convert it to QString
    QJsonArray ja;
    ja << 3 << 4 << 5;
    QString jpar = QString(QJsonDocument(ja).toJson());
    QString cmd = QString("qtPara(%0)").arg(QString(QJsonDocument(ja).toJson()));
    //Method 2: Write the array directly as a string
// QString cmd = QString("qtPara([13,14,15])");
 
    //Run js function webView->page()->runJavaScript(cmd);

The javascript function called by the above code is:

 function qtPara(numList)
 { 
  alert("js alert: " + numList); //Display the entire array sent by qt alert("js alert[0]: " + numList[0]); //Display the 0th element of the array sent by qt }

2. JS passes array to QT

If JS passes an array to QT, QT will convert this value into QJsonArray

JS can also pass any JS object to QT, so QT needs to convert it into QJsonObject

QT end sample code:

    QString cmd = QString("jsString()");
 
    webView->page()->runJavaScript(cmd, [](const QVariant &v)
    {
        //Case 1: When js returns a number qDebug() << "qt call js = " << v.toDouble();
        //Case 2: When js returns a string value qDebug() << "qt call js = " << v.toString();
        //Case 3: When js returns a js array QJsonArray ja = v.toJsonArray();
        qDebug() << "j[0] = " << ja.at(0).toDouble();
        //Case 4: When js returns a js object QJsonObject jo = v.toJsonObject();
        qDebug() << jo;
    });

The JS function called by the above code:

// var jArr = [120.123456789, 22, 33, 44]; //js array
 // var jObj = {"num":[120.123456789, 22, 33, 44], "name":"Tom"}; //json
var jNum = 120.1234567;
 function jsString()
 {
   alert("jsString");
   //return jNum;
   //return jArr;
   //return jObj;
 }

3. JS passes any type of data to QT

The QT side receives it with the QVariant type, and then qDebug this value, you can see how this JS value is encapsulated as a QVariant, and then we can

For example, JS returns a value like this to QT, which is an array of JS objects, each element of which is a Point object, and this Point object has lng and lat attribute values.

path = [new Point(116.387112,39.920977), new Point(116.387112,39.920977)];

After QT receives it, it qDebugs it as follows:

QVariant(QVariantList,
(QVariant(QVariantMap, QMap(("lat", QVariant(double, 39.921))("lng", QVariant(double, 116.387)))),
QVariant(QVariantMap, QMap(("lat", QVariant(double, 39.921))("lng", QVariant(double, 116.387))))))

We found that

① QT encapsulates the JS object array into QVariantList, that is, QList<QVariant>,

② Each member of this list is encapsulated by QT into a QVariantMap, that is, QMap<QString, QVariant>

③ The key in each map is QString, the value is QVariant, and this QVariant is double.

After the above analysis steps, we can easily parse out any data sent from JS to QT.

This is the end of this article about the implementation of interactive data between QT and javascript. For more relevant content about QT and javascript interaction, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to make QWebEngineView in PyQt5 interact with JavaScript

<<:  Detailed explanation of the difference between "/" and "~" in Linux

>>:  The pitfalls and solutions caused by the default value of sql_mode in MySQL 5.7

Recommend

Summary of Mysql-connector-java driver version issues

Mysql-connector-java driver version problem Since...

Detailed analysis of the usage and application scenarios of slots in Vue

What are slots? We know that in Vue, nothing can ...

How to modify the default storage location of Docker images (solution)

Due to the initial partitioning of the system, th...

Docker builds CMS on-demand system with player function

Table of contents text 1. Prepare the machine 2. ...

Design and implementation of Vue cascading drop-down box

Table of contents 1. Database design 2. Front-end...

VMware installation of Centos8 system tutorial diagram (Chinese graphical mode)

Table of contents 1. Software and system image 2....

Complete example of Vue encapsulating the global toast component

Table of contents Preface 1. With vue-cli 1. Defi...

About the "occupational disease" of designers

I always feel that designers are the most sensiti...

A brief discussion on the principle of Vue's two-way event binding v-model

Table of contents explain: Summarize Replenish Un...

Common causes and solutions for slow MySQL SQL statements

1. Slow query due to lack of index or invalid ind...

How to insert batch data into MySQL database under Node.js

In the project (nodejs), multiple data need to be...

Detailed explanation of how to find the location of the nginx configuration file

How can you find the location of the configuratio...

Detailed explanation of how to use zabbix to monitor oracle database

1. Overview Zabbix is ​​a very powerful and most ...

Example of using docker compose to build a consul cluster environment

Basic concepts of consul Server mode and client m...