当我最小化 QMdiSubWindow 时,嵌套在 QMdiSubWindow 段错误中的 FigureCanvasQTAgg [英] A FigureCanvasQTAgg nested in a QMdiSubWindow segfaults when i minimize the QMdiSubWindow

查看:131
本文介绍了当我最小化 QMdiSubWindow 时,嵌套在 QMdiSubWindow 段错误中的 FigureCanvasQTAgg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在QMdiSubWindow中包含FigureCanvasQTAgg,以便用户可以即时创建自己的绘图.我编写了一个非常小的自包含代码:

I am trying to have FigureCanvasQTAgg inside QMdiSubWindow such that the user can create his/her own plots on the fly. I have made this very small self contained code:

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对象中的错误.当然也可能是我错误地使用了这些对象.请帮助我了解当我最小化子窗口时为什么会出现段错误,并帮助我弄清楚如何解决此问题.谢谢.

The problem occurs when i run the program and try to minimize the QtGui.QMdiSubWindow object. When I do that the program segfaults and exits with no error description. This could be an error in qt, in the python bindings or in the FigureCanvasQTAgg object. Of course it could also be me who just use these objects wrongly. Please help me understand why the segfault occurs when i minimize the subwindow and help me figure out how i can solve this problem. Thank you.

我的环境是ubuntu 14.04,正在使用Qt版本:4.8.7SIP版本:4.16.9PyQt 版本:4.11.4MatplotLib版本:1.5.0

My environment is ubuntu 14.04 and using Qt version: 4.8.7 SIP version: 4.16.9 PyQt version: 4.11.4 MatplotLib version: 1.5.0

这里是拖放属性设置的示例.好像也有问题.

Here is an example of drag and drop properties set. It seems that there are issues with that as well.

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()

推荐答案

问题似乎是当最小化时,小部件的高度为负(我想这是有道理的,但是我找不到关于此事实的任何文档;我通过添加一些打印语句注意到了这一点).解决方案是在这些情况下不绘制.我已经提交了 PR 来解决此上游问题,但是您可能需要修修补补matplotlib.backends.backend_qt5agg.FigureCanvasQTAggBase.__draw_idle_agg 与:

The issue seems to be that when minimized the widget has a negative height (I guess that makes sense, but I can not find any documentation of this fact; I noticed this by adding some print statements). The solution is to just not draw in those cases. I have submitted a PR to fix this upstream, but you might need to monky patch matplotlib.backends.backend_qt5agg.FigureCanvasQTAggBase.__draw_idle_agg with:

def __draw_idle_agg(self, *args):
    if self.height() < 0 or self.width() < 0:
        self._agg_draw_pending = False
        return
    try:
        FigureCanvasAgg.draw(self)
        self.update()
    finally:
        self._agg_draw_pending = False

注意模块中的 qt5 不是拼写错误,Qt4 功能源自 Qt5 支持.

Note that the qt5 in the module is not a typo, the Qt4 functionality is derived from the Qt5 support.

这篇关于当我最小化 QMdiSubWindow 时,嵌套在 QMdiSubWindow 段错误中的 FigureCanvasQTAgg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