Search code examples
pythonqtpyqtpyqt5

PyQt5 label won't wrap text


So I'm trying to make a button with text that wraps, and I've looked into multiple posts in hope of achieving this...

Firstly, I've used the custom PushButton with a text label from this post, but the label won't text wrap. This post has an answer describing two ways to make a label with word wrapping, and none of them work for me.

My code looks like this:

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        container=QWidget()
        self.buttonGridLayout=QGridLayout()
        self.boardButtons=dict()
        for x in range(5):
            for y in range(5):
                self.boardButtons[(x,y)]=RichTextButton(f"wow, this is such a very long string, i hope nothing bad happens")
                self.boardButtons[(x,y)].setFixedSize(150,150)
                self.buttonGridLayout.addWidget(self.boardButtons[(x,y)], y,x)
        container.setLayout(self.buttonGridLayout)
        self.setCentralWidget(container)

class RichTextButton(QPushButton):
    def __init__(self, parent=None):
        QPushButton.__init__(self, parent)
        self.UnitText = QLabel(self)
        self.UnitText.setTextInteractionFlags(Qt.NoTextInteraction)
        self.UnitText.setAlignment(Qt.AlignCenter)
        self.UnitText.setMouseTracking(False)
        self.setLayout(QVBoxLayout())
        self.layout().setContentsMargins(0,0,0,0)
        self.layout().addWidget(self.UnitText)

app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())

As you can see, the buttons are in a grid, and the text assigned to the labels don't wrap at all. How can I fix this without having the buttons expand? Am I doing something wrong?


Solution

  • You are not assigning the text to the label you are creating in your custom button.

    The signature for your RichTextButton only accepts one keyword parameter, parent and then passes it to the super constructor.

    But when you actually define the RichTextButton instance in your main window you are passing it the text that is meant for the label. So your custom button is passing that text to the super constructor and it is being assigned as if it was the text for a standard QPushButton and your QLabel remains empty.

    What you can do is expand the signature for your custom button to accept the text for the button as well and then ensure that the constructor for the QLabel receives the text in it's constructor.

    For example:

    class RichTextButton(QPushButton):
    
        def __init__(self, text, parent=None):   # change signature to allow text
            QPushButton.__init__(self, parent)
            self.UnitText = QLabel(text, self)   # pass the text for the label
            self.UnitText.setTextInteractionFlags(Qt.NoTextInteraction)
            self.UnitText.setWordWrap(True)      # set word wrap for label
            self.UnitText.setAlignment(Qt.AlignCenter)
            self.UnitText.setMouseTracking(False)
            self.setLayout(QVBoxLayout())
            self.layout().setContentsMargins(0,0,0,0)
            self.layout().addWidget(self.UnitText)
    

    I also set setWordWrap(True) on the QLabel