PySide:QWebView的鼠标移动和鼠标按下事件冻结html文档

时间:2016-09-18 15:26:15

标签: python pyqt pyside

我试图在窗口(而不是标题栏)中拖动鼠标时移动QwebView窗口,同时html文档中的事件可以触发。

这是我的实现

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_()

运行上面的代码,可以看到悬停在html按钮上的大部分时间都不起作用(按钮的颜色不会改变光标)。

<小时/> 我的想法

我假设这是由于QWebview本身的mousePressEvent和mouseMoveEvents(我的实现)阻止或导致html文档冻结。

<小时/> 注意

如果我从类web_view中注释掉了mousePressEvent和mouseMoveEvent。将鼠标悬停在html按钮上(按钮会改变颜色并且光标会改变)

我尝试创建一个QThread来确定异步方法是否可以成为解决方案(同样糟糕的运气)......

问题:

如何在不导致窗口中的html文档冻结的情况下拖动QWebView窗口(因为我是PySide的新手,所以欢迎使用简单的方法)

1 个答案:

答案 0 :(得分:5)

你忘了调用鼠标*事件的超级方法。

原因:这些方法称为事件处理程序。由于你重写它并且没有调用默认实现,QWebView没有机会知道鼠标状态(但它不是&#34;冻结&#34;)。

Qt events overview

2016-09-21 13:47:32.912  WARN 1764 --- [nio-8090-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 17059, SQLState: 99999
2016-09-21 13:47:32.912 ERROR 1764 --- [nio-8090-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : Fail to convert to internal representation
2016-09-21 13:47:32.912  INFO 1764 --- [nio-8090-exec-1] o.h.e.internal.DefaultLoadEventListener  : HHH000327: Error performing load command : org.hibernate.exception.GenericJDBCException: Could not read entity state from ResultSet : EntityKey[com.entity.Sub#1283]
2016-09-21 13:47:33.240 ERROR 1764 --- [nio-8090-exec-1] o.a.c.c.C.[.[.[/].[servletContainer]     : Servlet.service() for servlet [servletContainer] in context with path [] threw exception [org.springframework.orm.jpa.JpaSystemException: Could not read entity state from ResultSet : EntityKey[com.Sub#1283]; nested exception is org.hibernate.exception.GenericJDBCException: Could not read entity state from ResultSet : EntityKey[com.entity.Sub#1283]] with root cause

java.sql.SQLException: Fail to convert to internal representation
    at oracle.jdbc.driver.CharCommonAccessor.getInt(CharCommonAccessor.java:147) ~[ojdbc6-11.2.0.2.0.jar:11.2.0.2.0]
    at oracle.jdbc.driver.T4CVarcharAccessor.getInt(T4CVarcharAccessor.java:818) ~[ojdbc6-11.2.0.2.0.jar:11.2.0.2.0]
    at oracle.jdbc.driver.OracleResultSetImpl.getInt(OracleResultSetImpl.java:928) ~[ojdbc6-11.2.0.2.0.jar:11.2.0.2.0]
    at oracle.jdbc.driver.OracleResultSet.getInt(OracleResultSet.java:434) ~[ojdbc6-11.2.0.2.0.jar:11.2.0.2.0]
    at org.hibernate.type.EnumType$OrdinalEnumValueMapper.getValue(EnumType.java:337) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.type.EnumType.nullSafeGet(EnumType.java:231) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]