[Qt-interest] Help with expandable QTextEdit

Gökmen GÖKSEL gokmen at pardus.org.tr
Tue Dec 30 21:13:28 CET 2008


> Hi everyone,
Hi,

> I want a QTextEdit whose height increases as I type in more lines ( instead
> of a vertical scroll coming). And similarly when I remove some lines, the
> size shrinks back.
Basically it can be produced as follow; -but maybe there is a more qtish way 
of it-

# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(470, 366)

        self.textEdit = QtGui.QTextEdit(Form)
        self.textEdit.setGeometry(QtCore.QRect(80, 50, 301, 21))

        self.fontHeight = 
QtGui.QFontMetrics(self.textEdit.currentFont()).height()

        Form.connect(self.textEdit, QtCore.SIGNAL("textChanged()"), 
self.guessHeight)
        self.guessHeight()

    def guessHeight(self):
        nol = len(str(self.textEdit.toPlainText()).split('\n'))
        if self.textEdit.height() < nol * self.fontHeight:
            self.textEdit.resize(self.textEdit.width(), 
									  self.textEdit.height() + self.fontHeight)
        else:
            self.textEdit.resize(self.textEdit.width(), 
									  nol * self.fontHeight + self.fontHeight)

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

-- 
Gökmen GÖKSEL




More information about the Qt-interest-old mailing list