<br>
I'm trying to make a tool for finding and replacing text across several files.<br>
<br>
I want two columns, one for before and one for after.<br>
<br>
In the left column, I want to display the original contents of the files, one QTextEdit per file.<br>
<br>
In the right column, I want the same thing, only I want to show the files with the new contents after the replace operation.<br>
<br>
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.<br>
<br>
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.<br>
<br>
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?<br>
<br>
-----------<br>
<br>
import sys<br>
<br>
from PyQt4 import QtGui, QtCore<br>
QTextEdit = QtGui.QTextEdit<br>
<br>
class ExpandoTextEdit(QTextEdit):<br>
def __init__(self, parent):<br>
QTextEdit.__init__(self, parent)<br>
<br>
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)<br>
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)<br>
<br>
self.connect(self, QtCore.SIGNAL("textChanged()"),<br>
self.on_text_changed)<br>
<br>
self.setText("Testing\n" * 10)<br>
<br>
self.setMinimumSize(QtCore.QSize(100,100)) <br>
self.setFixedSize(QtCore.QSize(100,100))<br>
#<br>
<br>
def on_text_changed(self):<br>
self.setMinimumSize(QtCore.QSize(100,100)) <br>
fm = QtGui.QFontMetrics(self.font())<br>
bounding_rect = QtCore.QRect(0,0,150,0)<br>
bounding_rect = fm.boundingRect(bounding_rect, QtCore.Qt.TextWordWrap, <br>
self.toPlainText())<br>
bounding_rect.adjust(0,0,30,30)<br>
size = bounding_rect.size().expandedTo(self.minimumSize())<br>
self.setFixedSize(size)<br>
self.adjustSize()<br>
#<br>
<br>
<br>
app = QtGui.QApplication(sys.argv)<br>
<br>
frame = QtGui.QMainWindow()<br>
<br>
scrollArea = QtGui.QScrollArea()<br>
<br>
frame.setCentralWidget(scrollArea)<br>
<br>
layout = QtGui.QVBoxLayout(scrollArea)<br>
scrollArea.setLayout(layout)<br>
<br>
text_controls = []<br>
for i in range(10):<br>
new_text_control = ExpandoTextEdit(frame)<br>
text_controls.append(new_text_control)<br>
layout.addWidget(new_text_control)<br>
<br>
frame.show()<br>
app.exec_()<br>