Pyside: addToJavaScriptWindowObject not working properly when application is compiled with cython

筅森魡賤 提交于 2020-03-22 03:39:13

问题


I have built an application that runs a couple thousand of lines of code.

I am trying to hide the codes by compiling the python codes to c codes and then to an object file.

This application works well in pyside. I converted this python file .py to a .c file and compiled the c to and object file .o. The name of this file is test2.py and my steps to convert it to an .o file:

 cython --embed -o test2.c test2.py

from the above I now compile the test2.c to test2.o as follows

gcc -I /usr/include/python2.7 -o test2.o -fPIC test2.c -lpython2.7 -lpthread -lm -lutil -ldl

My problem is the minimize and close button designed in the webkit frame is not working after compiling (However It works well when executed from python file) and other addtoJavascripWindowObjects are not working

I developed a simple frame window model with nothing inside using Pyside

The test2.py file codes are as follows: My codes are below

from __future__ import print_function
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

title_bar_str="""<!doctype html>
<html>
<body>

<section id="exit_panel">
    <div id="window_title_meta_container">
        <img id="window_title_icon" draggable="false">
        <div id="window_title"></div>
    </div>
    <div id="min_close_container">
    <div id="min_ap" class="win_signals">_</div>
    <div id="close_ap" class="win_signals">x</div>
    </div>
</section>
<style>
img#window_title_icon{
    display: none;
    width:25px;
    height:25px;
    vertical-align:top;
    border:none;
}
#window_title_meta_container{
    float:left;
    color:grey !important;
}
#window_title_meta_container,#window_title_icon,#window_title{
    display:inline-block;
}
#title_bar{
    color:black;
    background: -webkit-linear-gradient(blue,grey);
    font-size:15px;
}
.btns{
    position:relative;
    display:inline-block;
    padding:5px;
    border:solid red 1px;
    background:red;
    left:85%
}
body{
    border:solid black 1px;
    height:100%;
    margin:auto;
    background:red;
    -webkit-user-select:none;
    -moz-user-select:none;

}
body:hover{
    cursor:pointer;
}
html,body{
    overflow:hidden
}
.win_signals{
    display:inline-block;
    padding:5px;

}
#close_ap,#min_ap{
color: white;
background: rgb(18, 110, 201) none repeat scroll 0% 0%;
border-radius: 10px;            
}
#close_ap:hover,#min_ap:hover{
    cursor:pointer;
    background:green;
}
#min_close_container{
position: relative;;
display: inline-block;
}

#exit_panel{
    text-align: right;
    background: -webkit-linear-gradient(#104477, #000) repeat scroll 0% 0% transparent;
    color: white;
}
</style>
<script>
window.onload=function(){
    document.getElementById('close_ap').onclick=function(){
        console.log(trigger_title_signals.close(''))
    }
    document.getElementById('min_ap').onclick=function(){
        console.log(trigger_title_signals.minimize(''))
    }    


}

</script>
</body>
</html>
</doctype>

"""
class Frame(QFrame,QObject):
    def __init__(self):
        super(Frame,self).__init__()
        self.gui_height=None
        self.title_bar_height=None
        self.html_title_bar_webview=web_view_with_move(self)
        self.html_title_bar_webview.init_signals()
        self.html_title_bar_webview.setContent(title_bar_str)
        self.html_title_bar_webview.page().mainFrame().setScrollBarPolicy(Qt.Vertical,Qt.ScrollBarAlwaysOff)
        self.html_title_bar_webview.page().mainFrame().setScrollBarPolicy(Qt.Horizontal,Qt.ScrollBarAlwaysOff)
        self.html_title_bar_webview.page().mainFrame().addToJavaScriptWindowObject("Frame",self)      
        self.title_bar_q_widget=title_bar_q()
        self.title_bar_q_widget.setMaximumHeight(35)
        self.title_bar_q_widget.setMinimumHeight(35)
        self.title_bar_q_widget.setContentsMargins(0,0,0,0)
        #self.title_bar_q_widget.layout.setSpacing(0)
        self.title_bar_q_widget_layout=QGridLayout()
        self.title_bar_q_widget_layout.setSpacing(0)
        self.title_bar_q_widget_layout.setContentsMargins(0,0,0,0)
        self.title_bar_q_widget_layout.setVerticalSpacing(0);
        self.title_bar_q_widget_layout.addWidget(self.html_title_bar_webview)
        self.title_bar_q_widget.setLayout(self.title_bar_q_widget_layout)
        #self.html_title_bar_webview.page().mainFrame().evaluateJavaScript("Frame.adjustTitleBarHeight(getComputedStyle(document.body,null).height)")  
        #######
        self.adstar_gui_webview=web_view()
        self.adstar_gui_webview.page().mainFrame().addToJavaScriptWindowObject("Frame",self)          
        self.adstar_gui_webview.show()

        self.adstar_gui_webview.page().mainFrame().setScrollBarPolicy(Qt.Vertical,Qt.ScrollBarAlwaysOff)
        self.adstar_gui_q_widget=QWidget()
        self.adstar_gui_q_widget.setContentsMargins(0,0,0,0)
        self.adstar_gui_q_widget_layout=QGridLayout()
        self.adstar_gui_q_widget_layout.setSpacing(0)
        self.adstar_gui_q_widget_layout.setVerticalSpacing(0)
        self.adstar_gui_q_widget_layout.setContentsMargins(0,0,0,0)
        self.adstar_gui_q_widget_layout.addWidget(self.adstar_gui_webview)
        self.adstar_gui_q_widget.setLayout(self.adstar_gui_q_widget_layout)

        ######
        #self.html_title_bar.show()
