0

我遇到了一个奇怪的行为,我的 QApplication 似乎没有正确初始化。

我有两个模块:foo.pybar.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

4

0 回答 0