Design of the Interaction framework between QT and JavaScript.

Source: Internet
Author: User

[-]

  1. Two Methods of interaction between QT and JavaScript
  2. Instance analysis
    1. Front-end -- HTML and JavaScript
    2. Background -- QT
  3. Summary
  4. Appendix

Reprinted please indicate the source: http://blog.csdn.net/footman_/article/details/6694161

Directory

1. Two Methods of interaction between QT and JavaScript

2. instance analysis

2.1 front-end -- HTML and JavaScript

2.2 background -- QT

3 Summary

4 Appendix

1. Two Methods of interaction between QT and JavaScript

In order to make full use of the cross-platform features of JavaScript, QT and JavaScript interaction will not use the "QT public object reference to JavaScript" method (that is, to provide object reference to JavaScript through the addtojavascriptwindowobject method of qwebframe ). Although this method enables direct interaction between QT and JavaScript, the Javascript dependency on QT will affect JavaScript's cross-platform performance. The call principle is shown in the left figure in 1-1:

Figure 1-1

It is not hard to see that JavaScript's dependency on QT objects on the left is not conducive to JavaScript porting to other platforms.

The "custom URL" method on the right is different. JavaScript does not choose to use the QT object to call the QT method, but directly sends the custom URL to the WebKit browser, after WebKit captures the URL, it uses the signal provided by qwebkit to associate the slots function in QT and transmits the URL to the slots function. The slots function can process the information in the URL accordingly. In the "custom URL" mode, for the underlying platform, whether QT or Android, the upper-layer JavaScript only needs to send "custom URL. In this way, JavaScript platform independence can be fully realized. 1-2:

Figure 1-2

2. instance analysis

After a preliminary understanding of the "custom URL" method, we will use an example to describe the technical points that may be involved in this "custom URL" method.

2.1 front-end -- HTML and JavaScript

The example involves two HTML files, a.html# B .html, and one Javascript file, JS. js. The B .html file is very simple. The a.html file runs for 2-1:

Figure 2-1

The corresponding code is as follows:

<HTML>

<Script language = "JavaScript" type = "text/JavaScript" src = "Js. js"> </SCRIPT>

<Body>

<Center>

<Span> text A: </span>

<Input type = "text" name = "atext" id = "atext"/>

<Input type = "Submit" value = "go" onclick = "javascript: sendurl ()"/>

<A href = "B .html"> B .html </a>

<Font color = Red> <Div id = warning> </div> </font>

</Center>

</Body>

</Html>

The JS. JS Code referenced in the HTML file is as follows:

Function sendurl ()

{

VaR value = Document. getelementbyid ("atext"). value. Replace (/\ s/g ,"");

If (value! = "")

{

If (confirm ("go to" + value + "? "))

{

Location. href = value;

}

}

Else

{

Document. getelementbyid ("warning"). innerhtml = "Please input target ";

}

}

2.2 background -- QT

From the.html and js.js codes, when users enter content in the text box on the.html page, clicking the "go" button will trigger Js. in JS, The sendurl method sends the content entered by the user in the text box as a URL. How does the background QT end get this URL?

2.2.1 loadstarted Signal

Check the QT help document to find such a signal: loadstarted (), which exists in qwebview, qwebpage, and qwebframe. This signal is generated when a new webpage is loaded, so you can start by receiving the URL generated by JavaScript.

2.2.2 requestedurl Function

Qwebframe has a method called requestedurl (). When a URL request is generated and a web page is loaded, you can call this method to obtain the requested URL. It seems that this method is indispensable to receive URLs generated by JavaScript.

2.2.3 obtain URL

The following uses the signal and method selected earlier to obtain the URL generated by JavaScript.

1. Set the slots function for the loadstarted Signal

Qwebpage * page = view-> page ();

