1

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?

rllysleepy
  • 52
  • 7

1 Answers1

1

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

Alexander
  • 16,091
  • 5
  • 13
  • 29
  • 1
    Oh LMAO, I didn't realize at all! Thank you :) – rllysleepy May 22 '23 at 16:21
  • Oh also, quick question, if you don't mind; how come `QPushButton.__init__(self, parent)` doesn't need to include the `text` variable? And why does it not user `super()`? – rllysleepy May 22 '23 at 23:11
  • Hmm... I just tested it, and it turns out word wrapping doesn't seem to work at all for me, even in isolated conditions with just a label? Does this code work for you? EDIT: NVM, I added invisible spaces and it all works now, thanks for your help! – rllysleepy May 22 '23 at 23:18
  • 1
    @rllysleepy `QPushButton.__init__(self, parent)` is roughly the same thing as `super().__init__(parent)`. And you could pass the `text` with it but then you would be assigning the label value twice, once with the regular label text and once for your custom label. – Alexander May 23 '23 at 00:15
  • Oh right, because super() is creating an instance of QPushButton! Thanks, that makes it clearer :) – rllysleepy May 23 '23 at 00:29