Qt formlayout not expanding qplaintextedit vertically
Asked Answered
H

3

10

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.

Hifi answered 16/11, 2012 at 11:48 Comment(3)
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(), expandingDirections(), etc)Velasquez
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?Hifi
A picture is worth a thousand words! Looks like I didn't quite understand your problem correctly. Please see my solution below.Velasquez
V
15

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)
Velasquez answered 19/11, 2012 at 18:38 Comment(0)
I
0

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

Ihram answered 23/12, 2018 at 20:26 Comment(0)
D
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()
Djebel answered 2/2, 2023 at 22:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.