PyQt5 QLabel setWordWrap doesn't work when labels text contains only letters

58 Views Asked by At

So, I am trying to make a whatsapp like app that allow you to chat with someone and works thru using firebase but the thing is message qlabel doesnt work properly. qLabels text is forcing ScrollArea to expand horizontally instead of making itself vetrically bigger. When the text of Qlabel contains spaces between words, setwordwrap works perfectly otherwise it's not working the way I want. I ll share pics of ui below.

With spaces its fine:

with spaces its fine

And the problem:

and the problem

and I'm adding this message widgets dynamicly from code:

    msgContent = QtWidgets.QWidget()
    msgConLayout = QtWidgets.QHBoxLayout()
    userIconWidget = QtWidgets.QWidget()
    userIconWidLayout = QtWidgets.QVBoxLayout()
    msgWidget = QtWidgets.QWidget()
    msgWidLayout = QtWidgets.QVBoxLayout()
    icon = QtWidgets.QLabel()
    message = QtWidgets.QLabel()
    date = QtWidgets.QLabel()

    msgWidget.setStyleSheet("background-color: blue; border-radius: 10px; background-color: rgb(27, 29, 35);")
    icon.setStyleSheet("border-radius: 25px; color: rgb(255, 255, 255); background-color: rgb(44, 49, 60); border: 5px solid rgb(39, 44, 54);")
    message.setStyleSheet("color: rgb(255, 255, 255); padding-top: 10px; padding-left: 15px; padding-right: 15px;")
    date.setStyleSheet("color: rgb(255, 255, 255); padding-left: 10px; padding-right: 10px;")

    icon.setFont(QtGui.QFont("Sagoe UI", 16))
    message.setFont(QtGui.QFont("Sagoe UI", 10))
    date.setFont(QtGui.QFont("Sagoe UI", 10))

    icon.setAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)
    icon.setText("R")
    message.setText(self.ui.ChatInput.text())
    message.setWordWrap(True)
    message.setTextInteractionFlags(QtCore.Qt.TextInteractionFlag.TextSelectableByMouse)
    current_time = QtCore.QTime.currentTime()
    time = current_time.toString("hh:mm")
    date.setText(time)

    userIconWidget.setMaximumWidth(50)
    userIconWidget.setMinimumWidth(50)
    icon.setMinimumSize(50,50)
    icon.setMaximumSize(50,50)
    msgContent.setMinimumHeight(80)
    date.setMaximumHeight(25)
    date.setMinimumHeight(25)

    msgWidLayout.setSpacing(0)
    msgWidLayout.setContentsMargins(0,0,0,0)
    userIconWidLayout.setSpacing(0)
    userIconWidLayout.setContentsMargins(0,0,0,0)
    userIconWidLayout.setAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignBottom)
    msgConLayout.setSpacing(10)
    msgConLayout.setContentsMargins(150,0,0,0)

    userIconWidLayout.addWidget(icon)
    msgWidLayout.addWidget(message)
    msgWidLayout.addWidget(date)
    userIconWidget.setLayout(userIconWidLayout)
    msgWidget.setLayout(msgWidLayout)
    msgConLayout.addStretch()
    msgConLayout.addWidget(msgWidget)
    msgConLayout.addWidget(userIconWidget)
    msgContent.setLayout(msgConLayout)
    self.ui.scrollAreaWidgetContents.layout().addWidget(msgContent)
    self.ui.ChatInput.setText("")  

and what I trying to add:

widgets

I tried to make maximum width of label 300 and this happend:

This time I cant see the whole text:

this time I cant see the whole text

    message.setMaximumWidth(300)
0

There are 0 best solutions below