[PyQt] sizing a QTextEdit...

Baz Walter bazwal at ftml.net
Thu Sep 9 16:55:52 BST 2010


On 09/09/10 05:12, Peter Milliken wrote:
> Sorry, that previous email should have explained that within the main QFrame
> I am attempting to lay everything out using gridlayout.
>
> On Thu, Sep 9, 2010 at 2:06 PM, Peter Milliken<peter.milliken at gmail.com>wrote:
>
>> Probably not. The code snippet might be a bad example. I am attempting to
>> create a QFrame in the MainWindow and populate it with a number of pairs of
>> lQLabel/QLineEdit. One of the "pairs" needs to be a QLabel/QTextEdit and
>> unfortunately as soon as I add that the grid seems to 'explode' - in the
>> vertical direction.  I just want a QTextEdit sized about 3 or 4 times the
>> height of a QLineEdit.

does this do what you want?


from PyQt4.QtCore import Qt
from PyQt4.QtGui import (
     QApplication, QWidget, QLabel, QLineEdit, QTextEdit,
     QFrame, QGridLayout, QVBoxLayout,
     )

class Window(QWidget):
     def __init__(self):
         QWidget.__init__(self)
         self.resize(400, 200)
         self.frame = QFrame(self)
         self.frame.setFrameShape(QFrame.Box)
         grid = QGridLayout(self.frame)
         grid.addWidget(QLabel('Item 1', self.frame), 0, 0)
         grid.addWidget(QLineEdit(self.frame), 0, 1)
         label = QLabel('Item 2', self.frame)
         label.setAlignment(Qt.AlignLeft|Qt.AlignTop)
         textedit = QTextEdit(self.frame)
         textedit.setMaximumHeight(label.sizeHint().height() * 4)
         grid.addWidget(label, 1, 0)
         grid.addWidget(textedit, 1, 1)
         grid.addWidget(QLabel('Item 3', self.frame), 2, 0)
         grid.addWidget(QLineEdit(self.frame), 2, 1)
         vbox = QVBoxLayout(self)
         vbox.addWidget(self.frame)
         vbox.addStretch()


if __name__ == "__main__":

     import sys
     app = QApplication(sys.argv)
     win = Window()
     win.show()
     sys.exit(app.exec_())


More information about the PyQt mailing list