Connect (page, signal (loadstarted (), this, slot (doload ()));

2. Receive URL

Void mymainwindows: doload ()

{

Qurl url = view-> page ()-> mainframe ()-> requestedurl ();

Qdebug () <"doload..." <URL;

/* Parse URL */

Parseurl (URL );

}

For more information about how to implement parseurl, see the appendix.

2.2.4 demo instance

After receiving the message, you can use QT to parse the URL and select what operations to perform Based on the URL Information.

In this example, if urlis a serial number of bytes of printframes, you can use the original printframes function (simple output information is then sent to the B .html page ). Figure 2-2 shows the main interface after running the instance:

Figure 2-2

Enter print and click "go" to switch the page, as shown in Figure 2-3:

Figure 2-3

Observe the console output at the same time, as shown in Figure 2-4:

Figure 2-4

From the output information, we can see that the received URL is the absolute path of the file. After parsing, compare the URL. If it is equal to "print", run "Call Moudle of printprinting" and merge B .html.

When generating the loadstarted signal, the pre-configured slots function is also executed on the B .html page. This demonstration is complete!

3 Summary

First, let's look at a small foreshadowing in a.html.

Isn't there any jump in the pipeline?

The reason is that the linkclicked signal in qwebkit generates this signal when you click the hyperlink in the web page. In this example, the slots function is set for this signal. The function does not load the URL of the hyperlink, so no jump is generated after you click the hyperlink. There are many other signals like linkclicked, which can be used according to different requirements.

Comparing the two interaction modes, the "QT exposes object reference to JavaScript" method is more direct in the Process of JavaScript calling QT, the interaction mode of "custom URL" solves the cross-platform problem well. In short, there are no good or bad solutions, each of which has its own strengths. You can select one based on your actual needs.

4 Appendix

1. the specific implementation of the parseurl method is as follows:

Void mymainwindows: parseurl (qurl URL)

{

Qstring urlstr = URL. tostring ();

Qdebug () <"parseurl..." <urlstr;

 

/*

* This method is executed because every call to load triggers the loadstarted signal,

* Make sure that the URL will not be parsed when you load the webpage; otherwise, the webpage will not be loaded.

*/

If (urlstr. Right (5). Operator! = (Qstring (". html ")))

{

Int Len = urlstr. Length ();

Qdebug () <"urlstr. Length ():" <Len;

 

Qstring ret = urlstr. Right (len-24 );

Qdebug () <"Start parse URL:" <ret;

 

If (Ret. Operator = (qstring ("print ")));

{

Qdebug () <"Call Moudle of print ";

 

/* If the URL cannot be found, app exited with code-1073741819 */

View-> load (qurl ("../B .html "));

}

}

}

 

2. Before using the linkclicked signal, you must call setlinkdelegationpolicy:

Page-> setlinkdelegationpolicy (qwebpage: delegatealllinks );

Connect (page, signal (linkclicked (qurl), this, slot (parseurl (qurl )));

Reprinted please indicate the source: http://blog.csdn.net/footman_/article/details/6694161

Directory

1. Two Methods of interaction between QT and JavaScript

2. instance analysis

2.1 front-end -- HTML and JavaScript

2.2 background -- QT

3 Summary

4 Appendix

1. Two Methods of interaction between QT and JavaScript

In order to make full use of the cross-platform features of JavaScript, QT and JavaScript interaction will not use the "QT public object reference to JavaScript" method (that is, to provide object reference to JavaScript through the addtojavascriptwindowobject method of qwebframe ). Although this method enables direct interaction between QT and JavaScript, the Javascript dependency on QT will affect JavaScript's cross-platform performance. The call principle is shown in the left figure in 1-1:

Figure 1-1

It is not hard to see that JavaScript's dependency on QT objects on the left is not conducive to JavaScript porting to other platforms.

The "custom URL" method on the right is different. JavaScript does not choose to use the QT object to call the QT method, but directly sends the custom URL to the WebKit browser, after WebKit captures the URL, it uses the signal provided by qwebkit to associate the slots function in QT and transmits the URL to the slots function. The slots function can process the information in the URL accordingly. In the "custom URL" mode, for the underlying platform, whether QT or Android, the upper-layer JavaScript only needs to send "custom URL. In this way, JavaScript platform independence can be fully realized. 1-2:

Figure 1-2

2. instance analysis

After a preliminary understanding of the "custom URL" method, we will use an example to describe the technical points that may be involved in this "custom URL" method.

2.1 front-end -- HTML and JavaScript

The example involves two HTML files, a.html# B .html, and one Javascript file, JS. js. The B .html file is very simple. The a.html file runs for 2-1:

Figure 2-1

The corresponding code is as follows:

<HTML>

<Script language = "JavaScript" type = "text/JavaScript" src = "Js. js"> </SCRIPT>

<Body>

<Center>

<Span> text A: </span>

<Input type = "text" name = "atext" id = "atext"/>

<Input type = "Submit" value = "go" onclick = "javascript: sendurl ()"/>

<A href = "B .html"> B .html </a>

<Font color = Red> <Div id = warning> </div> </font>

</Center>

</Body>

</Html>

The JS. JS Code referenced in the HTML file is as follows:

Function sendurl ()

{

VaR value = Document. getelementbyid ("atext"). value. Replace (/\ s/g ,"");

If (value! = "")

{

If (confirm ("go to" + value + "? "))

{

Location. href = value;

}

}

Else

{

Document. getelementbyid ("warning"). innerhtml = "Please input target ";

}

}

2.2 background -- QT

From the.html and js.js codes, when users enter content in the text box on the.html page, clicking the "go" button will trigger Js. in JS, The sendurl method sends the content entered by the user in the text box as a URL. How does the background QT end get this URL?

2.2.1 loadstarted Signal

Check the QT help document to find such a signal: loadstarted (), which exists in qwebview, qwebpage, and qwebframe. This signal is generated when a new webpage is loaded, so you can start by receiving the URL generated by JavaScript.

2.2.2 requestedurl Function

Qwebframe has a method called requestedurl (). When a URL request is generated and a web page is loaded, you can call this method to obtain the requested URL. It seems that this method is indispensable to receive URLs generated by JavaScript.

2.2.3 obtain URL

The following uses the signal and method selected earlier to obtain the URL generated by JavaScript.

1. Set the slots function for the loadstarted Signal

Qwebpage * page = view-> page ();

Connect (page, signal (loadstarted (), this, slot (doload ()));

2. Receive URL

Void mymainwindows: doload ()

{

Qurl url = view-> page ()-> mainframe ()-> requestedurl ();

Qdebug () <"doload..." <URL;

/* Parse URL */

Parseurl (URL );

}

For more information about how to implement parseurl, see the appendix.

2.2.4 demo instance

After receiving the message, you can use QT to parse the URL and select what operations to perform Based on the URL Information.

In this example, if urlis a serial number of bytes of printframes, you can use the original printframes function (simple output information is then sent to the B .html page ). Figure 2-2 shows the main interface after running the instance:

Figure 2-2

Enter print and click "go" to switch the page, as shown in Figure 2-3:

Figure 2-3

Observe the console output at the same time, as shown in Figure 2-4:

Figure 2-4

From the output information, we can see that the received URL is the absolute path of the file. After parsing, compare the URL. If it is equal to "print", run "Call Moudle of printprinting" and merge B .html.

When generating the loadstarted signal, the pre-configured slots function is also executed on the B .html page. This demonstration is complete!

3 Summary

First, let's look at a small foreshadowing in a.html.

Isn't there any jump in the pipeline?

The reason is that the linkclicked signal in qwebkit generates this signal when you click the hyperlink in the web page. In this example, the slots function is set for this signal. The function does not load the URL of the hyperlink, so no jump is generated after you click the hyperlink. There are many other signals like linkclicked, which can be used according to different requirements.

Comparing the two interaction modes, the "QT exposes object reference to JavaScript" method is more direct in the Process of JavaScript calling QT, the interaction mode of "custom URL" solves the cross-platform problem well. In short, there are no good or bad solutions, each of which has its own strengths. You can select one based on your actual needs.

4 Appendix

1. the specific implementation of the parseurl method is as follows:

Void mymainwindows: parseurl (qurl URL)

{

Qstring urlstr = URL. tostring ();

Qdebug () <"parseurl..." <urlstr;

 

/*

* This method is executed because every call to load triggers the loadstarted signal,

* Make sure that the URL will not be parsed when you load the webpage; otherwise, the webpage will not be loaded.

*/

If (urlstr. Right (5). Operator! = (Qstring (". html ")))

{

Int Len = urlstr. Length ();

Qdebug () <"urlstr. Length ():" <Len;

 

Qstring ret = urlstr. Right (len-24 );

Qdebug () <"Start parse URL:" <ret;

 

If (Ret. Operator = (qstring ("print ")));

{

Qdebug () <"Call Moudle of print ";

 

/* If the URL cannot be found, app exited with code-1073741819 */

View-> load (qurl ("../B .html "));

}

}

}

 

2. Before using the linkclicked signal, you must call setlinkdelegationpolicy:

Page-> setlinkdelegationpolicy (qwebpage: delegatealllinks );

Connect (page, signal (linkclicked (qurl), this, slot (parseurl (qurl )));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.