我正在尝试使用WebView在pyqt中开发漂亮的应用程序并且有一点问题。 我想通过在WebView中按下按钮来创建新窗口。
我有主窗口( 简短视图 ):
class LoginWindow(QWidget):
def __init__(self):
# creating window and different properties...
# Its not important,i think.
self.foo = LoginConnector(self)
view.page().mainFrame().addToJavaScriptWindowObject("foo", self.foo)
... JS连接器
class LoginConnector(QObject):
@pyqtSlot(result=str)
def get_user_list(self):
return ','.join(core.available_users())
@pyqtSlot()
def new_window(self):
print 'Hello!'
#What can i do here?
@pyqtSlot()
def quit(self):
QApplication.quit()
当我在窗口中按下按钮时,它从LoginConnector调用new_window(),但是如何在LoginConnector中创建Dialog?
解决了!我是白痴,听下面那个聪明的家伙
答案 0 :(得分:1)
看看api文档。 http://pyqt.sourceforge.net/Docs/PyQt4/classes.html 如果你不喜欢这个,那么PySide与语法上的一些差异基本相同。 http://pyside.github.io/docs/pyside/
class LoginWindow(QWebView):
def __init__(self, *args, **kwargs):
super(QWebView, self).__init__(*args, **kwargs):
...
self.foo = LoginConnector(self)
self.loadPage("httpdocs/login.hmtl")
# end Constructor
def loadPage(self, web_page):
"""Loads a web page in the browser.
Args:
webPage (str): The URL of the desired web page with JavaScript Python Communication.
"""
# Inspector - right click inspect to debug javascript and have access to the console.
inspector = QtWebKit.QWebInspector()
inspector.setPage(self.page())
# Force relative path otherwise you get a does not except file:// error
if os.path.isabs(web_page):
web_page = os.path.relpath(web_page)
url = QtCore.QUrl(web_page)
self.load(url)
self.frame = self.page().mainFrame()
self.frame.addToJavaScriptWindowObject("foo", self.foo)
# end loadPage
# end class LoginWindow
class LoginConnector(QObject):
@pyqtSlot()
def new_window(self):
self.dialog = QDialog()
self.dialog.show() # or exec
# end new_window
# end class LoginConnector
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = LoginWindow()
window.show()
sys.exit(app.exec_())
这是PySide的主要应用程序结构。 PyQt应该是类似的。