1+ <!DOCTYPE html>
2+ < html >
3+
4+ < head >
5+ < meta http-equiv ="Content-Type " content ="text/html; charset=utf-8 " />
6+ < script type ="text/javascript " src ="./qwebchannel.js "> </ script >
7+
8+ < script type ="text/javascript ">
9+ function appendText ( message ) {
10+ // 添加输出到页面
11+ var textArea = document . getElementById ( "textArea" ) ;
12+ output . innerHTML = output . innerHTML + message + "\n" ;
13+ }
14+ window . onload = function ( ) {
15+ // 创建websocket
16+ var socket = new WebSocket ( "ws://localhost:12345" ) ;
17+ socket . onclose = function ( ) {
18+ appendText ( "WebSocket连接关闭" ) ;
19+ } ;
20+ socket . onerror = function ( ) {
21+ appendText ( "WebSocket连接发生错误" ) ;
22+ } ;
23+ socket . onopen = function ( ) {
24+ appendText ( "WebSocket连接成功" ) ;
25+ // 创建webchannel
26+ window . channel = new QWebChannel ( socket , function ( channel ) {
27+ // 重点:把注册的对象全部设为全局,注册了多少个就设置多少个
28+ window . WebChannelObject = channel . objects . WebChannelObject ;
29+ window . qtwindow = channel . objects . qtwindow ;
30+ // 绑定窗口标题变化信号
31+ window . qtwindow . windowTitleChanged . connect ( function ( title ) {
32+ appendText ( '标题变化:' + title ) ;
33+ } ) ;
34+ } ) ;
35+ } ;
36+
37+ }
38+ </ script >
39+ </ head >
40+
41+ < body >
42+ < span > 输出:</ span > < br /> < textarea id ="output " style ="width:400px;height:200px; "> </ textarea > < br /> < br />
43+
44+ < input type ="button " value ="设置属性(int)= 100 " onclick ="javascript: window.WebChannelObject.intValue = 100; " />
45+ < input type ="button " value ="获取属性(int) "
46+ onclick ="javascript: appendText('intValue:' + window.WebChannelObject.intValue); " /> < br /> < br />
47+
48+ < input type ="button " value ="设置属性(float)= 99.9 " onclick ="javascript: window.WebChannelObject.floatValue = 99.9; " />
49+ < input type ="button " value ="获取属性(float) "
50+ onclick ="javascript: appendText('floatValue:' + window.WebChannelObject.floatValue); " /> < br /> < br />
51+
52+ < input type ="button " value ="设置属性(str)= Irony " onclick ="javascript: window.WebChannelObject.strValue = 'Irony'; " />
53+ < input type ="button " value ="获取属性(str) "
54+ onclick ="javascript: appendText('strValue:' + window.WebChannelObject.strValue); " /> < br /> < br />
55+
56+ < input type ="button " value ="设置属性(bool)= true " onclick ="javascript: window.WebChannelObject.boolValue = true; " />
57+ < input type ="button " value ="获取属性(bool) "
58+ onclick ="javascript: appendText('boolValue:' + window.WebChannelObject.boolValue); " /> < br /> < br />
59+
60+ <!-- <input type="button" value="设置属性(list)= [1, '2', false]"
61+ onclick="javascript: window.WebChannelObject.listValue = [1, '2', false];" />
62+ <input type="button" value="获取属性(list)"
63+ onclick="javascript: appendText('listValue:' + window.WebChannelObject.listValue);" /><br /><br />
64+
65+ <input type="button" value="设置属性(map)= {'key1': 1, 'key2': '2', 'key3': false}"
66+ onclick="javascript: window.WebChannelObject.mapValue = {'key1': 1, 'key2': '2', 'key3': false};" />
67+ <input type="button" value="获取属性(map)"
68+ onclick="javascript: appendText('mapValue:' + window.WebChannelObject.mapValue);" /><br /><br /> -->
69+
70+ < input type ="button " value ="调用加法(testAdd(1, 2)) "
71+ onclick ="javascript: window.WebChannelObject.testAdd(1, 2, function(result) { appendText('testAdd ret:' + result); }); " /> < br /> < br />
72+
73+ < input type ="button " value ="设置窗口大小(resize(400, 400)) " onclick ="javascript: window.qtwindow.resize(400, 400); " />
74+ </ body >
75+
76+ </ html >
0 commit comments