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
566 views
in Technique[技术] by (71.8m points)

python - "Must construct a QApplication before a QPaintDevice" from QWidget

I'm busy porting an IRC client from Python 2.6 to 3.3 and I've stumbled across a problem with PyQt. The application originally used PyQt4, I'm also recoding it to get it to work with PyQt5 but I'm getting an error without any line references: "QWidget: Must construct a QApplication before a QPaintDevice". I have narrowed the issue down to a single class.

I understand it's been asked here many times already but I couldn't extract a sure-fire answer for my case so I apologise if my question appears ignorant.

Here's some of the code: http://pastebin.com/Lj60icgQ

Stupid me didn't put the "app" variable just after the import statements when I should've. I then put the rest of the code at the bottom of the main file and I'm not longer getting that error. Thanks for the help!

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

I'm afraid single file will not be enough in this situation - the execution flow is not clear just from this one module. The message in question usually appears when you try to use some resources/create some objects that require initialized QApplication - like QIcon, for example.

Instantiation of Qt-based GUI application usually looks like this:

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    main_window = MainWindowClass()
    main_window.show()
    sys.exit(app.exec_())

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

...