bhlee
bhlee

Reputation: 114

How to resize QLabels to fit contents in QScrollArea

This issue is specific to PyQt5, but C++ Qt5 answers are fine too.

Within a QScrollArea with fixed width and variable height, I have a QVBoxLayout that contains QLabels. These QLabels have setWordWrap(True) and contain text which may be longer than the fixed width of the QScrollArea. When the text inside a QLabel wraps to ~4 lines, everything works fine, but when the QLabel requires more than that, it fails to continue increasing the height of the QLabel, and cuts off some of the text on the top and bottom.

This answer tried to fix essentially the same issue, which involves setting the QLabel's vertical sizePolicy() to MinimumExpanding, and this technically worked, but it will force the QLabels to try and fill the entire QScrollArea viewport if the viewport hasn't been filled with QLabels yet.

Here's how it currently looks without the sizePolicy set to MinimumExpanding (notice the 1st QLabel):

sizePolicy not set

Here's how it looks when MinimumExpanding is set as the vertical size policy of the QLabels (Looks great...):

sizePolicy set

But it results in this behavior when there is only a few QLabels in the scroll area, which is unacceptable behavior, as this is going to be a "comments" service, where people can post their questions in plain text:

unacceptable behavior

Does anyone have a workaround for this issue, or experience anything similar to it?

For reference, here is some of my code:

class NewsList(QtWidgets.QScrollArea):
    def __init__(self, parent=None):
        super(NewsList, self).__init__(parent)
        self.setMaximumWidth(200)
        self.setWidgetResizable(True)

        layout = QtWidgets.QVBoxLayout()
        layout.setContentsMargins(5, 5, 5, 5)
        layout.setSpacing(5)
        layout.setAlignment(QtCore.Qt.AlignTop)
        self.news_widget = QtWidgets.QFrame()
        self.news_widget.setStyleSheet("""
            QFrame {
                background-color: #ffffff;
            }
        """)
        self.news_widget.setLayout(layout)
        self.setWidget(self.news_widget)
        self.fetch_news()

    def fetch_news(self):
        self.append_message('DSADAISH dshadbsasdsadh sd ashd sah dsha dhsa dsa d')
        self.append_message('DSADAISH dshadbsasdsadh sd ashd sah dsha dhsa dsa d')
        self.append_message('DSADAISH dshadbsasdsadh sd ashd sah dsha dhsa dsa d DSADAISH dshadbsasdsadh sd ashd sah dsha dhsa dsa d')
        self.append_message('DSADAISH ')
        self.append_message('DSADAISH ')
        self.append_message('DSADAISH dshadbsasdsadh sd ashd sah dsha dhsa dsa d')
        self.append_message('DSADAISH dshadbsasdsadh sd ashd sah dsha dhsa dsa d')

    def append_message(self, text):
        new_item = QtWidgets.QLabel(text)
        new_item.setWordWrap(True)
        new_item.setStyleSheet("""
            QLabel {
                padding: 4px;
                border: 1px solid black;
                background-color: #ffffff;
            }
        """)

Upvotes: 1

Views: 2326

Answers (1)

ekhumoro
ekhumoro

Reputation: 120718

This can be solved quite simply by using the addStretch method of the layout that contains the news items:

class NewsList(QtWidgets.QScrollArea):
    def __init__(self, parent=None):
        ...    
        layout = QtWidgets.QVBoxLayout()
        layout.setContentsMargins(5, 5, 5, 5)
        layout.setSpacing(5)
        layout.setAlignment(QtCore.Qt.AlignTop)
        # add a stretchable space to the bottom of the layout
        layout.addStretch(1)

    def append_message(self, text):
        ...
        # set the size policy of the label
        new_item.setSizePolicy(
            QtWidgets.QSizePolicy.Preferred,
            QtWidgets.QSizePolicy.MinimumExpanding)
        # insert the label before the spacer        
        layout = self.news_widget.layout()
        layout.insertWidget(layout.count() - 1, new_item)

The spacer pushes the labels upwards, which stops them streching to take up the available space. Using the stretch-factor argument of addStretch ensures that the spacer always takes precedence over the other items in the layout.

Upvotes: 2

Related Questions