Hello,<br><br>Somebody reported some strange resizing behavior at the matplotlib mailing list. matplotlib has a PyQt4 plot rendering widget, and the script below creates a window containing such a widget and also a scrollbar. If you resize the window's width, the scrollbar often seems to be resized according to the plot widget's previous size. So if I make the window narrower, the scrollbar extends beyond the window, if I make the window wider, the scrollbar does not expand to the edge of the window:<br>
<br><br>==========================<br># requires matplotlib be installed<br>import sys<br><br>from PyQt4 import QtGui, QtCore<br>from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg<br>from matplotlib.figure import Figure<br>
<br>class DiagramWidget(QtGui.QWidget):<br> def __init__(self, parent=None):<br> QtGui.QWidget.__init__(self, parent)<br><br> self.scrollbar = QtGui.QScrollBar(QtCore.Qt.Horizontal, self)<br><br> fig = Figure()<br>
axes = fig.add_subplot(111)<br> axes.plot(xrange(100))<br> self.diagram = FigureCanvasQTAgg(fig)<br> self.diagram.setParent(self)<br><br><br> layout = QtGui.QVBoxLayout(self)<br> self.setLayout(layout)<br>
layout.addWidget(self.diagram)<br> layout.addWidget(self.scrollbar)<br><br><br>a = QtGui.QApplication(sys.argv)<br>w = DiagramWidget()<br>w.show()<br>a.exec_()<br>==========================<br><br>I think the relevant code from matplotlibs plot widget is here:<br>
<br>==========================<br> def resizeEvent( self, event ):<br> if DEBUG: print 'resize (%d x %d)' % (event.size().width(), event.size().height())<br> QtGui.QWidget.resizeEvent( self, event )<br>
w = event.size().width()<br> h = event.size().height()<br> if DEBUG: print "FigureCanvasQtAgg.resizeEvent(", w, ",", h, ")"<br> dpival = self.figure.dpi<br> winch = w/dpival<br>
hinch = h/dpival<br> self.figure.set_size_inches( winch, hinch )<br> self.draw()<br><br> def resize( self, w, h ):<br> # Pass through to Qt to resize the widget.<br> QtGui.QWidget.resize( self, w, h )<br>
<br> # Resize the figure by converting pixels to inches.<br> pixelPerInch = self.figure.dpi<br> wInch = w / pixelPerInch<br> hInch = h / pixelPerInch<br> self.figure.set_size_inches( wInch, hInch )<br>
<br> # Redraw everything.<br> self.draw()<br><br> def sizeHint( self ):<br> w, h = self.get_width_height()<br> return QtCore.QSize( w, h )<br>
==========================<br><br>I have tried commenting out the resize and sizeHint methods, I've tried calling self.update and QtGui.QWidget.resizeEvent(self, event) at the end of the resizeEvent implementation in matplotlib's backend_qt4, but it doesn't seem to have an effect. Could anyone offer an idea of what is going on? Can I do something to improve the resize behavior in matplotlib so it doesnt confuse PyQt/Qt, or is this possibly an artifact/bug in PyQt/Qt?<br>
<br>Thank you,<br>Darren<br>