Home  >  Article  >  Backend Development  >  QWidget: QApplication must be constructed before QPaintDevice

QWidget: QApplication must be constructed before QPaintDevice

WBOY
WBOYforward
2024-02-09 11:50:141141browse

QWidget:必须在 QPaintDevice 之前构造 QApplication

Question content

First of all, for compatibility reasons, I use windows 7-64 bit and pyqwt5.2.0, pyqt4.5.4, numpy1.3.0 , python2.6.2 32-bit.

When I run my script this will appear:

QWidget: Must construct a QApplication before a QPaintDevice

Surfing the web, looking for some ways to fix it, I found that qwidget inherits from qobject and qpaintdevice (almost every object I use inherits from it), while qmainwindow inherits qwidget. I also found that some static functions are trying to use some classes, but I don't quite understand what it means.

If anyone could explain it I would be very grateful.

ps: We apologize for any translation errors.


Correct answer


From the code point of view, the error is caused by line 102. When loading a module, you create a qwidget (more precisely qmainwindow). This happens before qapplication is created.

Also, I don't know why you have this start variable, since it doesn't seem to be used.

If you want to create it using the hellobegin object, move it into the __init__ method.

edit:

If you want to display a splash screen when a module is loaded, you need to launch the application from a small, lightweight module. In this module you will:

  1. Create qapplication
  2. Open splash screen/message box
  3. Then load other modules

To make everything work smoothly, I would import the module in a separate function and use a little trick to make sure it only starts when the gui is ready. The code is as follows:

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 the startup screen

The above is the detailed content of QWidget: QApplication must be constructed before QPaintDevice. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete