Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Pyqt5从QtWebChannel移动窗口_Javascript_Python_Pyqt5_Backend_Qtwebchannel - Fatal编程技术网

Javascript Pyqt5从QtWebChannel移动窗口

Javascript Pyqt5从QtWebChannel移动窗口,javascript,python,pyqt5,backend,qtwebchannel,Javascript,Python,Pyqt5,Backend,Qtwebchannel,我正在尝试使MousePresseEvent和mouseMoveEvent事件能够从QtWebChannel移动我的应用程序窗口 我正在使用self.setWindowFlags(QtCore.Qt.FramelessWindowHint)删除窗口标志,并使用带有html、css和js的自定义标志 在html中,我有一个id为“header”的div,它具有自定义窗口标志,我希望按住鼠标左键,可以像拖动传统窗口标志一样拖动窗口 使用后端单击时,“关闭”、“最小化”和“最大化”按钮已经具有各自的功

我正在尝试使MousePresseEvent和mouseMoveEvent事件能够从QtWebChannel移动我的应用程序窗口

我正在使用self.setWindowFlags(QtCore.Qt.FramelessWindowHint)删除窗口标志,并使用带有html、css和js的自定义标志

在html中,我有一个id为“header”的div,它具有自定义窗口标志,我希望按住鼠标左键,可以像拖动传统窗口标志一样拖动窗口

使用后端单击时,“关闭”、“最小化”和“最大化”按钮已经具有各自的功能,但我在创建允许从
移动窗口的功能时遇到问题

如何在QtWebChannel中运行MousePresseEvent和mouseMoveEvent事件来移动窗口并模拟传统的窗口标志

从PyQt5导入QtCore、QtGui、qtwidget、QtWebEngineWidgets、QtWebChannel
从PyQt5.QtCore导入QPoint
类后端(QtCore.QObject):
valueChanged=QtCore.pyqtSignal(str)
def uuu init uuu(self,parent=None):
super()。\uuuu init\uuuu(父级)
self._value=“”
@QtCore.pyqtProperty(str)
def值(自身):
返回自我值
@价值设定者
def值(自身,v):
自我价值=v
self.valueChanged.emit(v)
类小部件(qtwidts.QWidget):
def uuu init uuu(self,parent=None):
super()。\uuuu init\uuuu(父级)
self.webEngineView=QtWebEngineWidgets.QWebEngineView()
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setAttribute(QtCore.Qt.WA_半透明背景)
自我调整大小(650610)
self.setWindowOpacity(0.99)
icon=QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(“favicon.ico”)、QtGui.QIcon.Normal、QtGui.QIcon.Off)
self.setWindowIcon(图标)
lay=qtwidts.QVBoxLayout(self)
lay.setContentsMargins(0,0,0,0)
lay.addWidget(self.webEngineView)
后端=后端(自身)
backend.valueChanged.connect(self.foo_函数)
self.channel=QtWebChannel.QWebChannel()
self.channel.registerObject(“后端”,后端)
self.webEngineView.page().setWebChannel(self.channel)
路径=”http://localhost"
self.webEngineView.setUrl(QtCore.QUrl(路径))
@QtCore.pyqtSlot(str)
def foo_函数(自身、值):
打印(“JS:,值)
如果名称=“\uuuuu main\uuuuuuuu”:
导入系统
app=qtwidts.QApplication(sys.argv)
w=Widget()
w、 show()
sys.exit(app.exec_())
片段html代码


班克斯1.0
github上的完整html代码:

代码最小html:


身体{
保证金:0;
}
#标题{
宽度:100%;
高度:30px;
背景:红色;
位置:绝对位置;
}

您必须检测鼠标事件,将它们发送到python并实现窗口移动:

main.py

导入json
导入操作系统
从PyQt5导入QtCore、QtGui、QtWidgets、QtWebEngineWidgets、QtWebChannel
类WindowManager(QtCore.QObject):
def uuu init uuu(self,window,parent=None):
super()。\uuuu init\uuuu(父级)
self.\u window=window
self.\u drag\u position=QtCore.QPoint()
@财产
def窗口(自):
返回自我。\u窗口
@QtCore.pyqtSlot(str)
def MousePresseEvent(自、字符串事件):
event=json.load(字符串\事件)
如果事件[“which”]==1:
gp=QtCore.QPoint(事件[“screenX”]、事件[“screenY”])
self.drag_position=gp-self.window.frameGeometry().topLeft()
@QtCore.pyqtSlot(str)
def mouseMoveEvent(自身、字符串事件):
event=json.load(字符串\事件)
如果事件[“which”]==1:
gp=QtCore.QPoint(事件[“screenX”]、事件[“screenY”])
self.window.move(gp-self.drag_位置)
类WebEngineView(QtWebEngineWidgets.QWebEngineView):
def uuu init uuu(self,parent=None):
super()。\uuuu init\uuuu(父级)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setAttribute(QtCore.Qt.WA_半透明背景)
self.setWindowOpacity(0.99)
自我调整大小(640480)
icon=QtGui.QIcon()
icon.addPixmap(
QtGui.QPixmap(“favicon.ico”),QtGui.QIcon.Normal,QtGui.QIcon.Off
)
self.setWindowIcon(图标)
self.window\u manager=WindowManager(self.window())
self.channel=QtWebChannel.QWebChannel()
self.channel.registerObject(“窗口管理器”,self.window\u管理器)
self.page().setWebChannel(self.channel)
current_dir=os.path.dirname(os.path.realpath(uu文件_uu))
self.load(QtCore.QUrl.fromLocalFile(os.path.join(current_dir,“index.html”))
如果名称=“\uuuuu main\uuuuuuuu”:
导入系统
sys.argv.append(“--远程调试端口=8000”)
app=qtwidts.QApplication(sys.argv)
w=WebEngineView()
w、 show()
sys.exit(app.exec_())
index.html

<!DOCTYPE html>
<html>
<head>
    <script src="qrc:///qtwebchannel/qwebchannel.js"></script>
    <style type="text/css">

    body {
        margin: 0;
    }

    #header {
        width: 100%;
        height: 30px;
        background: red;
        position: absolute;
    }

    </style>
</head>
<body>
    <div id="header"></div>

    <script type="text/javascript">

        function event_to_string(event){
            return JSON.stringify({"screenX": event.screenX, "screenY": event.screenY, "which": event.which})
        }
        var window_manager = null;
        if (typeof QWebChannel !== "undefined") {
            new QWebChannel(qt.webChannelTransport, function (channel) {
                window_manager = channel.objects.window_manager;
                var header = document.getElementById("header")
                header.addEventListener('mousedown', function(event){
                    window_manager.mousePressEvent(event_to_string(event))
                });
                header.addEventListener('mousemove', function(event){
                    window_manager.mouseMoveEvent(event_to_string(event))
                });
            });
        }
    </script>
</body>

身体{
保证金:0;
}
#标题{
宽度:100%;
高度:30px;
背景:红色;
位置:绝对位置;
}
函数事件到字符串(事件){
返回JSON.stringify({“screenX”:event.screenX,“screenY”:event.screenY,“which”:event.which})
}
var窗口管理器=null;
if(QWebChannel的类型!=“未定义”){
新的QWebChannel(qt.WebChannel传输,功能(通道){
窗口\u管理器=channel.objects.window\u管理器;
var header=document.getElementById(“header”)
header.addEventListener('mousedown',函数(事件){
窗口管理器.鼠标压力事件(事件到字符串(事件))
});
header.addEventListener('mousemove',函数(事件){
窗口管理器.mouseMoveEvent(事件到字符串(事件))
});
});
}
共享您的html。