[PyQt] QLabel word wrap causes incorrect sizeHint
Jugdish
jugdizh at gmail.com
Mon Jan 18 16:00:55 GMT 2010
If you enable wordWrap on a QLabel, it calculates the sizeHint()
incorrectly. When there is a certain length of text in the label, it seems
to calculate the sizeHint() based on if the text were wrapped -- even when
it isn't -- causing the height to be larger than it should. Here is a simple
example:
####################################################
from PyQt4 import QtGui
class MyWidget(QtGui.QWidget):
def __init__(self, wordWrap, parent=None):
super(MyWidget, self).__init__(parent)
self.setFixedWidth(800)
self.label = QtGui.QLabel()
self.label.setFrameStyle(QtGui.QFrame.Box)
self.label.setWordWrap(wordWrap)
self.label.setText("Word wrap is set to '%s'" % wordWrap)
layout = QtGui.QVBoxLayout()
layout.addWidget(self.label)
self.setLayout(layout)
if __name__ == "__main__":
app = QtGui.QApplication([])
w1 = MyWidget(True)
w2 = MyWidget(False)
w1.show()
w2.show()
print "wordWrap sizeHint() =", w1.label.sizeHint()
print "non-wordWrap sizeHint() =", w2.label.sizeHint()
app.exec_()
####################################################
If you run this, you see that the label with wordWrap enabled has a larger
top and bottom margin, and the sizeHint returned is 112x33 whereas the one
without wordWrap has a sizeHint of 169x18.
Any ideas why this is and how to avoid it?
Thanks,
-Jugdish
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20100118/cb7f89a0/attachment.html>
More information about the PyQt
mailing list