[PyQt] question about resizing behavior

Darren Dale dsdale24 at gmail.com
Thu Jul 2 18:42:04 BST 2009


Hello,

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:


==========================
# requires matplotlib be installed
import sys

from PyQt4 import QtGui, QtCore
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg
from matplotlib.figure import Figure

class DiagramWidget(QtGui.QWidget):
   def __init__(self, parent=None):
       QtGui.QWidget.__init__(self, parent)

       self.scrollbar = QtGui.QScrollBar(QtCore.Qt.Horizontal, self)

       fig = Figure()
       axes = fig.add_subplot(111)
       axes.plot(xrange(100))
       self.diagram = FigureCanvasQTAgg(fig)
       self.diagram.setParent(self)


       layout = QtGui.QVBoxLayout(self)
       self.setLayout(layout)
       layout.addWidget(self.diagram)
       layout.addWidget(self.scrollbar)


a = QtGui.QApplication(sys.argv)
w = DiagramWidget()
w.show()
a.exec_()
==========================

I think the relevant code from matplotlibs plot widget is here:

==========================
    def resizeEvent( self, event ):
        if DEBUG: print 'resize (%d x %d)' % (event.size().width(),
event.size().height())
        QtGui.QWidget.resizeEvent( self, event )
        w = event.size().width()
        h = event.size().height()
        if DEBUG: print "FigureCanvasQtAgg.resizeEvent(", w, ",", h, ")"
        dpival = self.figure.dpi
        winch = w/dpival
        hinch = h/dpival
        self.figure.set_size_inches( winch, hinch )
        self.draw()

    def resize( self, w, h ):
        # Pass through to Qt to resize the widget.
        QtGui.QWidget.resize( self, w, h )

        # Resize the figure by converting pixels to inches.
        pixelPerInch = self.figure.dpi
        wInch = w / pixelPerInch
        hInch = h / pixelPerInch
        self.figure.set_size_inches( wInch, hInch )

        # Redraw everything.
        self.draw()

    def sizeHint( self ):
        w, h = self.get_width_height()
        return QtCore.QSize( w, h )
==========================

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?

Thank you,
Darren
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20090702/99cbcf8a/attachment.html


More information about the PyQt mailing list