11

I'm confused why a QPlainTextEdit widget will not resize vertically when added to a QFormLayout. In the code below the text field correctly scales up horizontally, but does not scale up vertically.

Can anyone explain this behavior and offer a solution? I've tried all the tricks I know to no avail.

from PyQt4 import QtGui

class Diag(QtGui.QDialog):

    def __init__(self, parent, *args, **kwargs):
        QtGui.QDialog.__init__(self, parent)
        layout = QtGui.QFormLayout(self)
        widg = QtGui.QPlainTextEdit(self)
        layout.addRow('Entry', widg)

if __name__ == '__main__': #pragma: no cover
    app = QtGui.QApplication([])
    window = Diag(None)
    window.show()
    app.exec_()

Here is an example of the QPlainTextEdit widget not resizing vertically: QPlainTextEdit added to QFormLayout but not resizing vertically

This is on Windows 7 using PyQt 4.5.2 and Python 32-bit 2.6.

Thanks.

Community
  • 1
  • 1
Jon Lauridsen
  • 2,521
  • 5
  • 31
  • 38
  • I get the exact opposite behaviour on both Linux and WinXP. The text-edit resizes vertically to fill the space no matter what settings are chosen (e.g. [setFieldGrowthPolicy()](http://doc.qt.nokia.com/4.8-snapshot/qformlayout.html#fieldGrowthPolicy-prop), [expandingDirections()](http://doc.qt.nokia.com/4.8-snapshot/qlayoutitem.html#expandingDirections), etc) – ekhumoro Nov 16 '12 at 18:04
  • Huh, which Qt version are you using ekhumoro? I've added an example image to my post showing the result I get from my code, and just to be sure I understand you're saying you see the field expanding to fill the widget? – Jon Lauridsen Nov 19 '12 at 10:16
  • A picture is worth a thousand words! Looks like I didn't quite understand your problem correctly. Please see my solution below. – ekhumoro Nov 19 '12 at 18:42

3 Answers3

16

It seems that, by default, a QFormLayout will only resize the height of its fields according to their sizeHint.

To change this behaviour, adjust the vertical stretch as appropriate:

policy = widg.sizePolicy()
policy.setVerticalStretch(1)
widg.setSizePolicy(policy)
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
0

You should set the object in the last row of formlayout (see QPlainTextEdit), its vertical Stretch factor should not be 0.

0

This works for me: it is small at the time of calculating the initial size of the dialog widget and can grow with the dialog once it is already visible

class q2text(QTextEdit):
    def showEvent(self, ev):
        self.updateGeometry()
        return super().showEvent(ev)

    def sizeHint(self):
        if self.isVisible():
            return QSize(99999, 99999)
        else:
            return super().sizeHint()