[PyQt] How can I make a column of self expanding QTextEdits?

Jesse Aldridge jessealdridge at gmail.com
Wed Mar 18 01:15:29 GMT 2009


I'm trying to make a tool for finding and replacing text across several
files.

I want two columns, one for before and one for after.

In the left column, I want to display the original contents of the files,
one QTextEdit per file.

In the right column, I want the same thing, only I want to show the files
with the new contents after the replace operation.

I want to stick these two columns side by side in a scroll area so I can
easily scroll through and see all the changes.

I also want the QTextEdits to automatically resize themselves based on the
contents of their file.  This is because I don't want to have to scroll each
TextEdit individually to see all of its contents.

Below is my first attempt at making a column of expanding QTextEdits.  It
doesn't work because the layout seems to be squishing everything down to fit
in the frame.  How can I prevent this behavior?  Or better yet, can anyone
fix my example to make it work as described?

-----------

import sys

from PyQt4 import QtGui, QtCore
QTextEdit = QtGui.QTextEdit

class ExpandoTextEdit(QTextEdit):
    def __init__(self, parent):
        QTextEdit.__init__(self, parent)

        self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)

        self.connect(self, QtCore.SIGNAL("textChanged()"),
                     self.on_text_changed)

        self.setText("Testing\n" * 10)

        self.setMinimumSize(QtCore.QSize(100,100))
        self.setFixedSize(QtCore.QSize(100,100))
    #

    def on_text_changed(self):
        self.setMinimumSize(QtCore.QSize(100,100))
        fm = QtGui.QFontMetrics(self.font())
        bounding_rect = QtCore.QRect(0,0,150,0)
        bounding_rect = fm.boundingRect(bounding_rect,
QtCore.Qt.TextWordWrap,
                                        self.toPlainText())
        bounding_rect.adjust(0,0,30,30)
        size = bounding_rect.size().expandedTo(self.minimumSize())
        self.setFixedSize(size)
        self.adjustSize()
#


app = QtGui.QApplication(sys.argv)

frame = QtGui.QMainWindow()

scrollArea = QtGui.QScrollArea()

frame.setCentralWidget(scrollArea)

layout = QtGui.QVBoxLayout(scrollArea)
scrollArea.setLayout(layout)

text_controls = []
for i in range(10):
    new_text_control = ExpandoTextEdit(frame)
    text_controls.append(new_text_control)
    layout.addWidget(new_text_control)

frame.show()
app.exec_()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20090318/d966589f/attachment.html


More information about the PyQt mailing list