Skip to content

Commit 4aa9ad3

Browse files
committed
CallEachWithJs
1 parent 48ef9a5 commit 4aa9ad3

File tree

9 files changed

+794
-0
lines changed

9 files changed

+794
-0
lines changed

QWebChannel/CallEachWithJs.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
Created on 2021/12/15
5+
@author: Irony
6+
@site: https://pyqt.site , https://github.com/PyQt5
7+
8+
@file: CallEachWithJs.py
9+
@description: 与JS之间的互相调用
10+
"""
11+
12+
import os
13+
14+
from PyQt5.QtCore import QUrl, pyqtSlot
15+
from PyQt5.QtGui import QDesktopServices
16+
from PyQt5.QtWidgets import (QApplication, QLineEdit, QPushButton, QVBoxLayout,
17+
QWidget)
18+
19+
from Lib.WebChannelObject import WebChannelObject
20+
21+
22+
class Window(QWidget):
23+
24+
def __init__(self, *args, **kwargs):
25+
super(Window, self).__init__(*args, **kwargs)
26+
self.m_obj = WebChannelObject(self)
27+
# 注册该窗口,可以访问该窗口的属性,槽函数,信号
28+
# https://doc.qt.io/qt-5/qwidget.html#properties
29+
# https://doc.qt.io/qt-5/qwidget.html#signals
30+
# https://doc.qt.io/qt-5/qwidget.html#public-slots
31+
self.m_obj.registerObject('qtwindow', self)
32+
self.m_obj.start()
33+
34+
layout = QVBoxLayout(self)
35+
self.editTitle = QLineEdit(self, placeholderText='输入标题')
36+
layout.addWidget(self.editTitle)
37+
layout.addWidget(QPushButton('修改标题', self, clicked=self.onChangeTitle))
38+
39+
QDesktopServices.openUrl(
40+
QUrl.fromLocalFile(
41+
os.path.join(os.path.dirname(sys.argv[0] or __file__),
42+
'Data/CallEachWithJs.html')))
43+
44+
def onChangeTitle(self):
45+
self.setWindowTitle(self.editTitle.text())
46+
47+
# ------- 把非槽函数通过pyqtSlot重新暴露 -------
48+
@pyqtSlot(int, int)
49+
def resize(self, width, height):
50+
super().resize(width, height)
51+
52+
53+
if __name__ == '__main__':
54+
import cgitb
55+
import sys
56+
57+
cgitb.enable(format='text')
58+
app = QApplication(sys.argv)
59+
w = Window()
60+
w.show()
61+
sys.exit(app.exec_())

QWebChannel/Data/CallEachWithJs.html

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)