且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

QWidget:必须在 QPaintDevice 之前构造一个 QApplication

更新时间:2023-02-24 18:20:18

从代码来看,错误是由于第 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:

  1. 创建 QApplication
  2. 打开启动画面/消息框
  3. 然后才加载其他模块

为了一切顺利,我会在一个单独的函数中导入模块,并使用一个小技巧来确保它只在 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