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?