Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QWidget: Must construct a QApplication before a QPaintDevice

Tags:

python

pyqt

pyqt4

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.

like image 741
bomba Avatar asked Jul 12 '12 13:07

bomba


1 Answers

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

like image 182
PierreBdR Avatar answered Oct 17 '22 10:10

PierreBdR