QWidget:必须在 QPaintDevice 之前构造一个 QApplication [英] QWidget: Must construct a QApplication before a QPaintDevice
问题描述
首先,出于兼容性原因,我使用 Windows 7-64 位和 PyQwt5.2.0、PyQt4.5.4、NumPy1.3.0、python2.6.2 32 位.
First, I use Windows 7-64 bit with PyQwt5.2.0, PyQt4.5.4, NumPy1.3.0, python2.6.2 32-bit for compatibility reasons.
在运行我的脚本时出现:
While runnning my script this appears:
QWidget: Must construct a QApplication before a QPaintDevice
在网上冲浪,寻找修复它的方法,我发现 QWidget
继承了 QObject
和 QPaintDevice
(它是继承的,再见几乎我使用的每个对象),并且 QMainWindow
继承了 QWidget
.我还了解到某些静态函数正在尝试使用某个类,但我不太明白这意味着什么.
Surfing the net, looking for some way to fix it, I got that QWidget
inherits QObject
and QPaintDevice
(and it is inherited bye almost every object I use), and QMainWindow
inherits QWidget
. I also got that some static function is trying to use some class, but I do not really understand what it means.
如果有人能解释一下,我将不胜感激.
If someone could please explain this, I would really appreciate it.
PS:如有翻译错误,请见谅.
PS: Sorry for any translation mistakes.
推荐答案
从代码来看,错误是由于第 102 行.在那里,在加载模块时,您创建了一个 QWidget
(更多正好是一个 QMainWindow
).这发生在之前QApplication
被创建.
From the code, the error is due to line 102. There, while the module is loaded, you create a QWidget
(more precisely a QMainWindow
). And this happens before the QApplication
is created.
另外,我不知道为什么你在那里有这个起始变量,因为它似乎没有被使用.
Also, I don't know why you have this start variable there, as it doesn't seem to be used.
如果要使用 HelloBegin
对象创建它,请将其移动到 __init__
方法中.
If you want to create it with the HelloBegin
object, move it in the __init__
method.
如果您想在加载模块时显示启动画面,您需要由一个小型轻量级模块启动应用程序.在本模块中,您将:
If you want to display a splash screen while your modules are loading, you need the application to be started by a small, lightweight, module. In this module, you will:
- 创建 QApplication
- 打开启动画面/消息框
- 然后才加载其他模块
为了一切顺利,我会在一个单独的函数中导入模块,并使用一个小技巧来确保它只在 GUI 准备好后才启动.代码将如下所示:
For everything to work smoothly, I would import the modules in a separate function, and use a small trick to make sure it's started only once the GUI is ready. The code will look like this:
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import QTimer
def startApp():
import m1
import m2
wnd = createWindow()
wnd.show()
import sys
app = QApplication(sys.argv)
splash = createSplashScreen()
splash.show()
QTimer.singleShot(1, startApp) # call startApp only after the GUI is ready
sys.exit(app.exec_())
其中 createSplashScreen
是创建启动画面的函数
where createSplashScreen
is the function that creates your splash screen
这篇关于QWidget:必须在 QPaintDevice 之前构造一个 QApplication的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!