5
votes

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.

2
As the error said, you are not allowed to create a QWidget (or any QPaintDevice) before the QApplication is created. But without the code that creates the problem, I don't see how we can help beside repeating the error message ... - PierreBdR
probably, while I was trying to fix it I messed up - bomba
This question should likely be deleted as the O.P. posted his code to a temporary pastebin (which no longer exists). This really isn't a useful question. - weberc2

2 Answers

6
votes

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

0
votes

I encountered the error while running unittest.

QWidget: Must construct a QApplication before a QWidget

The solution was constructing the QApplication for every test method.

app = QtWidgets.QApplication(sys.argv)