import json
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QObject, pyqtSlot, QUrl
from PyQt5.QtWebChannel import QWebChannel
from PyQt5.QtWebEngineWidgets import QWebEngineView
class CallHandler(QObject):
@pyqtSlot(result=str)
def myHello(self):
print("777")
zNodes = [
{"id": 1, "pId": 0, "name": "父节点1", "open": True},
{"id": 11111111111, "pId": 1, "name": "父节点11"},
{"id": 1111111, "pId": 11111111111, "name": "叶子节点111"},
{"id": 1121111, "pId": 11111111111, "name": "叶子节点112"},
{"id": 113111, "pId": 11111111111, "name": "叶子节点113"},
{"id": 114111, "pId": 11111111111, "name": "叶子节点114"},
{"id": 12, "pId": 1, "name": "父节点12"},
{"id": 121, "pId": 12, "name": "叶子节点121"},
{"id": 122, "pId": 12, "name": "叶子节点122"},
{"id": 123, "pId": 12, "name": "叶子节点123"},
{"id": 124, "pId": 12, "name": "叶子节点124"},
{"id": 13, "pId": 1, "name": "父节点13", "isParent": True},
{"id": 2, "pId": 0, "name": "父节点2"},
{"id": 21, "pId": 2, "name": "父节点21", "open": True},
{"id": 211, "pId": 21, "name": "叶子节点211"},
{"id": 212, "pId": 21, "name": "叶子节点212"},
{"id": 213, "pId": 21, "name": "叶子节点213"},
{"id": 214, "pId": 21, "name": "叶子节点214"},
{"id": 22, "pId": 2, "name": "父节点22"},
{"id": 221, "pId": 22, "name": "叶子节点221"},
{"id": 222, "pId": 22, "name": "叶子节点222"},
{"id": 223, "pId": 22, "name": "叶子节点223"},
{"id": 224, "pId": 22, "name": "叶子节点224"},
{"id": 23, "pId": 2, "name": "父节点23"},
{"id": 231, "pId": 23, "name": "叶子节点231"},
{"id": 232, "pId": 23, "name": "叶子节点232"},
{"id": 233, "pId": 23, "name": "叶子节点233"},
{"id": 234, "pId": 23, "name": "叶子节点234"},
{"id": 3, "pId": 0, "name": "父节点3", "isParent": True}
]
print(type(zNodes))
print('call received')
data=json.dumps(zNodes,ensure_ascii=False)
print(type(data))
return data
@pyqtSlot(int, result=int)
def myTest(self, test):
print('test is', test)
return False
if __name__ == '__main__':
app = QApplication(sys.argv)
view = QWebEngineView()
channel = QWebChannel()
handler = CallHandler()
channel.registerObject('pyjs', handler) ##前者是str,后者是一个QObject(里面放着需要调用的函数)
view.page().setWebChannel(channel)
url_string = "D:/WorkSpace/PyCharm/power/templates/ztree.html"
view.load(QUrl(url_string))
view.show()
sys.exit(app.exec_())
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title></title>
<script type="text/javascript" src="../static/js/jquery-3.3.1.min.js"></script>
<link rel="stylesheet" href="../static/css/metroStyle.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../static/js/jquery.ztree.all.min.js"></script>
<script type="text/javascript" src="../static/js/qwebchannel.js"></script>
</head>
<body>
<div>
<ul id="treeDemo" class="ztree"></ul>
</div>
<script>
window.onload = function () {
new QWebChannel(qt.webChannelTransport, function (channel) {
window.pyjs = channel.objects.pyjs; //把对象赋值到JS中
// pyjs.myHello(alert);
pyjs.myHello("hhh", function (res) {
var zNodes = JSON.parse(res);
var setting = {
view: {
addHoverDom: addHoverDom,
removeHoverDom: removeHoverDom,
selectedMulti: false,
showLine: false
},
check: {
enable: true
},
data: {
simpleData: {
enable: true
}
},
edit: {
enable: true
},
callback: {
beforeRemove: qt5test
}
};
zTreeObj = $.fn.zTree.init($("#treeDemo"), setting, zNodes); //初始化树
zTreeObj.expandAll(true); //true 节点全部展开、false节点收缩
var newCount = 1;
function addHoverDom(treeId, treeNode) {
var sObj = $("#" + treeNode.tId + "_span");
if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0) return;
var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
+ "' title='add node' onfocus='this.blur();'></span>";
sObj.after(addStr);
var btn = $("#addBtn_" + treeNode.tId);
if (btn) btn.bind("click", function () {
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
zTree.addNodes(treeNode, {
id: (100 + newCount),
pId: treeNode.id,
name: "new node" + (newCount++)
});
return false;
});
};
function removeHoverDom(treeId, treeNode) {
//此处先去数据库查询该节点下是否有文件或文件夹
$("#addBtn_" + treeNode.tId).unbind().remove();
};
var countDeleteTimes = 1;
function zTreeBeforeRemove(treeId, treeNode) {
console.log("treeId:" + treeId)
console.log(treeNode)
console.log("id:" + treeNode.id)
console.log("countDeleteTimes = " + countDeleteTimes)
if (countDeleteTimes % 2 == 0) {
countDeleteTimes++;
return true;
} else {
countDeleteTimes++;
return false;
}
}
async function qt5test(treeId, treeNode) {
removeFlag = true;
let resFlag = 0;
async function f() {
new Promise(function(resolve, reject) {
return pyjs.myTest(treeNode.id, function (res) {
if (res == 0 ) {
res = false;
removeFlag = false;
resFlag = res;
}
alert("1:" + res)
resolve(removeFlag)
return removeFlag;
});
});
}
return removeFlag;
}
})
});
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="../static/js/qwebchannel.js"></script>
<title>QWebChannel测试</title>
<script type="text/javascript" src="../static/js/jquery-3.3.1.min.js"></script>
<script>
window.onload = function () {
new QWebChannel(qt.webChannelTransport, function (channel) {
window.pyjs = channel.objects.pyjs; //把对象赋值到JS中
// pyjs.myHello(alert);
pyjs.myHello(function (res) {
alert(res)
})
});
}
</script>
</head>
<body>
<div id="test">
this is test !
</div>
<div onclick="qt5test();">测试</div>
<button class = 'btn'>点击</button>
<script>
function qt5test() {
pyjs.myTest('这是测试传参的',function (res) {
alert('sssss')
alert(res);
});
}
// pyqt返回来的值必须写一个方法接收,如上面方法所示
// js调用Python函数时,传递给python的参数默认为构造函数,如果想传递自定义参数,随便传,只是在参数列表的最后,
// 一定要加上回调函数,自定义参数需要在python函数的形参列表中声明,回调函数则是默认的,不需要声明。
$('.btn').click(function () {
var con =pyjs.myTest('mxh_connncon')
alert(typeof(con),con)
})
function uptext(msg) {
document.getElementById('test').innerHTML=msg;
}
</script>
</body>
</html>