2

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?

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
tal
  • 860
  • 7
  • 19

1 Answers1

2

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.

tal
  • 860
  • 7
  • 19
ekhumoro
  • 115,249
  • 20
  • 229
  • 336