PyQt4:QTreeView构造函数导致崩溃:QWidget:必须在QPaintDevice之前构造一个QApplication

时间:2018-02-26 01:44:09

标签: python crash pyqt pyside qapplication

我遇到了一个奇怪的行为,我的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

0 个答案:

没有答案