“必须在 QPaintDevice 之前构造 QApplication";来自 QWidget [英] "Must construct a QApplication before a QPaintDevice" from QWidget
问题描述
我正忙于将 IRC 客户端从 Python 2.6 移植到 3.3,并且偶然发现了 PyQt 的问题.该应用程序最初使用 PyQt4,我也在重新编码它以使其与 PyQt5 一起使用,但我收到一个没有任何行引用的错误:QWidget:必须在 QPaintDevice 之前构造一个 QApplication".我已将问题缩小到一个类.
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.
这是一些代码:http://pastebin.com/Lj60icgQ
愚蠢的我没有把app"变量放在应该的导入语句之后.然后我把其余的代码放在主文件的底部,我不会再收到那个错误了.感谢您的帮助!
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!
推荐答案
在这种情况下,恐怕单个文件是不够的 - 仅从这个模块来看执行流程并不清楚.当您尝试使用某些资源/创建某些需要初始化 QApplication 的对象(例如 QIcon)时,通常会出现有问题的消息.
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.
基于 Qt 的 GUI 应用程序的实例化通常如下所示:
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_())
这篇关于“必须在 QPaintDevice 之前构造 QApplication";来自 QWidget的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!