#        self.adstar_gui.show()
        self.layout=QGridLayout()
        self.layout.setContentsMargins(0,0,0,0)
        self.layout.setSpacing(0)
        self.layout.setVerticalSpacing(0)
        #self.layout.setHorizontalSpacing(0)        
        self.layout.addWidget(self.title_bar_q_widget)   
        self.layout.addWidget(self.adstar_gui_q_widget)
        self.setLayout(self.layout)
        self.setWindowFlags(Qt.FramelessWindowHint)
    @Slot(str)
    def python_evaluate_variable(self,var):
        global stop_session
        if var != "":
            exec(var)                    
    @Slot(str)
    def setWindowTitle(self,win_title_str):
        self.win_title_str=win_title_str
        self.html_title_bar_webview.page().mainFrame().evaluateJavaScript("document.getElementById('window_title').textContent='%s'" % (self.win_title_str,))

    def closeEvent(self,event):
        global main
        #super(Frame,self).closeEvent(event)
        try:
            if main==None:
                return
        except NameError:
            main=None
        if self==main:
            for child_win in all_windows:
                child_win.close()
                #del self
                QCoreApplication.processEvents()

class web_view_with_move(QWebView):
    def __init__(self,qframe):
        super(web_view_with_move,self).__init__()
        self.mouseup=None
        self.mousedown=None
        self.qframe=qframe
        self.settings().setAttribute(QWebSettings.WebAttribute.DeveloperExtrasEnabled, True)
    def mouseReleaseEvent(self,event):
        super(web_view_with_move,self).mouseReleaseEvent(event)        
        self.mouseup=True
        self.mousedown=False

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

    def init_signals(self):
        self.title_bar_signals=title_bar_signals(self.qframe)
        self.page().mainFrame().addToJavaScriptWindowObject("trigger_title_signals",self.title_bar_signals)



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


class title_bar_signals(QObject):
    def __init__(self,qframe):
        super(title_bar_signals,self).__init__()
        self.qframe=qframe
    @Slot(str)
    def close(self,tmp):
        self.qframe.close()
        #del self
        QCoreApplication.processEvents()
    @Slot(str)
    def minimize(self,tmp):
        self.qframe.showMinimized()
        #self.qframe.setWindowState(Qt.WindowMinimized)
        QCoreApplication.processEvents()

class title_bar_q(QWidget):
    def __init__(self):
        super(title_bar_q,self).__init__()


    def mouseReleaseEvent(self,event):
        super(title_bar_q,self).mouseReleaseEvent(event)
        self.mouseup=True
        self.mousedown=False



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


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

class web_view(QWebView):
    def __init__(self):
        super(web_view,self).__init__()
        self.settings().setAttribute(QWebSettings.WebAttribute.DeveloperExtrasEnabled, True)

def main():
    try:
        Qapp=QApplication(sys.argv)
        ev_runner=QCoreApplication
        main=Frame()
        main.setAttribute(Qt.WA_DeleteOnClose)
        main.adstar_gui_q_widget.setMinimumWidth(850)
        main.adstar_gui_q_widget.setMaximumWidth(850)    
        main.setMinimumHeight(790)
        main.setMaximumHeight(790)
        main.setWindowTitle("Application")
        ev=QCoreApplication
        main.show()
        QCoreApplication.processEvents()
        Qapp.exec_()
    except KeyboardInterrupt:
        pass    
if __name__=="__main__":
    main()

Note: How I implemented the title bar

  • I created a webview frame in the class named Frame
    self.html_title_bar_webview=web_view_with_move(self)
    

  • I executed a class init_signal() which adds the javascript window object
    self.html_title_bar_webview.init_signals()
    

    来源:https://stackoverflow.com/questions/40965776/pyside-addtojavascriptwindowobject-not-working-properly-when-application-is-com

  • 易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!