简体   繁体   English

如何使用 python 或 Shell 代码退出 Qapplication (PyQt)

[英]How to Quit Qapplication (PyQt) with python or Shell code

Hi Everybody i'm newbie in almost every technology that i'm about to talk you about below, i'm launching an QApplication with sys.exit(qapp.exec_()) (required because i'm using a QWebView in my python class) and everything's fine with that except that the application does not quit by its own after execution and this is causing a problem when i call this Qapplication through a REST Django webservice (the server won't quit loading), so i was wondering if there's any solution for that, thank you, I was thinking of performing a SIGTSTP (ctrl + z) with python after launching the app, is this a practical solution?大家好,我几乎是下面将要与您讨论的所有技术的新手,我正在使用sys.exit(qapp.exec_())启动QApplication (必需,因为我在我的QWebView中使用了 QWebView类),一切都很好,除了应用程序在执行后不会自行退出,当我通过 REST Django 网络服务调用此 Qapplication 时,这会导致问题(服务器不会退出加载),所以我想知道是否有任何解决方案,谢谢,我正在考虑在启动应用程序后使用 python 执行 SIGTSTP (ctrl + z),这是一个实用的解决方案吗?

Here is a portion of the code这是代码的一部分

def main():
    import sys
    qApp = QtGui.QApplication(sys.argv)

    myappWebView = myappWebView()
    myappWebView.load('http://website.com')
    myappWebView.show()
    sys.exit(qApp.exec_())

if __name__ == "__main__":
    main()

A window is being launched whenever i execute this and the linux console won't prompt me for a new command and is stuck until i close the window manually.每当我执行此操作时,都会启动 window,而 linux 控制台不会提示我输入新命令,并且一直卡住,直到我手动关闭 window。

It's hard to answer without seeing code, but I think you can call QCoreApplication.exit() when job is done.没有看到代码就很难回答,但我认为你可以在工作完成后调用QCoreApplication.exit()

You can quit the application when the page has finished loading:您可以在页面加载完成后退出应用程序:

myappWebView.loadFinished.connect(qApp.quit)

Or if the page has some javascript that needs time to execute, you can use a timer to delay the application closing:或者如果页面有一些 javascript 需要时间来执行,你可以使用计时器来延迟应用程序关闭:

timer = QTimer()
timer.setInterval(2000) # 2 seconds
myappWebView.loadFinished.connect(timer.start)
timer.timeout.connect(qApp.quit)

(Or you can choose a more complicated and maybe more accurate method: How to know when a web page is loaded when using QtWebKit? ). (或者你可以选择更复杂也可能更准确的方法: How to know when a web page is loaded when using QtWebKit? )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM