0

I'm currently watching some videos for learning PyQT.

I wrote some code, which the instructor was going through in the tutorial into my IDE (wing) and run it and it worked fine. Today, I've gone to run it and I'm getting this error:

QWidget: Must construct a QApplication before a QPaintDevice

But nothing's changed (in my code). I've opened the instructor's code (which is pretty much verbatim code) and the same error. But it worked the other day and now it doesn't. What gives? Has someone had the same experience? It's driving me crazy!! I'm running OS X, and writing code in Wing IDE if that's of any relevance.

The code I was trying to run:

import PyQt4.QtCore as qc
import PyQt4.QtGui as qg

class simpleUI(qg.QDialog):
    def __init__(self):
        qg.QDialog.__init__(self)
        self.setWindowTitle('Simple UI')
        self.setWindowFlags(qc.Qt.WindowStaysOnTopHint)
        self.setModal(False)
        self.setFixedHeight(250)
        self.setFixedWidth(300)

dialog = simpleUI()
dialog.show()

Help!!!!

James Porter
  • 119
  • 2
  • 12
  • I can resolve it by adding: `app = qg.QApplication(sys.argv)` at the beginning and: `sys.exit(app.exec_())` at the end of the code. But it still begs the question, why did it work without it before, but not now? – James Porter Jul 24 '16 at 07:55
  • Just guessing (because I've never used a IDE), but it's probably because Wing automatically creates a `QApplication` so that the code can be run/debugged interactively. It seems like a pretty poor tutorial if it doesn't teach you how to write a basic pyqt script properly, though. The `QApplication` class is fundamental to how all Qt applications work. See [this answer](http://stackoverflow.com/a/8763339/984421) for a standard "hello world" example. – ekhumoro Jul 24 '16 at 23:30
  • What's also weird is the example works on Linux, but not on OS X – James Porter Jul 25 '16 at 00:07

0 Answers0