1
votes

I have a QLabel inside a QFrame.

Sometimes I have too much text in the QLabel and it resizes the QFrame where it is in.

Now, I want to prevent the QLabel from resizing the QFrame where it resides in. I don't want to limit the amount of lines or setting the maximum size of the QLabel because if the window size of the app increases, I do want to allow the QLabel to increase in size.

Just want to prevent the QLabel from expanding it's parent.

Any clean way to do it?

1
Don't put too much text in it? How do you want to handle the text that is not shown? QLabel is not designed to do it, you'll need a custom widget for that.Kuba hasn't forgotten Monica
Just truncate the QLabel. show as much text as fits.tal

1 Answers

1
votes

Use a QScrollArea (which inherits QFrame), and hide its scrollbars:

label = QtGui.QLabel(text)
frame = QtGui.QScrollArea()
frame.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
frame.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
frame.setWidgetResizable(True)
frame.setWidget(label)

This has the side-benefit that the user will still be able to view any hidden text by scrolling with the mouse-wheel.