我正在尝试在 QMdiSubWindow 中使用 FigureCanvasQTAgg,以便用户可以即时创建他/她自己的绘图。我制作了这个非常小的自包含代码:
from PyQt4 import QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import sys
class ExampleApp(QtGui.QMainWindow):
def __init__(self):
super(self.__class__, self).__init__()
self.mdiarea = QtGui.QMdiArea()
self.setCentralWidget(self.mdiarea)
sub = QtGui.QMdiSubWindow(self.mdiarea)
fig = Figure()
p = FigureCanvas(fig)
sub.layout().addWidget(p)
sub.show()
def main():
app = QtGui.QApplication(sys.argv)
form = ExampleApp()
form.show()
app.exec_()
if __name__ == '__main__':
main()
当我运行程序并尝试最小化 QtGui.QMdiSubWindow 对象时会出现问题。当我这样做时,程序会出现段错误并退出而没有错误描述。这可能是 qt、python 绑定或 FigureCanvasQTAgg 对象中的错误。当然也可能是我错误地使用了这些对象。请帮助我理解为什么当我最小化子窗口时会发生段错误并帮助我弄清楚如何解决这个问题。谢谢你。
我的环境是 ubuntu 14.04 并使用 Qt 版本:4.8.7 SIP 版本:4.16.9 PyQt 版本:4.11.4 MatplotLib 版本:1.5.0
这是拖放属性集的示例。这似乎也有问题。
from PyQt4 import QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import sys
class QtZListView(QtGui.QListView):
def __init__(self, *args, **kwargs):
QtGui.QListView.__init__(self, *args, **kwargs)
self.model = QtGui.QStringListModel(['a','b','c'])
self.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
self.setModel(self.model)
self.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
self.setDragEnabled(True)
def setStringList(self, *args, **kwargs):
return self.model.setStringList(*args, **kwargs)
class mplsubwindow(QtGui.QMdiSubWindow):
def __init__(self, *args, **kwargs):
QtGui.QMdiSubWindow.__init__(self, *args, **kwargs)
self.setWindowTitle("testing")
self.setAcceptDrops(True)
self.resize(400,400)
self.show()
def dragEnterEvent(self, event):
print('entering')
super(mplsubwindow, self).dragEnterEvent(event)
def dragMoveEvent(self, event):
print('drag moving')
super(mplsubwindow, self).dragMoveEvent(event)
def dropEvent(self, event):
print('dropped')
super(mplsubwindow, self).dropEvent(event)
class ExampleApp(QtGui.QMainWindow):
def __init__(self):
super(self.__class__, self).__init__()
mainwid = QtGui.QWidget()
self.mdiarea = QtGui.QMdiArea()
layout = QtGui.QGridLayout(mainwid)
layout.addWidget(self.mdiarea)
sub = mplsubwindow(self.mdiarea)
sub.show()
layout.addWidget(QtZListView())
self.setCentralWidget(mainwid)
#self.setWidget(mainwid)
def main():
app = QtGui.QApplication(sys.argv)
form = ExampleApp()
form.show()
app.exec_()
if __name__ == '__main__':
main()