我遇到了一个奇怪的行为,我的QApplication似乎没有正确初始化。
我有两个模块:foo.py
和bar.py
。
bar.py
内容:
module: bar
from PyQt4 import QtGui
class MyTreeView(QtGui.QTreeView):
def __init__(self, name, parent=None):
super(MyTreeView, self).__init__(parent)
foo.py
内容:
# module: foo
from PyQt4 import QtGui
import bar
class MyDialog(QtGui.QDialog):
def __init__(self, name, parent=None):
super(MyDialog, self).__init__(parent)
self._name = name
self.setMinimumSize(500, 200)
self.setLayout(QtGui.QHBoxLayout())
self._tree = bar.MyTreeView(name)
self.layout().addWidget(self._tree)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
dialog = MyDialog('foo')
dialog.exec_()
sys.exit(app.exec_())
运行foo.py
时收到以下错误消息:
QWidget: Must construct a QApplication before a QPaintDevice
调试器在调用QTreeView构造函数时停止:
super(MyTreeView, self).__init__(parent)
这里出了什么问题?
系统:Windows 7
Python:2.7.9
PyQt:PyQt4
- 4.8.6