How to let QWebengineView call React function

QWebengineView of Qt(golang) use RunJavaScript4 can not call React function successfully !

I solved this problem flow steps below.

  1. in js file, we should export React Class instance to window.
  import xxx;
  //...
  window.updateValue = function(url) {
  	alert("call ok !");
    // if(window.callback != undefined) {
    //    window.callback.updateValue(url);
    // }
  };
  window.setCallback = function(callback) {
    window.callback = callback;
  }
  //...
  export default class MyFeedback extends React.Component {
   //...
   // key step
   //  in react life cycle `componentWillMount` set `this` :
   componentWillMount() {
         window.setCallback(this);
   }
  //...
  }
  1. in go file, we call function like this :
	ap.WebView.Page().RunJavaScript4("window.updateValue('hello')")

after run project . i see alert call ok!

Refer to the link : https://blog.csdn.net/ZHOUYONGXYZ/article/details/82760929


Related