I need to make the message_container adjust to the size of the label, but up to a maximum of 60% of the scroll_area width. And for some reason, even the short text is moved to a new line. from PyQt6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QScrollArea from PyQt6.QtCore import Qt class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("JChatting") self.setGeometry(100, 100, 800, 600) self.username = "user1" # Main widget and layout main_widget = QWidget() main_layout = QVBoxLayout() main_widget.setLayout(main_layout) self.setCentralWidget(main_widget) # Chat Display self.scroll_area = QScrollArea() self.scroll_area.setWidgetResizable(True) self.chat_container = QWidget() self.chat_layout = QVBoxLayout() self.chat_layout.setAlignment(Qt.AlignmentFlag.AlignTop) self.chat_container.setLayout(self.chat_layout) self.scroll_area.setWidget(self.chat_container) main_layout.addWidget(self.scroll_area) self.add_messages() def display_message(self, sender, message_text): """Display a message in the chat.""" message_widget = QWidget() message_layout = QVBoxLayout() message_widget.setLayout(message_layout) # Create message container message_container = QWidget() message_container_layout = QHBoxLayout() message_container_layout.setContentsMargins(0, 0, 0, 0) message_container_layout.setSpacing(5) message_container.setLayout(message_container_layout) max_container_width = int(self.scroll_area.viewport().width() * 0.6) # Styling the message label = QLabel(f"{sender}: {message_text}") label.setWordWrap(True) label.setStyleSheet(f"font-size: 14px;") styleSheet = f""" border-radius: 10px; padding: 10px; max-width: {max_container_width}; """ if sender == self.username: message_container.setStyleSheet(f"{styleSheet} background-color: #282828;") message_layout.setAlignment(Qt.AlignmentFlag.AlignLeft) else: message_container.setStyleSheet(f"{styleSheet} background-color: #505050;") message_layout.setAlignment(Qt.AlignmentFlag.AlignRight) # Add label to container and container to message layout message_container_layout.addWidget(label) message_layout.addWidget(message_container) self.chat_layout.addWidget(message_widget) # Auto-scroll to the latest message self.scroll_area.verticalScrollBar().setValue( self.scroll_area.verticalScrollBar().maximum() ) def add_messages(self): messages = [ ("user1", "bim bim"), ("Friend4", "bam bam"), ("Friend4", "bom bom"), ("user1", "bim bim"), ("user1", "BAM BAM BAM"), ("user1", "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s..."), ("Friend4", "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s...") ] for sender, message_text in messages: self.display_message(sender, message_text) if __name__ == "__main__": app = QApplication([]) window = MainWindow() window.show() app.exec() I tried to do this by resizing the text and enabling label.setWordWrap(True) only for those that exceed the maximum width of the container, but it didn't work correctly. Some messages still did not fit into the container and were moved to another line, and the container itself did not expand. Continue reading...