[PyQt] QTextEdit "self adjustable"

Paul A. Giannaros paul at giannaros.org
Fri Nov 21 20:49:43 GMT 2008


On Fri, Nov 21, 2008 at 8:24 PM, Celso Fontes <celsowm at gmail.com> wrote:
>
> i tried this:
>
> import sys
> from PyQt4.QtGui  import *
> from PyQt4.QtCore import *
>
> class QuitButton(QWidget):
>
>     def __init__(self, parent=None):
>       QWidget.__init__(self, parent)
>       self.setGeometry(0,0,250, 150)
>       self.lyricsEdit = QTextEdit(self)
>       self.lyricsEdit.setGeometry(QRect(0, 0, 250, 150))
>       self.lyricsEdit.setObjectName("lyricsEdit")
>
>       self.connect(self.lyricsEdit,
>                      SIGNAL("textChanged()"), self.fit)
>
>
>     def fit(self):
>     font = self.lyricsEdit.currentFont()
>     font_metrics = QFontMetrics(font).size(0, self.lyricsEdit.toPlainText())
>     print font_metrics.height()
>
>     while font_metrics.height() > self.lyricsEdit.height():
>       font = self.lyricsEdit.currentFont()
>       font_metrics = QFontMetrics(font).size(0, self.lyricsEdit.toPlainText())
>       font.setPointSize(font.pointSize()-1)
>
>       self.lyricsEdit.setFont(font)
>           font = self.lyricsEdit.currentFont()
>       font_metrics = QFontMetrics(font).size(0, self.lyricsEdit.toPlainText())
>
>
>
> app = QApplication(sys.argv)
> qb = QuitButton()
> qb.show()
> sys.exit(app.exec_())

And what went wrong? Please provide more information, and please don't top post.

>
>
> 2008/11/21 Paul A. Giannaros <paul at giannaros.org>
>>
>> On Fri, Nov 21, 2008 at 3:12 PM, celsowm <celsowm at gmail.com> wrote:
>> >
>> > I hope that this figure exemplifying my "brainstorm"
>> > See this:
>> >
>> > http://picasaweb.google.com.br/celsowm/Screenshots#5271123044270522034
>> >
>> > for this:
>> >
>> > http://picasaweb.google.com.br/celsowm/Screenshots#5270081438738311426
>>
>> Look at the documentation for QFontMetrics. It can tell you what the
>> bounding rectangle in pixels of your text will be. Use an algorithm
>> (binary search or something simpler) to find what the biggest font
>> size that fits within your desired rectangle is.
>>
>> And please structure your questions a little further next time if
>> possible -- it took lots of thinking for me to finally figure out what
>> you were asking about!
>>
>> Regards,
>> Paul
>


More information about the PyQt mailing list