Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
528 views
in Technique[技术] by (71.8m points)

python - QWidget: Must construct a QApplication before a QPaintDevice

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

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: Sorry for any translation mistakes.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

If you want to create it with the HelloBegin object, move it in the __init__ method.

Edit:

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. Create the QApplication
  2. Open your splash screen / message box
  3. Only then load the other modules

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_())

where createSplashScreen is the function that creates your splash screen


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...