repzero repzero - 2 months ago 23
Python Question

PySide:QWebView's Mouse move and Mouse press events Freezing html document

I am trying to make a QwebView window move whenever I drag the mouse within the window (not the title bar) while at the same time events in the html documents can trigger.

Here is my implementation of it

import sys
import json
import threading
from time import sleep
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtWebKit import QWebView, QWebSettings
from PySide.QtNetwork import QNetworkRequest
from PySide.QtCore import QObject, Slot, Signal

#html_str="".join((open("/root/adstar_gui_generate.html","r").readlines()))
html_str="""
<!doctype>
<body>
<html>
<div class="btns">Books</div>
<div class="btns">Enteraintment</div>
<div class="btns">Sport</div>
</html>
<style>
.btns{
display:inline-block;
color:white;
background:black;
}
.btns:hover{
background:green;
cursor:pointer;
}
</style>
</body>
</doctype>

"""


class move_win(QObject):
def __init__(self):
super(move_win,self).__init__()

@Slot(str)
def move(self,pos):
print pos
pos=json.loads(pos)
#x_w = event.pos().X
#y_w = event.pos().X
dial.webview.move(pos["x"],pos["y"])


class web_view(QWebView):
def __init__(self):
super(web_view,self).__init__()
self.mousedown=None
self.mouseup=None
def mouseReleaseEvent(self,event):
self.mouseup=True
self.mousedown=False

def mousePressEvent(self, event):
#self.offset = event.pos()
self.offset = event.pos()
self.mouseup=False
self.mousedown=True

def mouseMoveEvent(self, event):
if self.mousedown is True:
x=event.globalX()
y=event.globalY()
x_w = self.offset.x()
y_w = self.offset.y()
self.move(x-x_w, y-y_w)


if __name__=="__main__":
Qapp=QApplication(sys.argv)
t=web_view()
t.settings().setAttribute(QWebSettings.WebAttribute.DeveloperExtrasEnabled, True)
t.setHtml(html_str)
t.show()
QCoreApplication.processEvents()
Qapp.exec_()


Running the above codes, One can see that hovering over the html buttons does not work most of the time (the color of the button does not change neither the cursor).


My thoughts

I am assuming that this is due to mousePressEvent and mouseMoveEvents of QWebview Itself (well my implementation of it) preventing or causing the html document to freeze.



Note

If I commented out the mousePressEvent and mouseMoveEvent from the class web_view. hovering over the html buttons works (the buttons change color and the cursor changes)

I tried creating a QThread to determine if an asynchronous approach can be the solution (same bad luck)...

Question:

How can drag the QWebView Window without causing the html document in the window to freeze ( A simple approach will be welcomed since I am new to PySide)

Answer

You forgot to call super methods of mouse*Event.

Reason: Those methods are called event handler. Since you overridden it and didn't call default implementation, QWebView have no chance to know mouse states (But it was not "freezed").

Qt events overview

class web_view(QWebView):
    def __init__(self):
        super(web_view,self).__init__()
        self.mousedown=None
        self.mouseup=None

    def mouseReleaseEvent(self,event):
        self.mouseup=True
        self.mousedown=False
        super(web_view, self).mouseReleaseEvent(event)    # here

    def mousePressEvent(self, event):
        #self.offset = event.pos()
        self.offset = event.pos()
        self.mouseup=False
        self.mousedown=True
        super(web_view, self).mousePressEvent(event)    # here

    def mouseMoveEvent(self, event):
        if self.mousedown is True:
            x=event.globalX()
            y=event.globalY()
            x_w = self.offset.x()
            y_w = self.offset.y()
            self.move(x-x_w, y-y_w)
        super(web_view, self).mouseMoveEvent(event)    # here